primevue-mirror/components/datatable/HeaderCell.vue

313 lines
11 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2022-09-14 11:26:01 +00:00
<th
:style="containerStyle"
:class="containerClass"
:tabindex="columnProp('sortable') ? '0' : null"
2022-12-08 11:04:25 +00:00
role="columnheader"
:colspan="columnProp('colspan')"
:rowspan="columnProp('rowspan')"
:aria-sort="ariaSort"
2022-09-14 11:26:01 +00:00
@click="onClick"
@keydown="onKeyDown"
@mousedown="onMouseDown"
@dragstart="onDragStart"
@dragover="onDragOver"
@dragleave="onDragLeave"
@drop="onDrop"
>
<span v-if="resizableColumns && !columnProp('frozen')" class="p-column-resizer" @mousedown="onResizeStart"></span>
2022-09-06 12:03:37 +00:00
<div class="p-column-header-content">
2022-09-14 11:26:01 +00:00
<component v-if="column.children && column.children.header" :is="column.children.header" :column="column" />
<span v-if="columnProp('header')" class="p-column-title">{{ columnProp('header') }}</span>
2022-09-06 12:03:37 +00:00
<span v-if="columnProp('sortable')" :class="sortableColumnIcon"></span>
2022-09-14 11:26:01 +00:00
<span v-if="isMultiSorted()" class="p-sortable-column-badge">{{ getBadgeValue() }}</span>
<DTHeaderCheckbox v-if="columnProp('selectionMode') === 'multiple' && filterDisplay !== 'row'" :checked="allRowsSelected" @change="onHeaderCheckboxChange" :disabled="empty" />
<DTColumnFilter
v-if="filterDisplay === 'menu' && column.children && column.children.filter"
:field="columnProp('filterField') || columnProp('field')"
:type="columnProp('dataType')"
display="menu"
:showMenu="columnProp('showFilterMenu')"
:filterElement="column.children && column.children.filter"
:filterHeaderTemplate="column.children && column.children.filterheader"
:filterFooterTemplate="column.children && column.children.filterfooter"
:filterClearTemplate="column.children && column.children.filterclear"
:filterApplyTemplate="column.children && column.children.filterapply"
:filters="filters"
:filtersStore="filtersStore"
2022-12-08 11:04:25 +00:00
:filterInputProps="filterInputProps"
2022-09-14 11:26:01 +00:00
@filter-change="$emit('filter-change', $event)"
@filter-apply="$emit('filter-apply')"
:filterMenuStyle="columnProp('filterMenuStyle')"
:filterMenuClass="columnProp('filterMenuClass')"
:showOperator="columnProp('showFilterOperator')"
:showClearButton="columnProp('showClearButton')"
:showApplyButton="columnProp('showApplyButton')"
:showMatchModes="columnProp('showFilterMatchModes')"
:showAddButton="columnProp('showAddButton')"
:matchModeOptions="columnProp('filterMatchModeOptions')"
:maxConstraints="columnProp('maxConstraints')"
@operator-change="$emit('operator-change', $event)"
@matchmode-change="$emit('matchmode-change', $event)"
@constraint-add="$emit('constraint-add', $event)"
@constraint-remove="$emit('constraint-remove', $event)"
@apply-click="$emit('apply-click', $event)"
/>
2022-09-06 12:03:37 +00:00
</div>
</th>
</template>
<script>
2022-09-14 11:26:01 +00:00
import { DomHandler, ObjectUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
import ColumnFilter from './ColumnFilter.vue';
2022-12-08 11:04:25 +00:00
import HeaderCheckbox from './HeaderCheckbox.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'HeaderCell',
2022-09-14 11:26:01 +00:00
emits: [
'column-click',
'column-mousedown',
'column-dragstart',
'column-dragover',
'column-dragleave',
'column-drop',
'column-resizestart',
'checkbox-change',
'filter-change',
'filter-apply',
'operator-change',
'matchmode-change',
'constraint-add',
'constraint-remove',
'filter-clear',
'apply-click'
],
2022-09-06 12:03:37 +00:00
props: {
column: {
type: Object,
default: null
},
resizableColumns: {
type: Boolean,
default: false
},
groupRowsBy: {
2022-12-08 11:04:25 +00:00
type: [Array, String, Function],
2022-09-06 12:03:37 +00:00
default: null
},
sortMode: {
type: String,
default: 'single'
},
groupRowSortField: {
type: [String, Function],
default: null
},
sortField: {
type: [String, Function],
default: null
},
sortOrder: {
type: Number,
default: null
},
multiSortMeta: {
type: Array,
default: null
},
allRowsSelected: {
type: Boolean,
default: false
},
empty: {
type: Boolean,
default: false
},
filterDisplay: {
type: String,
default: null
},
filters: {
type: Object,
default: null
},
filtersStore: {
type: Object,
default: null
},
filterColumn: {
type: Boolean,
default: false
},
reorderableColumns: {
type: Boolean,
default: false
2022-12-08 11:04:25 +00:00
},
filterInputProps: {
type: null,
default: null
2022-09-06 12:03:37 +00:00
}
},
data() {
return {
styleObject: {}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
mounted() {
if (this.columnProp('frozen')) {
this.updateStickyPosition();
}
},
updated() {
if (this.columnProp('frozen')) {
this.updateStickyPosition();
}
},
methods: {
columnProp(prop) {
return ObjectUtils.getVNodeProp(this.column, prop);
},
onClick(event) {
2022-09-14 11:26:01 +00:00
this.$emit('column-click', { originalEvent: event, column: this.column });
2022-09-06 12:03:37 +00:00
},
onKeyDown(event) {
2022-12-08 11:04:25 +00:00
if ((event.code === 'Enter' || event.code === 'Space') && event.currentTarget.nodeName === 'TH' && DomHandler.hasClass(event.currentTarget, 'p-sortable-column')) {
2022-09-14 11:26:01 +00:00
this.$emit('column-click', { originalEvent: event, column: this.column });
2022-12-08 11:04:25 +00:00
event.preventDefault();
2022-09-06 12:03:37 +00:00
}
},
onMouseDown(event) {
2022-09-14 11:26:01 +00:00
this.$emit('column-mousedown', { originalEvent: event, column: this.column });
2022-09-06 12:03:37 +00:00
},
onDragStart(event) {
this.$emit('column-dragstart', event);
},
onDragOver(event) {
this.$emit('column-dragover', event);
},
onDragLeave(event) {
this.$emit('column-dragleave', event);
},
onDrop(event) {
this.$emit('column-drop', event);
},
onResizeStart(event) {
this.$emit('column-resizestart', event);
},
getMultiSortMetaIndex() {
2022-09-14 11:26:01 +00:00
return this.multiSortMeta.findIndex((meta) => meta.field === this.columnProp('field') || meta.field === this.columnProp('sortField'));
2022-09-06 12:03:37 +00:00
},
getBadgeValue() {
let index = this.getMultiSortMetaIndex();
2022-09-14 11:26:01 +00:00
return this.groupRowsBy && this.groupRowsBy === this.groupRowSortField && index > -1 ? index : index + 1;
2022-09-06 12:03:37 +00:00
},
isMultiSorted() {
2022-09-14 11:26:01 +00:00
return this.sortMode === 'multiple' && this.columnProp('sortable') && this.getMultiSortMetaIndex() > -1;
2022-09-06 12:03:37 +00:00
},
isColumnSorted() {
2022-09-14 11:26:01 +00:00
return this.sortMode === 'single' ? this.sortField && (this.sortField === this.columnProp('field') || this.sortField === this.columnProp('sortField')) : this.isMultiSorted();
2022-09-06 12:03:37 +00:00
},
updateStickyPosition() {
if (this.columnProp('frozen')) {
let align = this.columnProp('alignFrozen');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (align === 'right') {
let right = 0;
let next = this.$el.nextElementSibling;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (next) {
right = DomHandler.getOuterWidth(next) + parseFloat(next.style.right || 0);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.styleObject.right = right + 'px';
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
let left = 0;
let prev = this.$el.previousElementSibling;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (prev) {
left = DomHandler.getOuterWidth(prev) + parseFloat(prev.style.left || 0);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.styleObject.left = left + 'px';
}
let filterRow = this.$el.parentElement.nextElementSibling;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (filterRow) {
let index = DomHandler.index(this.$el);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
filterRow.children[index].style.left = this.styleObject.left;
filterRow.children[index].style.right = this.styleObject.right;
}
}
},
onHeaderCheckboxChange(event) {
this.$emit('checkbox-change', event);
}
},
computed: {
containerClass() {
2022-09-14 11:26:01 +00:00
return [
this.filterColumn ? this.columnProp('filterHeaderClass') : this.columnProp('headerClass'),
this.columnProp('class'),
{
2022-09-06 12:03:37 +00:00
'p-sortable-column': this.columnProp('sortable'),
'p-resizable-column': this.resizableColumns,
'p-highlight': this.isColumnSorted(),
'p-filter-column': this.filterColumn,
'p-frozen-column': this.columnProp('frozen'),
'p-reorderable-column': this.reorderableColumns
2022-09-14 11:26:01 +00:00
}
];
2022-09-06 12:03:37 +00:00
},
containerStyle() {
2022-09-14 11:26:01 +00:00
let headerStyle = this.filterColumn ? this.columnProp('filterHeaderStyle') : this.columnProp('headerStyle');
2022-09-06 12:03:37 +00:00
let columnStyle = this.columnProp('style');
2022-09-14 11:26:01 +00:00
return this.columnProp('frozen') ? [columnStyle, headerStyle, this.styleObject] : [columnStyle, headerStyle];
2022-09-06 12:03:37 +00:00
},
sortableColumnIcon() {
let sorted = false;
let sortOrder = null;
if (this.sortMode === 'single') {
sorted = this.sortField && (this.sortField === this.columnProp('field') || this.sortField === this.columnProp('sortField'));
2022-09-14 11:26:01 +00:00
sortOrder = sorted ? this.sortOrder : 0;
} else if (this.sortMode === 'multiple') {
2022-09-06 12:03:37 +00:00
let metaIndex = this.getMultiSortMetaIndex();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (metaIndex > -1) {
sorted = true;
sortOrder = this.multiSortMeta[metaIndex].order;
}
}
return [
2022-09-14 11:26:01 +00:00
'p-sortable-column-icon pi pi-fw',
{
2022-09-06 12:03:37 +00:00
'pi-sort-alt': !sorted,
'pi-sort-amount-up-alt': sorted && sortOrder > 0,
'pi-sort-amount-down': sorted && sortOrder < 0
}
];
},
ariaSort() {
if (this.columnProp('sortable')) {
const sortIcon = this.sortableColumnIcon;
2022-09-14 11:26:01 +00:00
if (sortIcon[1]['pi-sort-amount-down']) return 'descending';
else if (sortIcon[1]['pi-sort-amount-up-alt']) return 'ascending';
else return 'none';
} else {
2022-09-06 12:03:37 +00:00
return null;
}
}
},
components: {
2022-09-14 11:26:01 +00:00
DTHeaderCheckbox: HeaderCheckbox,
DTColumnFilter: ColumnFilter
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>