update Table

update Table
This commit is contained in:
梁灏 2016-11-28 16:53:19 +08:00
parent 731d69a29a
commit 35ad37642d
3 changed files with 77 additions and 54 deletions

View file

@ -11,8 +11,8 @@
<template v-else> <template v-else>
{{{ renderHeader(column, $index) }}} {{{ renderHeader(column, $index) }}}
<span :class="[prefixCls + '-sort']" v-if="column.sortable"> <span :class="[prefixCls + '-sort']" v-if="column.sortable">
<i class="ivu-icon ivu-icon-arrow-up-b" :class="{on: sortType === 'asc'}" @click="handleSortAsc($index)"></i> <i class="ivu-icon ivu-icon-arrow-up-b" :class="{on: column._sortType === 'asc'}" @click="handleSort($index, 'asc')"></i>
<i class="ivu-icon ivu-icon-arrow-down-b" :class="{on: sortType === 'desc'}" @click="handleSortDesc($index)"></i> <i class="ivu-icon ivu-icon-arrow-down-b" :class="{on: column._sortType === 'desc'}" @click="handleSort($index, 'desc')"></i>
</span> </span>
</template> </template>
</div> </div>
@ -36,11 +36,6 @@
objData: Object, objData: Object,
fixed: Boolean fixed: Boolean
}, },
data () {
return {
sortType: 'normal'
}
},
computed: { computed: {
isSelectAll () { isSelectAll () {
let isSelectAll = true; let isSelectAll = true;
@ -74,23 +69,11 @@
const status = !this.isSelectAll; const status = !this.isSelectAll;
this.$parent.selectAll(status); this.$parent.selectAll(status);
}, },
handleSortAsc (index) { handleSort (index, type) {
if (this.sortType === 'asc') { if (this.columns[index]._sortType === type) {
this.sortType = 'normal'; type = 'normal';
this.$parent.handleSort(index, 'normal');
} else {
this.sortType = 'asc';
this.$parent.handleSort(index, 'asc');
}
},
handleSortDesc (index) {
if (this.sortType === 'desc') {
this.sortType = 'normal';
this.$parent.handleSort(index, 'normal');
} else {
this.sortType = 'desc';
this.$parent.handleSort(index, 'desc');
} }
this.$parent.handleSort(index, type);
} }
} }
} }

View file

@ -1,5 +1,4 @@
<template> <template>
{{objData|json}}
<div :class="classes" :style="styles"> <div :class="classes" :style="styles">
<div :class="[prefixCls + '-title']" v-if="showSlotHeader" v-el:title><slot name="header"></slot></div> <div :class="[prefixCls + '-title']" v-if="showSlotHeader" v-el:title><slot name="header"></slot></div>
<div :class="[prefixCls + '-header']" v-if="showHeader" v-el:header @mousewheel="handleMouseWheel"> <div :class="[prefixCls + '-header']" v-if="showHeader" v-el:header @mousewheel="handleMouseWheel">
@ -120,12 +119,9 @@
columnsWidth: [], columnsWidth: [],
prefixCls: prefixCls, prefixCls: prefixCls,
compiledUids: [], compiledUids: [],
objData: this.makeObjData(), objData: this.makeObjData(), // checkbox or highlight-row
rebuildData: this.makeData(), // for sort or filter rebuildData: this.makeData(), // for sort or filter
cloneColumns: deepCopy(this.columns), cloneColumns: this.makeColumns(),
leftFixedColumns: [],
rightFixedColumns: [],
centerColumns: [],
showSlotHeader: true, showSlotHeader: true,
showSlotFooter: true, showSlotFooter: true,
bodyHeight: 0 bodyHeight: 0
@ -175,6 +171,24 @@
let style = {}; let style = {};
if (this.bodyHeight !== 0) style.height = `${this.bodyHeight - 1}px`; if (this.bodyHeight !== 0) style.height = `${this.bodyHeight - 1}px`;
return style; return style;
},
leftFixedColumns () {
let left = [];
this.cloneColumns.forEach((col) => {
if (col.fixed && col.fixed === 'left') {
left.push(col);
}
});
return left;
},
rightFixedColumns () {
let right = [];
this.cloneColumns.forEach((col) => {
if (col.fixed && col.fixed === 'right') {
right.push(col);
}
});
return right;
} }
}, },
methods: { methods: {
@ -277,24 +291,6 @@
}) })
} }
}, },
parseColumns () {
let left = [];
let right = [];
let center = [];
this.cloneColumns.forEach((col) => {
if (col.fixed && col.fixed === 'left') {
left.push(col);
} else if (col.fixed && col.fixed === 'right') {
right.push(col);
} else {
center.push(col);
}
});
this.leftFixedColumns = left;
this.rightFixedColumns = right;
this.centerColumns = center;
this.cloneColumns = left.concat(center).concat(right);
},
handleBodyScroll (event) { handleBodyScroll (event) {
if (this.showHeader) this.$els.header.scrollLeft = event.target.scrollLeft; if (this.showHeader) this.$els.header.scrollLeft = event.target.scrollLeft;
if (this.leftFixedColumns.length) this.$els.fixedBody.scrollTop = event.target.scrollTop; if (this.leftFixedColumns.length) this.$els.fixedBody.scrollTop = event.target.scrollTop;
@ -311,15 +307,36 @@
} }
}, },
handleSort (index, type) { handleSort (index, type) {
this.cloneColumns.forEach((col) => col._sortType = 'normal');
const key = this.cloneColumns[index].key;
if (type === 'asc') { if (type === 'asc') {
this.rebuildData.sort((a, b) => { this.rebuildData.sort((a, b) => {
return a.age > b.age; if (this.cloneColumns[index].sortMethod) {
}) return this.cloneColumns[index].sortMethod(a, b);
} else {
return a[key] > b[key];
}
});
} else if (type === 'desc') { } 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') { } else if (type === 'normal') {
this.rebuildData = this.makeData(); this.rebuildData = this.makeData();
} }
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
})
}, },
makeData () { makeData () {
let data = deepCopy(this.data); let data = deepCopy(this.data);
@ -336,10 +353,29 @@
data[index] = newRow; data[index] = newRow;
}); });
return data; return data;
},
makeColumns () {
let columns = deepCopy(this.columns);
let left = [];
let right = [];
let center = [];
columns.forEach((column, index) => {
column._sortType = 'normal';
column._index = index;
if (column.fixed && column.fixed === 'left') {
left.push(column);
} else if (column.fixed && column.fixed === 'right') {
right.push(column);
} else {
center.push(column);
}
});
return left.concat(center).concat(right);
} }
}, },
compiled () { compiled () {
this.parseColumns();
this.showSlotHeader = this.$els.title.innerHTML.replace(/\n/g, '').replace(/<!--[\w\W\r\n]*?-->/gmi, '') !== ''; this.showSlotHeader = this.$els.title.innerHTML.replace(/\n/g, '').replace(/<!--[\w\W\r\n]*?-->/gmi, '') !== '';
this.showSlotFooter = this.$els.footer.innerHTML.replace(/\n/g, '').replace(/<!--[\w\W\r\n]*?-->/gmi, '') !== ''; this.showSlotFooter = this.$els.footer.innerHTML.replace(/\n/g, '').replace(/<!--[\w\W\r\n]*?-->/gmi, '') !== '';
}, },
@ -362,8 +398,7 @@
}, },
columns: { columns: {
handler () { handler () {
this.cloneColumns = deepCopy(this.columns); this.cloneColumns = this.makeColumns();
this.parseColumns();
this.handleResize(); this.handleResize();
}, },
deep: true deep: true

View file

@ -19,7 +19,8 @@
@on-current-change="current" @on-current-change="current"
@on-select="select" @on-select="select"
@on-selection-change="schange" @on-selection-change="schange"
@on-select-all="sall"> @on-select-all="sall"
@on-sort-change="sortChange">
<!--<div slot="header">表格标题</div>--> <!--<div slot="header">表格标题</div>-->
<!--<div slot="footer">表格标题</div>--> <!--<div slot="footer">表格标题</div>-->
</i-table> </i-table>
@ -48,6 +49,7 @@
key: 'name', key: 'name',
align: 'left', align: 'left',
fixed: 'left', fixed: 'left',
sortable: true,
width: 100 width: 100
}, },
{ {
@ -146,6 +148,9 @@
} else { } else {
return ''; return '';
} }
},
sortChange (data) {
console.log(data)
} }
}, },
ready () { ready () {