vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > element表格自定义排序

elementui实现表格自定义排序的示例代码

作者:吃葡萄不吐葡萄皮嘻嘻

本文主要介绍了elementui实现表格自定义排序的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

需求说明:

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

效果图如下:

<template>
<div>
    <template>
        <el-table border :data="previewTableData" style="width:80%;margin:100px auto;" @sort-change="sortChange">
            <el-table-column :sortable="item.val=='showcityname'?false:'custom'" v-for="(item,index) in tableTitle" :prop="item.val" :label="item.name">
            </el-table-column>
        </el-table>
    </template>
</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 == "ascending") {
                    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 == "descending") {
                    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.previewTableData = 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>

到此这篇关于elementui实现表格自定义排序的示例代码的文章就介绍到这了,更多相关element表格自定义排序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文