Element-ui前端自定义排序

1.在需要自定义排序的列上(el-table-column)加上sortable="cistom"

   2.在el-table上增加sort-change事件,监听列的排序

 

//定义需要排序的列,这样可以省去多个if-else if
const actions = new Map([
  ['votes', 'votes'],
  ['calcWeight', 'calcWeight'],
  ['addTransferFee', 'addTransferFee'],
  ['kiloCostBalance', 'kiloCostBalance'],
  ['dispatcherCalcWeight', 'dispatcherCalcWeight'],
  ['pafeiClosureFee', 'pafeiClosureFee'],
  ['kiloPafeiCostBalance', 'kiloPafeiCostBalance']
])

  

使用一个proptype来存放需要排序的列

 // 自定义排序
    sortChange(column) {
      this.queryParams.pageIndex = 1
      const prop = actions.get(column.prop)
      if (prop) {
        this.proptype = prop
        if (column.order === 'ascending') {
          this.tableData.sort(this.ascSortFun)
        } else if (column.order === 'descending') {
          this.tableData.sort(this.desSortFun)
        }
      }
    },
    // 升序排列方法
    ascSortFun(a, b) {
      return a[this.proptype] - b[this.proptype]
    },
    // 降序排列方法
    desSortFun(a, b) {
      return b[this.proptype] - a[this.proptype]
    }

  

原文地址:https://www.cnblogs.com/tangxinwang/p/14047446.html