2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
2023-05-11 08:11:49 +00:00
|
|
|
<th
|
|
|
|
:class="containerClass"
|
2023-06-05 07:34:19 +00:00
|
|
|
:style="[containerStyle]"
|
2023-05-11 08:11:49 +00:00
|
|
|
@click="onClick"
|
|
|
|
@keydown="onKeyDown"
|
|
|
|
:tabindex="columnProp('sortable') ? '0' : null"
|
|
|
|
:aria-sort="ariaSort"
|
|
|
|
role="columnheader"
|
2023-06-05 08:36:46 +00:00
|
|
|
v-bind="{ ...getColumnPT('root'), ...getColumnPT('headerCell') }"
|
2023-06-05 07:34:19 +00:00
|
|
|
:data-p-sortable-column="columnProp('sortable')"
|
|
|
|
:data-p-resizable-column="resizableColumns"
|
|
|
|
:data-p-highlight="isColumnSorted()"
|
|
|
|
:data-p-frozen-column="columnProp('frozen')"
|
2023-05-11 08:11:49 +00:00
|
|
|
>
|
2024-05-13 14:26:45 +00:00
|
|
|
<span v-if="resizableColumns && !columnProp('frozen')" :class="cx('columnResizer')" @mousedown="onResizeStart" v-bind="getColumnPT('columnResizer')"></span>
|
2024-05-13 09:46:55 +00:00
|
|
|
<div :class="cx('columnHeaderContent')" v-bind="getColumnPT('columnHeaderContent')">
|
|
|
|
<component v-if="column.children && column.children.header" :is="column.children.header" :column="column" />
|
|
|
|
<span v-if="columnProp('header')" :class="cx('columnTitle')" v-bind="getColumnPT('columnTitle')">{{ columnProp('header') }}</span>
|
|
|
|
<span v-if="columnProp('sortable')" v-bind="getColumnPT('sort')">
|
|
|
|
<component :is="(column.children && column.children.sorticon) || sortableColumnIcon" :sorted="sortState.sorted" :sortOrder="sortState.sortOrder" :class="cx('sortIcon')" v-bind="getColumnPT('sortIcon')" />
|
|
|
|
</span>
|
2024-05-13 10:08:24 +00:00
|
|
|
<Badge v-if="isMultiSorted()" :class="cx('pcSortBadge')" v-bind="getColumnPT('pcSortBadge')" :value="getMultiSortMetaIndex() + 1" />
|
2024-05-13 09:46:55 +00:00
|
|
|
</div>
|
2022-09-06 12:03:37 +00:00
|
|
|
</th>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2024-05-13 10:08:24 +00:00
|
|
|
import Badge from 'primevue/badge';
|
2023-05-11 08:11:49 +00:00
|
|
|
import BaseComponent from 'primevue/basecomponent';
|
2023-04-18 12:53:43 +00:00
|
|
|
import SortAltIcon from 'primevue/icons/sortalt';
|
|
|
|
import SortAmountDownIcon from 'primevue/icons/sortamountdown';
|
|
|
|
import SortAmountUpAltIcon from 'primevue/icons/sortamountupalt';
|
2022-09-14 11:26:01 +00:00
|
|
|
import { DomHandler, ObjectUtils } from 'primevue/utils';
|
2023-07-13 11:12:07 +00:00
|
|
|
import { mergeProps } from 'vue';
|
2022-09-06 12:03:37 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'HeaderCell',
|
2023-07-04 06:29:36 +00:00
|
|
|
hostName: 'TreeTable',
|
2023-05-11 08:11:49 +00:00
|
|
|
extends: BaseComponent,
|
2022-09-14 11:26:01 +00:00
|
|
|
emits: ['column-click', 'column-resizestart'],
|
2022-09-06 12:03:37 +00:00
|
|
|
props: {
|
|
|
|
column: {
|
|
|
|
type: Object,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
resizableColumns: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
|
|
|
},
|
|
|
|
sortField: {
|
|
|
|
type: [String, Function],
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
sortOrder: {
|
|
|
|
type: Number,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
multiSortMeta: {
|
|
|
|
type: Array,
|
|
|
|
default: null
|
|
|
|
},
|
|
|
|
sortMode: {
|
|
|
|
type: String,
|
|
|
|
default: 'single'
|
2023-06-05 08:36:46 +00:00
|
|
|
},
|
|
|
|
index: {
|
|
|
|
type: Number,
|
|
|
|
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);
|
|
|
|
},
|
2023-06-05 08:36:46 +00:00
|
|
|
getColumnPT(key) {
|
|
|
|
const columnMetaData = {
|
2023-05-11 08:11:49 +00:00
|
|
|
props: this.column.props,
|
|
|
|
parent: {
|
2023-12-05 11:06:32 +00:00
|
|
|
instance: this,
|
2023-05-11 08:11:49 +00:00
|
|
|
props: this.$props,
|
|
|
|
state: this.$data
|
2023-06-05 08:36:46 +00:00
|
|
|
},
|
|
|
|
context: {
|
2023-07-17 09:23:09 +00:00
|
|
|
index: this.index,
|
|
|
|
sorted: this.isColumnSorted(),
|
2023-07-19 12:06:27 +00:00
|
|
|
frozen: this.$parentInstance.scrollable && this.columnProp('frozen'),
|
|
|
|
resizable: this.resizableColumns,
|
|
|
|
scrollable: this.$parentInstance.scrollable,
|
|
|
|
showGridlines: this.$parentInstance.showGridlines,
|
|
|
|
size: this.$parentInstance?.size
|
2023-05-11 08:11:49 +00:00
|
|
|
}
|
2023-06-05 08:36:46 +00:00
|
|
|
};
|
|
|
|
|
2023-07-13 11:12:07 +00:00
|
|
|
return mergeProps(this.ptm(`column.${key}`, { column: columnMetaData }), this.ptm(`column.${key}`, columnMetaData), this.ptmo(this.getColumnProp(), key, columnMetaData));
|
2023-05-11 08:11:49 +00:00
|
|
|
},
|
|
|
|
getColumnProp() {
|
|
|
|
return this.column.props && this.column.props.pt ? this.column.props.pt : undefined; //@todo:
|
|
|
|
},
|
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;
|
2023-11-09 15:02:09 +00:00
|
|
|
let next = DomHandler.getNextElementSibling(this.$el, '[data-p-frozen-column="true"]');
|
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;
|
2023-11-09 15:02:09 +00:00
|
|
|
let prev = DomHandler.getPreviousElementSibling(this.$el, '[data-p-frozen-column="true"]');
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
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) {
|
2023-12-20 10:45:43 +00:00
|
|
|
if ((event.code === 'Enter' || event.code === 'NumpadEnter' || event.code === 'Space') && event.currentTarget.nodeName === 'TH' && DomHandler.getAttribute(event.currentTarget, 'data-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
|
|
|
}
|
|
|
|
},
|
|
|
|
onResizeStart(event) {
|
|
|
|
this.$emit('column-resizestart', event);
|
|
|
|
},
|
|
|
|
getMultiSortMetaIndex() {
|
|
|
|
let index = -1;
|
|
|
|
|
|
|
|
for (let i = 0; i < this.multiSortMeta.length; i++) {
|
|
|
|
let meta = this.multiSortMeta[i];
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
if (meta.field === this.columnProp('field') || meta.field === this.columnProp('sortField')) {
|
|
|
|
index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
},
|
|
|
|
isMultiSorted() {
|
2022-09-14 11:26:01 +00:00
|
|
|
return 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
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
containerClass() {
|
2023-06-05 07:34:19 +00:00
|
|
|
return [this.columnProp('headerClass'), this.columnProp('class'), this.cx('headerCell')];
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
containerStyle() {
|
|
|
|
let headerStyle = this.columnProp('headerStyle');
|
|
|
|
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
|
|
|
},
|
2023-04-14 13:30:18 +00:00
|
|
|
sortState() {
|
2022-09-06 12:03:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-14 13:40:21 +00:00
|
|
|
return {
|
|
|
|
sorted,
|
|
|
|
sortOrder
|
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
2023-04-14 13:30:18 +00:00
|
|
|
sortableColumnIcon() {
|
|
|
|
const { sorted, sortOrder } = this.sortState;
|
|
|
|
|
|
|
|
if (!sorted) return SortAltIcon;
|
|
|
|
else if (sorted && sortOrder > 0) return SortAmountUpAltIcon;
|
|
|
|
else if (sorted && sortOrder < 0) return SortAmountDownIcon;
|
2023-04-13 08:53:28 +00:00
|
|
|
|
2023-04-14 13:30:18 +00:00
|
|
|
return null;
|
2023-04-13 08:53:28 +00:00
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
ariaSort() {
|
|
|
|
if (this.columnProp('sortable')) {
|
2023-04-14 13:30:18 +00:00
|
|
|
const { sorted, sortOrder } = this.sortState;
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2023-04-14 13:30:18 +00:00
|
|
|
if (sorted && sortOrder < 0) return 'descending';
|
|
|
|
else if (sorted && sortOrder > 0) return 'ascending';
|
2022-09-14 11:26:01 +00:00
|
|
|
else return 'none';
|
|
|
|
} else {
|
2022-09-06 12:03:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
}
|
2023-04-13 08:53:28 +00:00
|
|
|
},
|
|
|
|
components: {
|
2024-05-13 10:08:24 +00:00
|
|
|
Badge,
|
2023-04-13 08:53:28 +00:00
|
|
|
SortAltIcon: SortAltIcon,
|
|
|
|
SortAmountUpAltIcon: SortAmountUpAltIcon,
|
|
|
|
SortAmountDownIcon: SortAmountDownIcon
|
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>
|