Element中table排序<针对表格中的数据有空的部分进行处理>

前言:当用到Element的el-table自带的排序<sortable>属性时,如果表格中的数据某一项为空,就会出现排序错乱;


解决思路:将为空的数据所在行置前、置后展示

el-table设置项如下:

<template>
    <!--
        default-sort="sortRule" 对应的为默认的排序规则<内容置于data(){}中>
        @sort-change="handleSortChange" 自定义排序方法进行过滤<方法置于methods中>
        el-table-column中的需要设置sortable<必须指定> 开启排序功能
     -->
    <el-table :data="underclientlistData" :default-sort="sortRule" @sort-change="handleSortChange">
        <el-table-column prop='xxxx' label='xxxx' sortable></el-table-column>
    </el-table>
</template>

<script>
    export default {
        data(){
            return {
                //对应el-table中设置的属性default-sort
                sortRule:{prop:null,order:null},
                underclientlistData:[],//表格数据
            }
        },
        methods:{
            /**
             * 对应表格排序重置方法<el-table中设置的@sort-change=>
             * @param {Object} column 当前列的数据内容
             */
            handleSortChange(column){
               let _this = this
                if (column.order !== null &&  column.prop) {
              const data = [],k = column.prop;
              for (let i = 0; i < _this.underclientlistData.length; i++) { 
                if (_this.underclientlistData[i][k] === null) {
                  data.push(_this.underclientlistData[i])
                } else {
                  data.unshift(_this.underclientlistData[i])
                }
              }
              _this.underclientlistData = data 
            }
            if (column.order === null) {
              _this.underclientlistData = _this.tableData 
            }
            _this.sortRule.prop = column.prop
            _this.sortRule.order = column.order
            }
        }
    }
</script>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值