ux-grid实现表格排序

需求说明:
1、第一行不参与排序
2、实现带%排序
3、实现null值排序
4、实现值相等不排序
5、实现含有占位符‘–‘排序放到最后

表格属性说明文档

效果图如下:

在这里插入图片描述

代码如下:
<template>
  <div>
    <ux-grid
      highlightCurrentRow
      :data="previewTableData"
      @sort-change="sortChange"
      ref="maxTable"
    >
      <ux-table-column type="index" width="60"></ux-table-column>
      <ux-table-column
        :sortable="index!==0?true:false" 
        :remote-sort="index!==0?true:false"
        v-for="(item, index) in tableTitle"
        :key="index"
        :field="item.val"
        :title="item.name"
      >
      </ux-table-column>
    </ux-grid>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableTitle: [
        {
          name: "地区",
          val: "showcityname",
        },
        {
          name: "全为空",
          val: "date",
        },
        {
          name: "数量",
          val: "num",
        },
        {
          name: "包含--",
          val: "address",
        },
        {
          name: "包含null",
          val: "tag",
        },
        {
          name: "包含%",
          val: "bai",
        },
        
      ],
      previewTableData: [
        {
          date: null,
          num: 90,
          address: "--",
          tag: 2,
          bai: "100%",
          all: 1,
          showcityname:"重庆"
        },
        {
          date: null,
          num: 22,
          address: 1,
          tag: null,
          bai: "5%",
          all: 1,
          showcityname:"南岸"

        },
        {
          date: null,
          num: 3,
          address: 3,
          tag: 30,
          bai: null,
          all: 1,
          showcityname:"江北"

        },
        {
          date: null,
          num: 45,
          address: 2,
          tag: 11,
          bai: "-2.4%",
          all: 1,
          showcityname:"渝北"

        },
        {
          date: null,
          num: 5,
          address: "--",
          tag: 49,
          bai: "-8%",
          all: 1,
          showcityname:"九龙坡"

        },
        {
          date: null,
          num: 6,
          address: "--",
          tag: null,
          bai: null,
          all: 1,
          showcityname:"巴南"

        },
      ],
    //   复制一份数据
      previewTableDataClone: [
        {
          date: null,
          num: 90,
          address: "--",
          tag: 2,
          bai: "100%",
          all: 1,
          showcityname:"重庆"
        },
        {
          date: null,
          num: 22,
          address: 1,
          tag: null,
          bai: "5%",
          all: 1,
          showcityname:"南岸"

        },
        {
          date: null,
          num: 3,
          address: 3,
          tag: 30,
          bai: null,
          all: 1,
          showcityname:"江北"

        },
        {
          date: null,
          num: 45,
          address: 2,
          tag: 11,
          bai: "-2.4%",
          all: 1,
          showcityname:"渝北"

        },
        {
          date: null,
          num: 5,
          address: "--",
          tag: 49,
          bai: "-8%",
          all: 1,
          showcityname:"九龙坡"

        },
        {
          date: null,
          num: 6,
          address: "--",
          tag: null,
          bai: null,
          all: 1,
          showcityname:"巴南"

        },
      ],
    };
  },
  created() {},
  methods: {
    sortChange({ column, prop, order }) {
      let arr = [];
      let obj = {};
      console.log(column);
      console.log(prop);
      console.log(order);
      let nullArr = [];
      let otherArr = [];
      //判断是否都为null,true不执行排序,反之排序
      let flag = this.previewTableData.every((item) => !item[prop]);
      let equalVal = this.previewTableData.map((item) => item[prop]);
      let equalValFlag = false;
      //判断是否每个值都相等
      if (equalVal.length) {
        //和第一个值进行比较
        equalValFlag = equalVal.every((item) => item == equalVal[0]);
      }
      console.log(equalValFlag, "equalValFlag", flag, "flag");
      if (!flag && !equalValFlag) {
        console.log("我执行了");
        this.previewTableData.forEach((item, index) => {
          if (item.showcityname == "重庆") {
            console.log(item.showcityname);
            obj = item;
          }
        });
        if (!this.previewTableData.length) return;

        if (order == "asc") {
          this.previewTableData.forEach((item) => {
            if (item[prop] && item[prop] !== "--") {
              otherArr.push(item);
            } else {
              nullArr.push(item);
            }
          });
          // this.previewTableData = [...otherArr,...nullArr]
          this.previewTableData = otherArr.sort((a, b) => {
              if ((a[prop] + "").includes("%") ||(b[prop] + "").includes("%")) {
                return a[prop].replace("%", "") - b[prop].replace("%", "");
              } else {
                return a[prop] - b[prop];
              }
          }).concat(nullArr);
          this.previewTableData.forEach((item, index) => {
            if (item.showcityname == "重庆") {
              this.previewTableData.splice(index, 1);
            }
          });
          if (!Object.keys(obj).length) return;
          this.previewTableData.unshift(obj);
        } else if (order == "desc") {
          this.previewTableData.forEach((item) => {
            if (item[prop] && item[prop] !== "--") {
              otherArr.push(item);
            } else {
              nullArr.push(item);
            }
          });
          // this.previewTableData = [...otherArr,...nullArr]
          this.previewTableData = otherArr
            .sort((a, b) => {
              if (
                (a[prop] + "").includes("%") ||
                (b[prop] + "").includes("%")
              ) {
                return b[prop].replace("%", "") - a[prop].replace("%", "");
              } else {
                return b[prop] - a[prop];
              }
            })
            .concat(nullArr);
          this.previewTableData.forEach((item, index) => {
            if (item.showcityname == "重庆") {
              this.previewTableData.splice(index, 1);
            }
          });
          if (!Object.keys(obj).length) return;
          this.previewTableData.unshift(obj);
        } else {
          // this.previewTableDataClone是在接口得到表格数据的时候拷贝了一份,用来还原取消排序时的表格数据
          this.$refs.maxTable.loadData(this.previewTableDataClone);
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.elx-header--column.col--ellipsis > .elx-cell .elx-cell--title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: pre-wrap;
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吃葡萄不吐葡萄皮嘻嘻

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值