elementui的table字母、数字、汉字混合排序
<el-table-column label="盈亏金额" min-width="25%" sortable :sort-method="sortDevName" prop="profitOrLoss" align="center" class="flex_1"> <template slot-scope="scope"> <el-tag :type="scope.row.profitOrLoss!==0? scope.row.profitOrLoss.indexOf('+')!==-1?'success':'danger':'info'"> {{scope.row.profitOrLoss}} </el-tag> </template> </el-table-column>
// 字母数字混合排序 sortDevName(val1, val2) { let str1 = String(val1.profitOrLoss) let str2 = String(val2.profitOrLoss) let res = 0 for (let i = 0; ; i++) { if (!str1[i] || !str2[i]) { res = str1.length - str2.length break } const char1 = str1 const char1Type = this.getChartType(char1) const char2 = str2 const char2Type = this.getChartType(char2) // 类型相同的逐个比较字符 if (char1Type[0] === char2Type[0]) { if (char1 === char2) { continue } else { if (char1Type[0] === 'zh') { res = char1.localeCompare(char2) } else if (char1Type[0] === 'en') { res = char1.charCodeAt(0) - char2.charCodeAt(0) } else { res = char1 - char2 } break } } else { // 类型不同的,直接用返回的数字相减 res = char1Type[1] - char2Type[1] break } } return res }, getChartType(char) { // 数字可按照排序的要求进行自定义,我这边产品的要求是 // 数字(0->9)->大写字母(A->Z)->小写字母(a->z)->中文拼音(a->z) if (/^[\u4e00-\u9fa5]$/.test(char)) { return ['zh', 300] } if (/^[a-zA-Z]$/.test(char)) { return ['en', 200] } if (/^[0-9]$/.test(char)) { return ['number', 100] } return ['others', 999] },
本文来自博客园,作者:ALin_Da,转载请注明原文链接:https://www.cnblogs.com/alinda/p/15457268.html
。 一个programmer小菜鸟的成长记