日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

el-table文字根據首字母排序

作者:Ybittersweet 更新時間: 2023-07-24 編程語言

首先我們看tabledata的返回數據,里面僅包含了單獨的列名與每行(rows)的數據,此時我們需要對每列數據進行排序,但我們可操作的僅有rows每行數據,這點要搞清,不用去篩選出來列數據,直接對行數據進行操作

需要考慮兩種情況,初始tabledata進行排序與tabledata篩選過之后的filterdata再排序
簡單來說就是一次篩選與篩選再篩選

  • 正序A-Z
  • 倒序Z-A

this.rows代表需要篩選的行數據(在初始化時可以拿到)
@change="change"用于el-select變化時拿到值
sort方法對于含有標點符號的字段和中英文混雜的字段不生效

  <!-- 排序氣泡框 -->
          <el-popover placement="bottom" title="設置排序條件" width="380" trigger="click">
            <el-select v-model="groupList.value1" placeholder="請選擇" size="small" @change="change">
              <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
            <el-button-group style="margin-left: 10px">
              <el-button type="primary" size="small" @click="upOrder">升序</el-button>
              <el-button type="primary" size="small" @click="downOrder">降序</el-button>
            </el-button-group>
            <el-button type="text" icon="el-icon-delete" style="font-size: 16px; margin-left: 10px"
              @click="deleteOption"></el-button>


            <el-button type="text" :underline="false" slot="reference">排序</el-button>
          </el-popover>
         //定義
         name:'',//用于裝el-select 選擇后的e
         rows: [],//用于裝行數據
         filterData:[]//篩選后的數據
          //方法
   change(e) {
      this.name = e;
    },
    //取消排序恢復原樣
    deleteOption() {
    //把最初的tabledata賦值給originalValue
      this.tableData = this.originalValue
    },
    // 升序
    upOrder() {
      if (!this.filterData) {
        this.tableData = [];
        this.rows.sort((a, b) =>
          a.values[this.name] > b.values[this.name] ? 1 : -1
        );
        for (let i = 0; i < this.rows.length; i++) {
          this.tableData.push({ rowId: this.rows[i].id, ...this.rows[i].values });
        }
      } else {
        this.filterData.sort((a, b) => a[this.name].localeCompare(b[this.name]));
      }
    },
    // 降序
    downOrder() {
      if (!this.filterData) {
        this.tableData = [];
      this.rows.sort((a, b) =>
        a.values[this.name] < b.values[this.name] ? 1 : -1
      );
      for (let i = 0; i < this.rows.length; i++) {
        this.tableData.push({ rowId: this.rows[i].id, ...this.rows[i].values });
      }
      }else {
        this.filterData.sort((a, b) => b[this.name].localeCompare(a[this.name]));
      }
    },

原文鏈接:https://blog.csdn.net/Ybittersweet/article/details/129851052

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新