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

157 lines
7.3 KiB
Vue
Raw Normal View History

<template>
2017-01-05 14:10:08 +08:00
<table cellspacing="0" cellpadding="0" border="0" :style="styles">
2016-11-26 08:57:09 +08:00
<colgroup>
2017-03-09 14:11:22 +08:00
<col v-for="(column, index) in columns" :width="setCellWidth(column, index, true)">
2016-11-26 08:57:09 +08:00
</colgroup>
<thead>
<tr>
2017-03-09 14:11:22 +08:00
<th v-for="(column, index) in columns" :class="alignCls(column)">
2016-11-27 01:42:39 +08:00
<div :class="cellClasses(column)">
2017-05-27 15:13:10 +08:00
<template v-if="column.type === 'expand'"></template>
<template v-else-if="column.type === 'selection'"><Checkbox :value="isSelectAll" @on-change="selectAll"></Checkbox></template>
2016-11-27 22:56:05 +08:00
<template v-else>
2017-03-09 14:11:22 +08:00
<span v-html="renderHeader(column, index)"></span>
2016-11-27 22:56:05 +08:00
<span :class="[prefixCls + '-sort']" v-if="column.sortable">
2017-03-09 14:11:22 +08:00
<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: column._sortType === 'desc'}" @click="handleSort(index, 'desc')"></i>
2016-11-27 22:56:05 +08:00
</span>
2016-11-29 11:04:48 +08:00
<Poptip
2016-11-30 13:17:55 +08:00
v-if="isPopperShow(column)"
v-model="column._filterVisible"
2016-11-29 11:04:48 +08:00
placement="bottom"
2017-03-09 14:11:22 +08:00
@on-popper-hide="handleFilterHide(index)">
2016-11-28 21:30:59 +08:00
<span :class="[prefixCls + '-filter']">
2016-11-29 08:43:47 +08:00
<i class="ivu-icon ivu-icon-funnel" :class="{on: column._isFiltered}"></i>
2016-11-28 21:30:59 +08:00
</span>
2016-11-29 11:04:48 +08:00
<div slot="content" :class="[prefixCls + '-filter-list']" v-if="column._filterMultiple">
2016-11-29 08:43:47 +08:00
<div :class="[prefixCls + '-filter-list-item']">
2017-03-09 14:11:22 +08:00
<checkbox-group v-model="column._filterChecked">
<checkbox v-for="item in column.filters" :key="item" :label="item.value">{{ item.label }}</checkbox>
2016-11-29 08:43:47 +08:00
</checkbox-group>
</div>
<div :class="[prefixCls + '-filter-footer']">
2017-03-09 14:11:22 +08:00
<i-button type="text" size="small" :disabled="!column._filterChecked.length" @click.native="handleFilter(index)">{{ t('i.table.confirmFilter') }}</i-button>
<i-button type="text" size="small" @click.native="handleReset(index)">{{ t('i.table.resetFilter') }}</i-button>
2016-11-29 08:43:47 +08:00
</div>
2016-11-28 21:30:59 +08:00
</div>
2016-11-29 11:04:48 +08:00
<div slot="content" :class="[prefixCls + '-filter-list']" v-else>
<ul :class="[prefixCls + '-filter-list-single']">
2016-11-29 13:25:25 +08:00
<li
:class="itemAllClasses(column)"
2017-03-09 14:11:22 +08:00
@click="handleReset(index)">{{ t('i.table.clearFilter') }}</li>
2016-11-29 13:25:25 +08:00
<li
:class="itemClasses(column, item)"
2016-11-29 13:25:25 +08:00
v-for="item in column.filters"
@click="handleSelect(index, item.value)">{{ item.label }}</li>
2016-11-29 11:04:48 +08:00
</ul>
</div>
2016-11-28 21:30:59 +08:00
</Poptip>
2016-11-27 22:56:05 +08:00
</template>
2016-11-26 08:57:09 +08:00
</div>
</th>
</tr>
</thead>
</table>
</template>
<script>
2016-11-29 08:43:47 +08:00
import CheckboxGroup from '../checkbox/checkbox-group.vue';
2016-11-24 15:27:46 +08:00
import Checkbox from '../checkbox/checkbox.vue';
2016-11-28 21:30:59 +08:00
import Poptip from '../poptip/poptip.vue';
2016-11-29 08:43:47 +08:00
import iButton from '../button/button.vue';
2016-11-24 15:27:46 +08:00
import Mixin from './mixin';
import Locale from '../../mixins/locale';
2016-11-24 15:27:46 +08:00
export default {
2017-03-09 14:11:22 +08:00
name: 'TableHead',
mixins: [ Mixin, Locale ],
2016-11-29 08:43:47 +08:00
components: { CheckboxGroup, Checkbox, Poptip, iButton },
props: {
prefixCls: String,
2017-03-09 14:11:22 +08:00
styleObject: Object,
2016-11-24 15:27:46 +08:00
columns: Array,
2016-11-28 15:43:19 +08:00
objData: Object,
2016-11-29 16:29:52 +08:00
data: Array, // rebuildData
columnsWidth: Object,
2016-11-30 13:17:55 +08:00
fixed: {
type: [Boolean, String],
default: false
}
},
computed: {
2017-01-05 14:10:08 +08:00
styles () {
2017-03-09 14:11:22 +08:00
const style = Object.assign({}, this.styleObject);
const width = this.$parent.bodyHeight === 0 ? parseInt(this.styleObject.width) : parseInt(this.styleObject.width) + this.$parent.scrollBarWidth;
2017-01-05 14:10:08 +08:00
style.width = `${width}px`;
return style;
},
2016-11-24 15:27:46 +08:00
isSelectAll () {
2016-11-28 15:43:19 +08:00
let isSelectAll = true;
2016-12-23 10:31:37 +08:00
if (!this.data.length) isSelectAll = false;
2016-11-29 16:29:52 +08:00
for (let i = 0; i < this.data.length; i++) {
if (!this.objData[this.data[i]._index]._isChecked && !this.objData[this.data[i]._index]._isDisabled) {
2016-11-29 16:29:52 +08:00
isSelectAll = false;
break;
}
2016-11-28 15:43:19 +08:00
}
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-12-25 22:49:42 +08:00
];
2016-11-27 01:42:39 +08:00
},
itemClasses (column, item) {
return [
`${this.prefixCls}-filter-select-item`,
{
[`${this.prefixCls}-filter-select-item-selected`]: column._filterChecked[0] === item.value
}
2016-12-25 22:49:42 +08:00
];
},
itemAllClasses (column) {
return [
`${this.prefixCls}-filter-select-item`,
{
[`${this.prefixCls}-filter-select-item-selected`]: !column._filterChecked.length
}
2016-12-25 22:49:42 +08:00
];
},
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
},
2016-11-28 16:53:19 +08:00
handleSort (index, type) {
if (this.columns[index]._sortType === type) {
type = 'normal';
2016-11-28 14:23:49 +08:00
}
2016-11-28 16:53:19 +08:00
this.$parent.handleSort(index, type);
2016-11-28 21:30:59 +08:00
},
handleFilter (index) {
2016-11-29 11:04:48 +08:00
this.$parent.handleFilter(index);
2016-11-29 08:43:47 +08:00
},
2016-11-29 13:25:25 +08:00
handleSelect (index, value) {
this.$parent.handleFilterSelect(index, value);
},
2016-11-29 08:43:47 +08:00
handleReset (index) {
2016-11-29 11:04:48 +08:00
this.$parent.handleFilterReset(index);
2016-11-29 08:43:47 +08:00
},
2016-11-29 11:04:48 +08:00
handleFilterHide (index) {
this.$parent.handleFilterHide(index);
}
}
2016-12-25 22:49:42 +08:00
};
</script>