From 9f853e3ec898d605f2c16d3e456eaf41509cdee3 Mon Sep 17 00:00:00 2001 From: 梁灏 Date: Tue, 29 Nov 2016 15:35:06 +0800 Subject: [PATCH] update Table --- src/components/table/table.vue | 52 +++++++++++++++++++++++++++++++--------------------- test/routers/table.vue | 4 ++-- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/components/table/table.vue b/src/components/table/table.vue index b511728..f056f95 100644 --- a/src/components/table/table.vue +++ b/src/components/table/table.vue @@ -321,39 +321,35 @@ $body.scrollLeft = $body.scrollLeft - 10; } }, + sortData (data, type, index) { + const key = this.cloneColumns[index].key; + data.sort((a, b) => { + if (this.cloneColumns[index].sortMethod) { + return this.cloneColumns[index].sortMethod(a, b); + } else { + return type === 'asc' ? a[key] > b[key] : a[key] < b[key]; + } + }); + return data; + }, handleSort (index, type) { this.cloneColumns.forEach((col) => col._sortType = 'normal'); const key = this.cloneColumns[index].key; if (this.cloneColumns[index].sortable !== 'custom') { // custom is for remote sort - if (type === 'asc') { - this.rebuildData.sort((a, b) => { - if (this.cloneColumns[index].sortMethod) { - return this.cloneColumns[index].sortMethod(a, b); - } else { - return a[key] > b[key]; - } - }); - } else if (type === 'desc') { - this.rebuildData.sort((a, b) => { - if (this.cloneColumns[index].sortMethod) { - return this.cloneColumns[index].sortMethod(a, b); - } else { - return a[key] < b[key]; - } - }); - } else if (type === 'normal') { + if (type === 'normal') { this.rebuildData = this.makeData(); + } else { + this.rebuildData = this.sortData(this.rebuildData, type, index); } } - this.cloneColumns[index]._sortType = type; this.$emit('on-sort-change', { column: JSON.parse(JSON.stringify(this.columns[this.cloneColumns[index]._index])), key: key, order: type - }) + }); }, handleFilterHide (index) { // clear checked that not filter now if (!this.cloneColumns[index]._isFiltered) this.cloneColumns[index]._filterChecked = []; @@ -378,7 +374,7 @@ }, handleFilter (index) { const column = this.cloneColumns[index]; - let filterData = this.makeData(); + let filterData = this.makeDataWithSort(); // filter others first, after filter this column filterData = this.filterOtherData(filterData, index); @@ -396,7 +392,7 @@ this.cloneColumns[index]._filterVisible = false; this.cloneColumns[index]._filterChecked = []; - let filterData = this.makeData(); + let filterData = this.makeDataWithSort(); filterData = this.filterOtherData(filterData, index); this.rebuildData = filterData; }, @@ -405,6 +401,20 @@ data.forEach((row, index) => row._index = index); return data; }, + makeDataWithSort () { + let data = this.makeData(); + let sortType = 'normal'; + let sortIndex = -1; + for (let i = 0; i < this.cloneColumns.length; i++) { + if (this.cloneColumns[i]._sortType !== 'normal') { + sortType = this.cloneColumns[i]._sortType; + sortIndex = i; + break; + } + } + if (sortType !== 'normal') data = this.sortData(data, sortType, sortIndex); + return data; + }, makeObjData () { let data = {}; this.data.forEach((row, index) => { diff --git a/test/routers/table.vue b/test/routers/table.vue index 91afb40..aa363f3 100644 --- a/test/routers/table.vue +++ b/test/routers/table.vue @@ -99,7 +99,7 @@ key: 'age', align: 'right', // fixed: 'left', - sortable: 'custom', + sortable: true, width: 100, filters: [ { @@ -235,7 +235,7 @@ // }); // this.data.splice(0, 1) // this.columns.splice(2,1) - }, 2000); + }, 3000); } } \ No newline at end of file -- libgit2 0.21.4