element table sort-method 动态数据列自定义排序问题

<el-table-column :label="col.gradeSerial" header-align="center" v-for="(col,i) in cList">
  <el-table-column
    sortable
    :label="label"
    :sort-method="(a, b) => sortHandle(a, b, i, 'recordNum')"
    align="center">
    <template slot-scope="scope">
        {{ scope.row.list[i].recordNum }}
    </template>
  </el-table-column>
  <el-table-column
    sortable
    :label="label"
    :sort-method="(a, b) => sortHandle(a, b, i, 'prizeNum')"
    align="center">
    <template slot-scope="scope">
        {{ scope.row.list[i].prizeNum }}
    </template>
  </el-table-column>
</el-table-column>

<!--js部分 -->
sortHandle (a, b, i, type) {
  return a.gradeRecordList[i][type] - b.gradeRecordList[i][type]
}

:sort-method属性通过闭包返回一个函数, 可自定义传参用于自定义排序方法中,
sort-method用法见官方文档

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
el-tableElement UI 提供的表格组件,sort-method 属性用于自定义表格的排序方式。具体步骤如下: 1. 在 el-table 组件上绑定 :data 属性,绑定表格需要显示的数据。 2. 在 el-table-column 组件上绑定 :prop 属性,绑定该所对应的数据字段。 3. 在 el-table-column 组件上绑定 :sortable="customSort" 属性,开启该排序功能,同时传入一个自定义排序函数。 4. 在 el-table 组件上绑定 :sort-method="sortTable" 属性,将自定义排序函数传入表格组件。 示例代码如下: ```html <template> <el-table :data="tableData" :sort-method="sortTable"> <el-table-column prop="name" label="姓名" :sortable="customSort"></el-table-column> <el-table-column prop="age" label="年龄" :sortable="customSort"></el-table-column> <el-table-column prop="gender" label="性别" :sortable="customSort"></el-table-column> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, gender: '男' }, { name: '李四', age: 20, gender: '女' }, { name: '王五', age: 22, gender: '男' }, { name: '赵六', age: 19, gender: '女' } ] } }, methods: { customSort(sortObj) { // 如果需要自定义排序方式,可以在这里添加代码 return true; }, sortTable(sortObj) { // 根据 sortObj 对表格数据进行排序 const { prop, order } = sortObj; this.tableData.sort((a, b) => { const aValue = a[prop]; const bValue = b[prop]; if (order === 'ascending') { return aValue > bValue ? 1 : -1; } else { return aValue < bValue ? 1 : -1; } }); } } } </script> ``` 在上面的示例代码中,我们通过实现 sortTable 方法来自定义表格的排序方式。该方法接收一个 sortObj 参数,包含当前排序的字段和排序方式(升序或降序)。我们通过对 tableData 数组进行排序,来实现表格数据排序

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值