iview/src/components/table/table-head.vue

98 lines
3.5 KiB
Vue
Raw Normal View History

<template>
2016-11-26 08:57:09 +08:00
<table cellspacing="0" cellpadding="0" border="0" :style="style">
<colgroup>
<col v-for="column in columns" :width="setCellWidth(column, $index)">
</colgroup>
<thead>
<tr>
<th v-for="column in columns" :class="alignCls(column)">
2016-11-27 01:42:39 +08:00
<div :class="cellClasses(column)">
2016-11-26 08:57:09 +08:00
<template v-if="column.type === 'selection'"><Checkbox :checked="isSelectAll" @on-change="selectAll"></Checkbox></template>
2016-11-27 22:56:05 +08:00
<template v-else>
{{{ renderHeader(column, $index) }}}
<span :class="[prefixCls + '-sort']" v-if="column.sortable">
2016-11-28 14:23:49 +08:00
<i class="ivu-icon ivu-icon-arrow-up-b" :class="{on: sortType === 'asc'}" @click="handleSortAsc($index)"></i>
<i class="ivu-icon ivu-icon-arrow-down-b" :class="{on: sortType === 'desc'}" @click="handleSortDesc($index)"></i>
2016-11-27 22:56:05 +08:00
</span>
</template>
2016-11-26 08:57:09 +08:00
</div>
</th>
</tr>
</thead>
</table>
</template>
<script>
2016-11-24 15:27:46 +08:00
import Checkbox from '../checkbox/checkbox.vue';
import Mixin from './mixin';
import { deepCopy } from '../../utils/assist';
export default {
2016-11-24 15:27:46 +08:00
mixins: [ Mixin ],
components: { Checkbox },
props: {
prefixCls: String,
2016-11-26 08:57:09 +08:00
style: Object,
2016-11-24 15:27:46 +08:00
columns: Array,
2016-11-28 15:43:19 +08:00
objData: Object,
2016-11-26 08:57:09 +08:00
fixed: Boolean
},
2016-11-28 14:23:49 +08:00
data () {
return {
sortType: 'normal'
}
},
computed: {
2016-11-24 15:27:46 +08:00
isSelectAll () {
2016-11-28 15:43:19 +08:00
let isSelectAll = true;
for (let i in this.objData) {
if (!this.objData[i]._isChecked) isSelectAll = false;
}
return isSelectAll;
2016-11-24 15:27:46 +08:00
}
},
methods: {
2016-11-27 01:42:39 +08:00
cellClasses (column) {
return [
`${this.prefixCls}-cell`,
{
[`${this.prefixCls}-hidden`]: !this.fixed && column.fixed && (column.fixed === 'left' || column.fixed === 'right')
}
]
},
2016-11-26 08:57:09 +08:00
setCellWidth (column, index) {
return this.$parent.setCellWidth(column, index);
},
renderHeader (column, $index) {
if ('renderHeader' in this.columns[$index]) {
return this.columns[$index].renderHeader(column, $index);
} else {
return column.title || '#';
}
},
2016-11-24 15:27:46 +08:00
selectAll () {
const status = !this.isSelectAll;
2016-11-26 10:50:24 +08:00
this.$parent.selectAll(status);
2016-11-27 22:56:05 +08:00
},
handleSortAsc (index) {
2016-11-28 14:23:49 +08:00
if (this.sortType === 'asc') {
this.sortType = 'normal';
this.$parent.handleSort(index, 'normal');
} else {
this.sortType = 'asc';
this.$parent.handleSort(index, 'asc');
}
2016-11-27 22:56:05 +08:00
},
handleSortDesc (index) {
2016-11-28 14:23:49 +08:00
if (this.sortType === 'desc') {
this.sortType = 'normal';
this.$parent.handleSort(index, 'normal');
} else {
this.sortType = 'desc';
this.$parent.handleSort(index, 'desc');
}
}
}
}
2016-11-27 01:42:39 +08:00
</script>