解决el-table排序sortable只排序当前页问题

el-table-column中添加sortable只是让每页数据单独排序,没有把所有数据进行排序,可以通过sort-change事件解决。

1、首先在需要排序的列上加sortable="custom"

javascript 复制代码
 <el-table-column
        prop="date"
        label="日期"
        width="180"
        sortable="custom">
 </el-table-column>

2、在el-table中加@sort-change="sortChange"

javascript 复制代码
 <el-table
      :data="tableData"
      style="width: 100%"
      @sort-change="sortChange">

3、最后添加排序方法sortChange,其中column.prop是属性名,column.order是排序方式

javascript 复制代码
sortChange(column){ 
        var type="";
        for(var i=0;i<this.tableData.length;i++){
          if(this.tableData[i][column.prop]!=null&&this.tableData[i][column.prop]!=undefined&&this.tableData[i][column.prop]!=''){
            type=typeof(this.tableData[i][column.prop]);
            break;
          }
        }
        if(this.tableData.length>0&&type!=""){
          if(column.order=="ascending"){
            this.tableData=this.tableData.sort((a,b)=>{
              if(a[column.prop]==undefined||a[column.prop]==null){
                a[column.prop]='';
              } 
              if(b[column.prop]==undefined||b[column.prop]==null){
                b[column.prop]='';
              }
              if(type=="number"){
                return a[column.prop]-b[column.prop]
              }else{
                return a[column.prop].localeCompare(b[column.prop])
              }
              })
          }else if(column.order=="descending"){
            this.tableData=this.tableData.sort((a,b)=>{
              if(a[column.prop]==undefined||a[column.prop]==null){
                a[column.prop]='';
              } 
              if(b[column.prop]==undefined||b[column.prop]==null){
                b[column.prop]='';
              }
              if(type=="number"){
                return b[column.prop]-a[column.prop]
              }else{
                return b[column.prop].localeCompare(a[column.prop])
              }               
            })
          }
        }
      },
相关推荐
橘子星2 小时前
LLM 无状态架构实践:从原理到代码落地
前端·javascript·人工智能
LiuMingXin2 小时前
意图与代码之间:AI编程范式全景解读
前端·后端·面试
用户83134859306982 小时前
Cesium实现雾气效果:按钮一键控制打开/关闭雾气效果,滑块拖动实时控制雾气浓度
vue.js·cesium
壹方秘境2 小时前
ApiCatcher支持抓包HTTP传输大文件的实现原理分享
前端·后端·客户端
一份执念3 小时前
uni-app项目 (vue+vite + uni-UI)中引入umd格式JS文件,微信小程序中导入报错处理方案
前端·uni-app·echarts
ClouGence3 小时前
2026 年自动化测试工具选型指南:8 款主流工具对比
前端·测试
lichenyang4533 小时前
为什么需要双线程通信、JavaScriptProxy 和 runJavaScript 分别干什么
前端
以和为贵4 小时前
前端也能搞懂 RAG:用 JS 手写一条最小检索增强链路
前端·人工智能·面试
风止何安啊4 小时前
网课倍速痛点解决:一套前端代码实现自由控速播放器
前端·javascript·node.js
牧艺4 小时前
用 Next.js + React Three Fiber 打造 3D 快递仓储可视化
前端·three.js