From ae121eb751ed49c219bfc52a895d8b52fb89ac38 Mon Sep 17 00:00:00 2001 From: huayj Date: Thu, 22 Aug 2019 20:26:45 +0800 Subject: [PATCH] =?UTF-8?q?[=20Feature=20]=20=E6=96=B0=E5=A2=9E=E5=B1=9E?= =?UTF-8?q?=E6=80=A7:sortEnable,=E5=BC=80=E5=90=AF=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E6=8E=92=E5=BA=8F.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/sort.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/lib/sort.js b/src/lib/sort.js index c0e8219..f908713 100644 --- a/src/lib/sort.js +++ b/src/lib/sort.js @@ -244,8 +244,13 @@ export default function sort(Table, Icon) { } let sortButton; - if (column.sorter) { + + // sorter和sortEnable均可触发排序,且sorter优先级更高 + if (column.sorter || column.sortEnable ) { //大于0说明不是升序就是降序,判断orderNum有没有值,没有值赋值 + if ( column.sortEnable && !column.sorter) { + column.sorter = column.type === 'number' ? this.numberSortFn(column.dataIndex) : this.defaultSortFn(column.dataIndex); + } if (iconTypeIndex > 0 && !column.orderNum && mode == "multiple") { column.orderNum = this.getOrderNum(); } @@ -270,6 +275,17 @@ export default function sort(Table, Icon) { return column; }; + // 默认的比较函数,即字符串比较函数 + defaultSortFn = (key) => (a, b)=> { + return a[key] > b[key] ? 1 : -1; + } + // 数值比较函数 + numberSortFn = (key) => (a, b)=> { + let numberA = parseFloat(a[key]); + let numberB = parseFloat(b[key]); + return numberA > numberB ? 1 : -1; + } + _flatToColumn(flatColumns){ const colLen = flatColumns.length; let parentIndex,rsColumns = [];