vue3 el-table 列增加 自定义排序逻辑

在 Vue 3 中使用 Element Plus 的 <el-table> 组件时,如果你想增加自定义排序逻辑,可以通过以下几个步骤实现:

1. 使用 default-sort 属性

首先,你可以在 <el-table> 组件上使用 default-sort 属性来指定默认的排序规则。例如,如果你想默认按照某一列升序排序,可以这样做:

复制代码
<template>
  <el-table :data="tableData" default-sort="{prop: 'date', order: 'ascending'}">
    <el-table-column prop="date" label="日期" sortable />
    <el-table-column prop="name" label="姓名" />
    <el-table-column prop="address" label="地址" />
  </el-table>
</template>

2. 使用 sort-methodsort-comparator 属性

对于自定义排序逻辑,你可以使用 sort-methodsort-comparator 属性。sort-method 适用于简单的比较函数,而 sort-comparator 适用于更复杂的排序逻辑,比如异步排序。

使用 sort-method
复制代码
<template>
  <el-table :data="tableData">
    <el-table-column prop="date" label="日期" sortable :sort-method="dateSortMethod" />
    <el-table-column prop="name" label="姓名" />
    <el-table-column prop="address" label="地址" />
  </el-table>
</template>
 
<script setup>
import { ref } from 'vue';
 
const tableData = ref([...]); // 你的数据数组
 
const dateSortMethod = (a, b) => {
  return new Date(a) - new Date(b); // 示例:按日期字符串排序
};
</script>
使用 sort-comparator(适用于 Element Plus)
复制代码
<template>
  <el-table :data="tableData">
    <el-table-column prop="date" label="日期" sortable :sort-comparator="dateComparator" />
    <el-table-column prop="name" label="姓名" />
    <el-table-column prop="address" label="地址" />
  </el-table>
</template>
 
<script setup>
import { ref } from 'vue';
 
const tableData = ref([...]); // 你的数据数组
 
const dateComparator = (a, b) => {
  return new Date(a.date) - new Date(b.date); // 按日期对象排序,确保数据对象中有 date 属性
};
</script>

3. 使用 sort-change 事件自定义排序行为(动态排序)

如果你需要在用户点击列头进行排序时执行更复杂的逻辑,可以使用 sort-change 事件。这个事件会在列头排序变化时触发,你可以在这个事件处理函数中实现自定义的排序逻辑。

复制代码
<template>
  <el-table :data="tableData" @sort-change="handleSortChange">
    <el-table-column prop="date" label="日期" sortable />
    <el-table-column prop="name" label="姓名" />
    <el-table-column prop="address" label="地址" />
  </el-table>
</template>
 
<script setup>
import { ref } from 'vue';
 
const tableData = ref([...]); // 你的数据数组
const handleSortChange = ({ column, prop, order }) => {
  if (prop === 'date') {
    // 根据日期进行排序的自定义逻辑,例如使用 lodash 的 sortBy 或其他方法进行排序。
    // 这里仅作为示例,实际应根据需求调整排序逻辑。
    if (order === 'ascending') {
      tableData.value.sort((a, b) => new Date(a[prop]) - new Date(b[prop]));
    } else if (order === 'descending') {
      tableData.value.sort((a, b) => new Date(b[prop]) - new Date(a[prop]));
    } else { // order 为 null 表示取消排序,重置数据等逻辑可在此处理。
      // 重置数据或按其他逻辑处理。
    }
  }
};
</script>
相关推荐
不一样的少年_15 小时前
【前端效率工具】再也不用 APIfox 联调!零侵入 Mock,全程不改代码、不开代理
前端·javascript·浏览器
JIngJaneIL16 小时前
数码商城系统|电子|基于SprinBoot+vue的商城推荐系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·毕设·数码商城系统
艾小码16 小时前
Vue组件通信不再难!这8种方式让你彻底搞懂父子兄弟传值
前端·javascript·vue.js
lcc18716 小时前
Vue 数据代理
前端·javascript·vue.js
咖啡の猫17 小时前
Vue基本路由
前端·vue.js·状态模式
青衫码上行17 小时前
【Java Web学习 | 第七篇】JavaScript(1) 基础知识1
java·开发语言·前端·javascript·学习
咖啡の猫17 小时前
Vue编程式路由导航
前端·javascript·vue.js
一 乐1 天前
点餐|智能点餐系统|基于java+ Springboot的动端的点餐系统小程序(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·小程序·论文
视图猿人1 天前
RxJS基本使用及在next.js中使用的例子
开发语言·javascript
bitbitDown1 天前
从零打造一个 Vite 脚手架工具:比你想象的简单多了
前端·javascript·面试