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

235 lines
8.6 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<td :style="containerStyle" :class="containerClass" role="cell" v-bind="{ ...getColumnPT('root'), ...getColumnPT('bodyCell') }" :data-p-frozen-column="columnProp('frozen')">
2024-05-13 07:29:23 +00:00
<div :class="cx('bodyCellContent')" v-bind="getColumnPT('bodyCellContent')">
<button v-if="columnProp('expander')" v-ripple type="button" :class="cx('nodeToggleButton')" @click="toggle" :style="togglerStyle" tabindex="-1" v-bind="getColumnPT('nodeToggleButton')" data-pc-group-section="rowactionbutton">
<template v-if="node.loading && loadingMode === 'icon'">
<component v-if="templates['nodetoggleicon']" :is="templates['nodetoggleicon']" />
<!-- TODO: Deprecated since v4.0-->
<component v-if="templates['nodetogglericon']" :is="templates['nodetogglericon']" />
<SpinnerIcon v-else spin v-bind="ptm('nodetoggleicon')" />
</template>
<template v-else>
<component v-if="column.children && column.children.rowtoggleicon" :is="column.children.rowtoggleicon" :node="node" :expanded="expanded" :class="cx('rowToggleIcon')" />
<!-- TODO: Deprecated since v4.0-->
<component v-if="column.children && column.children.rowtogglericon" :is="column.children.rowtogglericon" :node="node" :expanded="expanded" :class="cx('rowToggleIcon')" />
<component v-else-if="expanded" :is="node.expandedIcon ? 'span' : 'ChevronDownIcon'" :class="cx('rowToggleIcon')" v-bind="getColumnPT('rowToggleIcon')" />
<component v-else :is="node.collapsedIcon ? 'span' : 'ChevronRightIcon'" :class="cx('rowToggleIcon')" v-bind="getColumnPT('rowToggleIcon')" />
</template>
</button>
<Checkbox
v-if="checkboxSelectionMode && columnProp('expander')"
:modelValue="checked"
:binary="true"
:class="cx('pcNodeCheckbox')"
@change="toggleCheckbox"
:tabindex="-1"
:indeterminate="partialChecked"
:unstyled="unstyled"
:pt="getColumnCheckboxPT('pcNodeCheckbox')"
:data-p-partialchecked="partialChecked"
>
<template #icon="slotProps">
<component v-if="templates['checkboxicon']" :is="templates['checkboxicon']" :checked="slotProps.checked" :partialChecked="partialChecked" :class="slotProps.class" />
</template>
</Checkbox>
<component v-if="column.children && column.children.body" :is="column.children.body" :node="node" :column="column" />
<template v-else>{{ resolveFieldData(node.data, columnProp('field')) }}</template>
</div>
2022-09-06 12:03:37 +00:00
</td>
</template>
<script>
2023-05-11 08:11:49 +00:00
import BaseComponent from 'primevue/basecomponent';
2024-01-14 13:38:51 +00:00
import Checkbox from 'primevue/checkbox';
import CheckIcon from 'primevue/icons/check';
import ChevronDownIcon from 'primevue/icons/chevrondown';
import ChevronRightIcon from 'primevue/icons/chevronright';
import MinusIcon from 'primevue/icons/minus';
import SpinnerIcon from 'primevue/icons/spinner';
import Ripple from 'primevue/ripple';
2022-12-08 11:04:25 +00:00
import { DomHandler, ObjectUtils } from 'primevue/utils';
import { mergeProps } from 'vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'BodyCell',
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: ['node-toggle', 'checkbox-toggle'],
2022-09-06 12:03:37 +00:00
props: {
node: {
type: Object,
default: null
},
column: {
type: Object,
default: null
},
level: {
type: Number,
default: 0
},
indentation: {
type: Number,
default: 1
},
leaf: {
type: Boolean,
default: false
},
expanded: {
type: Boolean,
default: false
},
selectionMode: {
type: String,
default: null
},
checked: {
type: Boolean,
default: false
},
partialChecked: {
type: Boolean,
default: false
},
templates: {
type: Object,
default: null
2023-06-05 08:36:46 +00:00
},
index: {
type: Number,
default: null
},
loadingMode: {
type: String,
default: 'mask'
2022-09-06 12:03:37 +00:00
}
},
data() {
return {
2024-01-14 13:38:51 +00:00
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: {
toggle() {
this.$emit('node-toggle', this.node);
},
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: {
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,
selectable: this.$parentInstance.rowHover || this.$parentInstance.rowSelectionMode,
selected: this.$parent.selected,
2023-07-19 12:06:27 +00:00
frozen: this.columnProp('frozen'),
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
};
return mergeProps(this.ptm(`column.${key}`, { column: columnMetaData }), this.ptm(`column.${key}`, columnMetaData), this.ptmo(this.getColumnProp(), key, columnMetaData));
2023-06-05 08:36:46 +00:00
},
getColumnProp() {
return this.column.props && this.column.props.pt ? this.column.props.pt : undefined; //@todo
2023-05-11 08:11:49 +00:00
},
2023-06-05 08:36:46 +00:00
getColumnCheckboxPT(key) {
2023-07-19 12:06:27 +00:00
const columnMetaData = {
2023-05-11 08:11:49 +00:00
props: this.column.props,
parent: {
instance: this,
2023-05-11 08:11:49 +00:00
props: this.$props,
state: this.$data
},
context: {
checked: this.checked,
partialChecked: this.partialChecked
}
2023-07-19 12:06:27 +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
},
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';
}
}
},
resolveFieldData(rowData, field) {
return ObjectUtils.resolveFieldData(rowData, field);
},
toggleCheckbox() {
this.$emit('checkbox-toggle');
}
},
computed: {
containerClass() {
2023-06-05 07:34:19 +00:00
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
},
togglerStyle() {
return {
marginLeft: this.level * this.indentation + 'rem',
visibility: this.leaf ? 'hidden' : 'visible'
};
},
checkboxSelectionMode() {
return this.selectionMode === 'checkbox';
}
},
components: {
2024-01-14 13:38:51 +00:00
Checkbox,
ChevronRightIcon,
ChevronDownIcon,
CheckIcon,
MinusIcon,
SpinnerIcon
},
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>