elementui實現表格自定義排序的示例代碼
更新時間:2023年07月06日 09:44:56 作者:吃葡萄不吐葡萄皮嘻嘻
本文主要介紹了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: "地區(qū)", 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不執(zhí)行排序,反之排序 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("我執(zhí)行了"); 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表格自定義排序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
詳解Vue-cli中的靜態(tài)資源管理(src/assets和static/的區(qū)別)
這篇文章主要介紹了Vue-cli中的靜態(tài)資源管理(src/assets和static/的區(qū)別,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06