1.在<el-table 中加 @sort-change=“changeTableSort_3”

<el-table
          v-else-if="category == 0"
          ref="multipleTable"
          :data="tableData"
          style="width: 100%"
          tooltip-effect="dark"
          @selection-change="handleSelectionChange"
          @sort-change="changeTableSort_3"
        >

2.在methods中添加两个方法,

changeTableSort_3(column) {  //名称排序
      var sortingType = column.order;
      var coprop = column.prop;//此处prop的值对应3例子中的dept
      if (sortingType === "descending") {
        this.tableData = this.tableData.sort((a, b) => this.getForSort(b[coprop], a[coprop], coprop));
      } else {
        this.tableData = this.tableData.sort((a, b) => this.getForSort(a[coprop], b[coprop], coprop));
      }
    },
    
    getForSort(stra, strb, coprop) {
      if (typeof stra === typeof strb && typeof strb === "string") {
        //汉字排序
        return stra.localeCompare(strb);
      } else {
        //数字排序
        return stra - strb;
      }
    }

3.添加 sortable=“custom”

<el-table-column
            prop="dept"
            label="所属机构"
            sortable="custom"
          />
Logo

前往低代码交流专区

更多推荐