primevue-mirror/components/lib/datatable/BodyCell.vue

578 lines
22 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-06-02 11:10:18 +00:00
<td v-if="loading" :style="containerStyle" :class="containerClass" role="cell" v-bind="{ ...getColumnPT('root'), ...getColumnPT('bodyCell') }">
2022-09-14 11:26:01 +00:00
<component :is="column.children.loading" :data="rowData" :column="column" :field="field" :index="rowIndex" :frozenRow="frozenRow" :loadingOptions="loadingOptions" />
2022-09-06 12:03:37 +00:00
</td>
<td
v-else
:style="containerStyle"
:class="containerClass"
:colspan="columnProp('colspan')"
:rowspan="columnProp('rowspan')"
@click="onClick"
@keydown="onKeyDown"
role="cell"
2023-06-02 11:10:18 +00:00
v-bind="{ ...getColumnPT('root'), ...getColumnPT('bodyCell') }"
:data-p-selection-column="columnProp('selectionMode') != null"
:data-p-editable-column="isEditable()"
:data-p-cell-editing="d_editing"
:data-p-frozen-column="columnProp('frozen')"
>
<component
v-if="column.children && column.children.body && !d_editing"
:is="column.children.body"
:data="rowData"
:column="column"
:field="field"
:index="rowIndex"
:frozenRow="frozenRow"
:editorInitCallback="editorInitCallback"
:rowTogglerCallback="toggleRow"
/>
2022-09-14 11:26:01 +00:00
<component
v-else-if="column.children && column.children.editor && d_editing"
:is="column.children.editor"
:data="editingRowData"
:column="column"
:field="field"
:index="rowIndex"
:frozenRow="frozenRow"
:editorSaveCallback="editorSaveCallback"
:editorCancelCallback="editorCancelCallback"
/>
<component v-else-if="column.children && column.children.body && !column.children.editor && d_editing" :is="column.children.body" :data="editingRowData" :column="column" :field="field" :index="rowIndex" :frozenRow="frozenRow" />
2022-09-06 12:03:37 +00:00
<template v-else-if="columnProp('selectionMode')">
<DTRadioButton v-if="columnProp('selectionMode') === 'single'" :value="rowData" :name="name" :checked="selected" @change="toggleRowWithRadio($event, rowIndex)" :column="column" :index="index" :unstyled="unstyled" :pt="pt" />
<DTCheckbox
v-else-if="columnProp('selectionMode') === 'multiple'"
:value="rowData"
:checked="selected"
:rowCheckboxIconTemplate="column.children && column.children.rowcheckboxicon"
:aria-selected="selected ? true : undefined"
@change="toggleRowWithCheckbox($event, rowIndex)"
:column="column"
:index="index"
:unstyled="unstyled"
2023-05-08 14:08:06 +00:00
:pt="pt"
/>
2022-09-06 12:03:37 +00:00
</template>
<template v-else-if="columnProp('rowReorder')">
2024-05-02 09:58:00 +00:00
<component v-if="column.children && column.children.rowreordericon" :is="column.children.rowreordericon" :class="cx('reorderableRowHandle')" />
<i v-else-if="columnProp('rowReorderIcon')" :class="[cx('reorderableRowHandle'), columnProp('rowReorderIcon')]" v-bind="getColumnPT('reorderableRowHandle')" />
<BarsIcon v-else :class="cx('reorderableRowHandle')" v-bind="getColumnPT('reorderableRowHandle')" />
2022-09-06 12:03:37 +00:00
</template>
<template v-else-if="columnProp('expander')">
<button
v-ripple
2024-05-02 12:10:01 +00:00
:class="cx('rowToggleButton')"
type="button"
:aria-expanded="isRowExpanded"
:aria-controls="ariaControls"
:aria-label="expandButtonAriaLabel"
@click="toggleRow"
2024-05-02 12:10:01 +00:00
v-bind="getColumnPT('rowToggleButton')"
data-pc-group-section="rowactionbutton"
>
2024-05-02 12:10:01 +00:00
<component v-if="column.children && column.children.rowtogglericon" :is="column.children.rowtogglericon" :class="cx('rowToggleIcon')" :rowExpanded="isRowExpanded" />
<template v-else>
2024-05-02 12:10:01 +00:00
<span v-if="isRowExpanded && expandedRowIcon" :class="[cx('rowToggleIcon'), expandedRowIcon]" />
<ChevronDownIcon v-else-if="isRowExpanded && !expandedRowIcon" :class="cx('rowToggleIcon')" v-bind="getColumnPT('rowToggleIcon')" />
<span v-else-if="!isRowExpanded && collapsedRowIcon" :class="[cx('rowToggleIcon'), collapsedRowIcon]" />
<ChevronRightIcon v-else-if="!isRowExpanded && !collapsedRowIcon" :class="cx('rowToggleIcon')" v-bind="getColumnPT('rowToggleIcon')" />
</template>
2022-09-06 12:03:37 +00:00
</button>
</template>
<template v-else-if="editMode === 'row' && columnProp('rowEditor')">
<Button
v-if="!d_editing"
2024-05-06 15:23:01 +00:00
:class="cx('pcRowEditorInit')"
:aria-label="initButtonAriaLabel"
:unstyled="unstyled"
@click="onRowEditInit"
2024-05-06 15:23:01 +00:00
v-bind="{ ...getColumnPT('pcRowEditorInit'), ...editButtonProps.init }"
data-pc-group-section="rowactionbutton"
>
<template #icon="slotProps">
2024-05-06 15:23:01 +00:00
<component :is="(column.children && column.children.roweditoriniticon) || 'PencilIcon'" :class="slotProps.class" v-bind="getColumnPT('pcRowEditorInit')['icon']" />
</template>
</Button>
<Button
v-if="d_editing"
2024-05-06 15:23:01 +00:00
:class="cx('pcRowEditorSave')"
:aria-label="saveButtonAriaLabel"
:unstyled="unstyled"
@click="onRowEditSave"
2024-05-06 15:23:01 +00:00
v-bind="{ ...getColumnPT('pcRowEditorSave'), ...editButtonProps.save }"
data-pc-group-section="rowactionbutton"
>
<template #icon="slotProps">
2024-05-06 15:23:01 +00:00
<component :is="(column.children && column.children.roweditorsaveicon) || 'CheckIcon'" :class="slotProps.class" v-bind="getColumnPT('pcRowEditorSave')['icon']" />
</template>
</Button>
<Button
v-if="d_editing"
2024-05-06 15:23:01 +00:00
:class="cx('pcRowEditorCancel')"
:aria-label="cancelButtonAriaLabel"
:unstyled="unstyled"
@click="onRowEditCancel"
2024-05-06 15:23:01 +00:00
v-bind="{ ...getColumnPT('pcRowEditorCancel'), ...editButtonProps.cancel }"
data-pc-group-section="rowactionbutton"
>
<template #icon="slotProps">
2024-05-06 15:23:01 +00:00
<component :is="(column.children && column.children.roweditorcancelicon) || 'TimesIcon'" :class="slotProps.class" v-bind="getColumnPT('pcRowEditorCancel')['icon']" />
</template>
</Button>
2022-09-06 12:03:37 +00:00
</template>
2022-09-14 11:26:01 +00:00
<template v-else>{{ resolveFieldData() }}</template>
2022-09-06 12:03:37 +00:00
</td>
</template>
<script>
2023-05-08 14:08:06 +00:00
import BaseComponent from 'primevue/basecomponent';
import Button from 'primevue/button';
import BarsIcon from 'primevue/icons/bars';
import CheckIcon from 'primevue/icons/check';
import ChevronDownIcon from 'primevue/icons/chevrondown';
import ChevronRightIcon from 'primevue/icons/chevronright';
import PencilIcon from 'primevue/icons/pencil';
import TimesIcon from 'primevue/icons/times';
2022-09-06 12:03:37 +00:00
import OverlayEventBus from 'primevue/overlayeventbus';
import Ripple from 'primevue/ripple';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils } from 'primevue/utils';
import { mergeProps } from 'vue';
2022-12-08 11:04:25 +00:00
import RowCheckbox from './RowCheckbox.vue';
import RowRadioButton from './RowRadioButton.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'BodyCell',
hostName: 'DataTable',
2023-05-08 14:08:06 +00:00
extends: BaseComponent,
2022-09-14 11:26:01 +00:00
emits: ['cell-edit-init', 'cell-edit-complete', 'cell-edit-cancel', 'row-edit-init', 'row-edit-save', 'row-edit-cancel', 'row-toggle', 'radio-change', 'checkbox-change', 'editing-meta-change'],
2022-09-06 12:03:37 +00:00
props: {
rowData: {
type: Object,
default: null
},
column: {
type: Object,
default: null
},
frozenRow: {
type: Boolean,
default: false
},
rowIndex: {
type: Number,
default: null
},
index: {
type: Number,
default: null
},
isRowExpanded: {
type: Boolean,
default: false
2022-09-06 12:03:37 +00:00
},
selected: {
type: Boolean,
default: false
},
editing: {
type: Boolean,
default: false
},
editingMeta: {
type: Object,
default: null
},
editMode: {
type: String,
default: null
},
virtualScrollerContentProps: {
type: Object,
default: null
2022-12-08 11:04:25 +00:00
},
ariaControls: {
type: String,
default: null
},
name: {
type: String,
default: null
},
expandedRowIcon: {
type: String,
default: null
},
collapsedRowIcon: {
type: String,
default: null
},
editButtonProps: {
type: Object,
default: null
2022-09-06 12:03:37 +00:00
}
},
documentEditListener: null,
selfClick: false,
overlayEventListener: null,
data() {
return {
d_editing: this.editing,
styleObject: {}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
editing(newValue) {
this.d_editing = newValue;
},
2022-09-14 11:26:01 +00:00
'$data.d_editing': function (newValue) {
this.$emit('editing-meta-change', { data: this.rowData, field: this.field || `field_${this.index}`, index: this.rowIndex, editing: newValue });
2022-09-06 12:03:37 +00:00
}
},
mounted() {
if (this.columnProp('frozen')) {
this.updateStickyPosition();
}
},
updated() {
if (this.columnProp('frozen')) {
this.updateStickyPosition();
}
if (this.d_editing && (this.editMode === 'cell' || (this.editMode === 'row' && this.columnProp('rowEditor')))) {
setTimeout(() => {
const focusableEl = DomHandler.getFirstFocusableElement(this.$el);
2022-09-14 11:26:01 +00:00
focusableEl && focusableEl.focus();
}, 1);
2022-09-06 12:03:37 +00:00
}
},
beforeUnmount() {
if (this.overlayEventListener) {
OverlayEventBus.off('overlay-click', this.overlayEventListener);
this.overlayEventListener = null;
}
},
methods: {
columnProp(prop) {
return ObjectUtils.getVNodeProp(this.column, prop);
},
2023-06-02 11:10:18 +00:00
getColumnPT(key) {
const columnMetaData = {
props: this.column.props,
parent: {
instance: this,
props: this.$props,
state: this.$data
2023-06-02 11:10:18 +00:00
},
context: {
index: this.index,
size: this.$parentInstance?.$parentInstance?.size,
showGridlines: this.$parentInstance?.$parentInstance?.showGridlines
}
2023-06-02 11:10:18 +00:00
};
return mergeProps(this.ptm(`column.${key}`, { column: columnMetaData }), this.ptm(`column.${key}`, columnMetaData), this.ptmo(this.getColumnProp(), key, columnMetaData));
},
2023-06-02 11:10:18 +00:00
getColumnProp() {
return this.column.props && this.column.props.pt ? this.column.props.pt : undefined;
},
2022-09-06 12:03:37 +00:00
resolveFieldData() {
return ObjectUtils.resolveFieldData(this.rowData, this.field);
},
toggleRow(event) {
this.$emit('row-toggle', {
originalEvent: event,
data: this.rowData
});
},
toggleRowWithRadio(event, index) {
2022-09-14 11:26:01 +00:00
this.$emit('radio-change', { originalEvent: event.originalEvent, index: index, data: event.data });
2022-09-06 12:03:37 +00:00
},
toggleRowWithCheckbox(event, index) {
this.$emit('checkbox-change', { originalEvent: event.originalEvent, index: index, data: event.data });
},
isEditable() {
return this.column.children && this.column.children.editor != null;
},
bindDocumentEditListener() {
if (!this.documentEditListener) {
this.documentEditListener = (event) => {
if (!this.selfClick) {
this.completeEdit(event, 'outside');
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.selfClick = false;
};
document.addEventListener('click', this.documentEditListener);
}
},
unbindDocumentEditListener() {
if (this.documentEditListener) {
document.removeEventListener('click', this.documentEditListener);
this.documentEditListener = null;
this.selfClick = false;
}
},
switchCellToViewMode() {
this.d_editing = false;
this.unbindDocumentEditListener();
OverlayEventBus.off('overlay-click', this.overlayEventListener);
this.overlayEventListener = null;
},
onClick(event) {
if (this.editMode === 'cell' && this.isEditable()) {
this.selfClick = true;
if (!this.d_editing) {
this.d_editing = true;
this.bindDocumentEditListener();
2022-09-14 11:26:01 +00:00
this.$emit('cell-edit-init', { originalEvent: event, data: this.rowData, field: this.field, index: this.rowIndex });
2022-09-06 12:03:37 +00:00
this.overlayEventListener = (e) => {
if (this.$el && this.$el.contains(e.target)) {
this.selfClick = true;
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
OverlayEventBus.on('overlay-click', this.overlayEventListener);
}
}
},
completeEdit(event, type) {
const completeEvent = {
originalEvent: event,
data: this.rowData,
newData: this.editingRowData,
value: this.rowData[this.field],
newValue: this.editingRowData[this.field],
field: this.field,
index: this.rowIndex,
type: type,
defaultPrevented: false,
2022-09-14 11:26:01 +00:00
preventDefault: function () {
2022-09-06 12:03:37 +00:00
this.defaultPrevented = true;
}
};
this.$emit('cell-edit-complete', completeEvent);
if (!completeEvent.defaultPrevented) {
this.switchCellToViewMode();
}
},
onKeyDown(event) {
if (this.editMode === 'cell') {
2022-12-08 11:04:25 +00:00
switch (event.code) {
case 'Enter':
case 'NumpadEnter':
2022-09-06 12:03:37 +00:00
this.completeEdit(event, 'enter');
2022-09-14 11:26:01 +00:00
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'Escape':
2022-09-06 12:03:37 +00:00
this.switchCellToViewMode();
2022-09-14 11:26:01 +00:00
this.$emit('cell-edit-cancel', { originalEvent: event, data: this.rowData, field: this.field, index: this.rowIndex });
break;
2022-09-06 12:03:37 +00:00
2022-12-08 11:04:25 +00:00
case 'Tab':
2022-09-06 12:03:37 +00:00
this.completeEdit(event, 'tab');
2022-09-14 11:26:01 +00:00
if (event.shiftKey) this.moveToPreviousCell(event);
else this.moveToNextCell(event);
break;
2022-12-08 11:04:25 +00:00
default:
break;
2022-09-06 12:03:37 +00:00
}
}
},
moveToPreviousCell(event) {
let currentCell = this.findCell(event.target);
let targetCell = this.findPreviousEditableColumn(currentCell);
if (targetCell) {
DomHandler.invokeElementMethod(targetCell, 'click');
event.preventDefault();
}
},
moveToNextCell(event) {
let currentCell = this.findCell(event.target);
let targetCell = this.findNextEditableColumn(currentCell);
if (targetCell) {
DomHandler.invokeElementMethod(targetCell, 'click');
event.preventDefault();
}
},
findCell(element) {
if (element) {
let cell = element;
2022-09-14 11:26:01 +00:00
while (cell && !DomHandler.getAttribute(cell, 'data-p-cell-editing')) {
2022-09-06 12:03:37 +00:00
cell = cell.parentElement;
}
return cell;
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
return null;
}
},
findPreviousEditableColumn(cell) {
let prevCell = cell.previousElementSibling;
if (!prevCell) {
let previousRow = cell.parentElement.previousElementSibling;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (previousRow) {
prevCell = previousRow.lastElementChild;
}
}
if (prevCell) {
if (DomHandler.getAttribute(prevCell, 'data-p-editable-column')) return prevCell;
2022-09-14 11:26:01 +00:00
else return this.findPreviousEditableColumn(prevCell);
} else {
2022-09-06 12:03:37 +00:00
return null;
}
},
findNextEditableColumn(cell) {
let nextCell = cell.nextElementSibling;
if (!nextCell) {
let nextRow = cell.parentElement.nextElementSibling;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (nextRow) {
nextCell = nextRow.firstElementChild;
}
}
if (nextCell) {
if (DomHandler.getAttribute(nextCell, 'data-p-editable-column')) return nextCell;
2022-09-14 11:26:01 +00:00
else return this.findNextEditableColumn(nextCell);
} else {
2022-09-06 12:03:37 +00:00
return null;
}
},
onRowEditInit(event) {
2022-09-14 11:26:01 +00:00
this.$emit('row-edit-init', { originalEvent: event, data: this.rowData, newData: this.editingRowData, field: this.field, index: this.rowIndex });
2022-09-06 12:03:37 +00:00
},
onRowEditSave(event) {
2022-09-14 11:26:01 +00:00
this.$emit('row-edit-save', { originalEvent: event, data: this.rowData, newData: this.editingRowData, field: this.field, index: this.rowIndex });
2022-09-06 12:03:37 +00:00
},
onRowEditCancel(event) {
2022-09-14 11:26:01 +00:00
this.$emit('row-edit-cancel', { originalEvent: event, data: this.rowData, newData: this.editingRowData, field: this.field, index: this.rowIndex });
2022-09-06 12:03:37 +00:00
},
editorInitCallback(event) {
2022-09-14 11:26:01 +00:00
this.$emit('row-edit-init', { originalEvent: event, data: this.rowData, newData: this.editingRowData, field: this.field, index: this.rowIndex });
2022-09-06 12:03:37 +00:00
},
editorSaveCallback(event) {
if (this.editMode === 'row') {
2022-09-14 11:26:01 +00:00
this.$emit('row-edit-save', { originalEvent: event, data: this.rowData, newData: this.editingRowData, field: this.field, index: this.rowIndex });
2022-09-06 12:03:37 +00:00
} else {
this.completeEdit(event, 'enter');
}
},
editorCancelCallback(event) {
if (this.editMode === 'row') {
2022-09-14 11:26:01 +00:00
this.$emit('row-edit-cancel', { originalEvent: event, data: this.rowData, newData: this.editingRowData, field: this.field, index: this.rowIndex });
2022-09-06 12:03:37 +00:00
} else {
this.switchCellToViewMode();
2022-09-14 11:26:01 +00:00
this.$emit('cell-edit-cancel', { originalEvent: event, data: this.rowData, field: this.field, index: this.rowIndex });
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 = 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;
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';
}
}
},
getVirtualScrollerProp(option) {
return this.virtualScrollerContentProps ? this.virtualScrollerContentProps[option] : null;
}
},
computed: {
editingRowData() {
return this.editingMeta[this.rowIndex] ? this.editingMeta[this.rowIndex].data : this.rowData;
},
field() {
return this.columnProp('field');
},
containerClass() {
return [this.columnProp('bodyClass'), this.columnProp('class'), this.cx('bodyCell')];
2022-09-06 12:03:37 +00:00
},
containerStyle() {
let bodyStyle = this.columnProp('bodyStyle');
let columnStyle = this.columnProp('style');
2022-09-14 11:26:01 +00:00
return this.columnProp('frozen') ? [columnStyle, bodyStyle, this.styleObject] : [columnStyle, bodyStyle];
2022-09-06 12:03:37 +00:00
},
loading() {
return this.getVirtualScrollerProp('loading');
},
loadingOptions() {
const getLoaderOptions = this.getVirtualScrollerProp('getLoaderOptions');
2022-09-14 11:26:01 +00:00
return (
getLoaderOptions &&
getLoaderOptions(this.rowIndex, {
cellIndex: this.index,
cellFirst: this.index === 0,
cellLast: this.index === this.getVirtualScrollerProp('columns').length - 1,
cellEven: this.index % 2 === 0,
cellOdd: this.index % 2 !== 0,
column: this.column,
field: this.field
})
);
2022-12-08 11:04:25 +00:00
},
expandButtonAriaLabel() {
return this.$primevue.config.locale.aria ? (this.isRowExpanded ? this.$primevue.config.locale.aria.expandRow : this.$primevue.config.locale.aria.collapseRow) : undefined;
},
initButtonAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.editRow : undefined;
},
saveButtonAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.saveEdit : undefined;
},
cancelButtonAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.cancelEdit : undefined;
2022-09-06 12:03:37 +00:00
}
},
components: {
2022-09-14 11:26:01 +00:00
DTRadioButton: RowRadioButton,
DTCheckbox: RowCheckbox,
Button,
ChevronDownIcon,
ChevronRightIcon,
BarsIcon,
PencilIcon,
CheckIcon,
TimesIcon
2022-09-06 12:03:37 +00:00
},
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
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>