From 5073e7f5fda8ffd9b508014bd5e00408d8ee9b13 Mon Sep 17 00:00:00 2001 From: huayj Date: Wed, 28 Aug 2019 10:47:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BA=8F=E5=8F=B7=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E6=96=B9=E5=BC=8F=EF=BC=8C=E6=8E=92=E5=BA=8F=E6=96=B0?= =?UTF-8?q?=E5=A2=9EstringChinese=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ColumnManager.js | 20 ++++++++++++++++---- src/Table.js | 32 ++++++++++++++++---------------- src/lib/sort.js | 20 +++++++++++++++++++- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/src/ColumnManager.js b/src/ColumnManager.js index f1bfbf7..bf9c41d 100644 --- a/src/ColumnManager.js +++ b/src/ColumnManager.js @@ -35,12 +35,24 @@ export default class ColumnManager { if(!showRowNum){ return columns } + let { key, fixed, width, name, type, base } = showRowNum; let order = { - dataIndex: showRowNum.key || '_index', + dataIndex: key || '_index', key:'_index', - fixed:showRowNum.fixed || 'left', - width:showRowNum.width || 50, - title: showRowNum.name || '序号', + fixed:fixed || 'left', + width:width || 50, + title: name || '序号', + render:(text, record, index)=>{ + switch( type ){ + case 'ascii':{ + return (String.fromCharCode((base || 'a').charCodeAt()) + index); + } + case 'number': + default:{ + return ( (base || 0) + index); + } + } + } } if(columns.length > 0 && columns[0].dataIndex !== 'checkbox' && columns[0].dataIndex !== 'radio'){ // 多选表格/单选表格时放在第二列,其他情况放到第一列 columns = [order].concat(columns); diff --git a/src/Table.js b/src/Table.js index 42d6e0a..a27acc9 100644 --- a/src/Table.js +++ b/src/Table.js @@ -691,23 +691,23 @@ class Table extends Component { const lazyEndIndex = props.lazyLoad && props.lazyLoad.endIndex ?props.lazyLoad.endIndex :-1; for (let i = 0; i < data.length; i++) { let isHiddenExpandIcon; - if ( props.showRowNum ){ - switch(props.showRowNum.type){ - case 'number':{ - data[i][props.showRowNum.key || '_index'] = (props.showRowNum.base || 0) + i; - break; - } - case 'ascii': { - data[i][props.showRowNum.key || '_index'] = String.fromCharCode(i + (props.showRowNum.base || '0').charCodeAt()); - break; - } - default: { - data[i][props.showRowNum.key || '_index'] = (props.showRowNum.base || 0) + i; - break; - } - } + // if ( props.showRowNum ){ + // switch(props.showRowNum.type){ + // case 'number':{ + // data[i][props.showRowNum.key || '_index'] = (props.showRowNum.base || 0) + i; + // break; + // } + // case 'ascii': { + // data[i][props.showRowNum.key || '_index'] = String.fromCharCode(i + (props.showRowNum.base || '0').charCodeAt()); + // break; + // } + // default: { + // data[i][props.showRowNum.key || '_index'] = (props.showRowNum.base || 0) + i; + // break; + // } + // } - } + // } const record = data[i]; const key = this.getRowKey(record, i); const childrenColumn = record[childrenColumnName]; diff --git a/src/lib/sort.js b/src/lib/sort.js index 62fe5de..1e4a4bf 100644 --- a/src/lib/sort.js +++ b/src/lib/sort.js @@ -249,7 +249,20 @@ export default function sort(Table, Icon) { if (column.sorter || column.sortEnable ) { //大于0说明不是升序就是降序,判断orderNum有没有值,没有值赋值 if ( column.sortEnable && !column.sorter) { - column.sorter = column.fieldType === 'number' ? this.numberSortFn(column.dataIndex) : this.defaultSortFn(column.dataIndex); + switch(column.fieldType){ + case 'number':{ + column.sorter = this.numberSortFn(column.dataIndex); + break; + } + case 'stringChinese':{ + column.sorter = this.chineseSortFn(column.dataIndex); + break; + } + default:{ + column.sorter = this.defaultSortFn(column.dataIndex); + break; + } + } } if (iconTypeIndex > 0 && !column.orderNum && mode == "multiple") { column.orderNum = this.getOrderNum(); @@ -286,6 +299,11 @@ export default function sort(Table, Icon) { return numberA >= numberB ? 1 : -1; } + // 中文比较函数,按拼音排序 + chineseSortFn = (key) => (a, b)=>{ + return a[key].localeCompare(b[key], 'zh-Hans-CN',{sensitivity: 'accent'}); + } + _flatToColumn(flatColumns){ const colLen = flatColumns.length; let parentIndex,rsColumns = [];