<script>
export default {
name: "VXETable2",
props: {
toggleMethod: {
type: Function,
},
columns: {
type: Array,
default: () => [],
},
autoResize: {
type: Boolean,
default: true,
},
border: {
type: Boolean,
default: true,
},
rowKey: {
type: Boolean,
default: true,
},
resizable: {
type: Boolean,
default: true,
},
showOverflow: {
type: Boolean,
default: true,
},
highlightHoverRow: {
type: Boolean,
default: true,
},
height: {
type: [String, Number],
default: "auto",
},
maxHeight: {
type: String,
default: "",
},
align: {
type: String,
default: "center",
},
rowHeight: {
type: String,
default: "",
},
rowStyle: {
type: [String, Object, Function],
},
containerHeight: {
type: String,
default: "100%",
},
containerMaxHeight: { type: String, default: "" },
containerMinHeight: { type: String, default: "" },
pager: {
type: Boolean,
default: false,
},
currentPage: {
type: Number,
default: 1,
},
pageSize: {
type: Number,
default: 5,
},
pageSizes: {
type: Array,
default: () => [50, 100, 150],
},
total: {
type: Number,
default: 0,
},
sortMethod: {
type: Boolean,
default: false,
},
checkField: {
type: String,
default: "pitch",
},
autoHidden: {
type: Boolean,
default: false,
},
showHeader: {
type: Boolean,
default: true,
},
checkMethod: Function,
},
render(h) {
return (
<div>
<div
style={{
height: this.containerHeight,
width: "100%",
maxHeight: this.containerMaxHeight,
minHeight: this.containerMinHeight,
}}
>
<vxe-table
scroll-y={{ enabled: true, gt: 0 }}
row-style={this.rowStyle}
ref="xTable"
row-config={{ height: this.rowHeight }}
border={this.border}
auto-resize={this.autoResize}
row-key={this.rowKey}
resizable={this.resizable}
show-overflow={this.showOverflow}
highlight-hover-row={this.highlightHoverRow}
align={this.align}
height={this.height}
max-height={this.maxHeight}
expand-config={{
trigger: "cell",
showIcon: false,
accordion: true,
toggleMethod: this.toggleMethod,
}}
resizable-config={{ minWidth: 10 }}
radio-config={{ trigger: "row" }}
checkbox-config={{
checkField: this.checkField,
trigger: "row",
checkMethod: this.checkMethod,
showHeader: this.showHeader,
}}
onSort-change={this.sortChangeEvent}
sort-config={{
remote: this.sortMethod,
trigger: "cell",
orders: ["asc", "desc", "default"],
}}
clear-sort={this.clearSort}
onCell-click={this.cellClick}
onCell-dblclick={this.onCellDblclick}
onCheckbox-change={this.checkbox}
onCheckbox-all={this.checkbox}
onRadio-change={this.radioChange}
>
{this.columns.map((item, index) => {
return item.type === "checkbox" ||
item.type === "radio" ||
item.type === "seq"
? this.vxeTableChechboxRadio(item, index)
: item.type === "expand"
? this.vxeTableExpand(item, index)
: this.vxeTable(item, index);
})}
</vxe-table>
</div>
{this.pager ? (
// 开启分页
<vxe-pager
perfect
size="mini"
current-page={this.currentPage}
page-size={this.pageSize}
page-sizes={this.pageSizes}
total={this.total}
auto-hidden={this.autoHidden}
layouts={[
"PrevPage",
"JumpNumber",
"NextPage",
"FullJump",
"Sizes",
"Total",
]}
onPage-change={(event) => this.pageChange(event)}
/>
) : (
""
)}
</div>
);
},
methods: {
getColumnByField(field) {
if(!field){
return
}
this.$refs.xTable.hideColumn(this.$refs.xTable.getColumnByField(field));
},
cellClick(e) {
this.$emit("cellClick", e);
},
onCellDblclick(e) {
this.$emit("cellDblclick", e);
},
getTableColumn() {
return this.$refs.xTable.getTableColumn();
},
getColumns() {
return this.$refs.xTable.getColumns();
},
refreshColumn() {
this.$refs.xTable.refreshColumn();
},
recalculate(is = true) {
this.$refs.xTable.recalculate(is);
this.refreshColumn();
},
reloadData(data) {
if (!Array.isArray(data)) data = [];
this.$refs.xTable.reloadData(data);
},
updateData(data) {
if (!Array.isArray(data)) data = [];
this.$refs.xTable.updateData(data);
},
loadData(data) {
if (!Array.isArray(data)) data = [];
this.$refs.xTable.loadData(data);
},
pageChange(e) {
this.$emit("tableChange", e);
},
sortChangeEvent(e) {
this.$emit("sortChangeEvent", e);
},
itemField(row, field) {
let field_zd = field?.split(".");
if (Array.isArray(field_zd) && field_zd?.length > 1) {
if (!row[field_zd[0]]) {
return "";
}
return row[field_zd[0]][field_zd[1]];
}
return row[field];
},
checkbox(e) {
this.$emit("checkbox", e);
},
radioChange(e) {
this.$emit("radioChange", e);
},
vxeTableChechboxRadio(item, index) {
return (
<vxe-column
key={index}
type={item.type}
sortable={item.sortable} // 是否开启排序
title={item.title} // 列标题
width={item.width} //列宽度 px, % number | string
min-width={item.minWidth} //最小列宽度;会自动将剩余空间按比例分配 number | string px, %
fixed={item.fixed} // 将列固定在左侧或者右侧(注意:固定列应该放在左右两侧的位置) left(固定左侧), right(固定右侧)
/>
);
},
vxeTableExpand(item, index) {
console.log(item);
return (
<vxe-column
key={index}
type={item.type}
sortable={item.sortable} // 是否开启排序
title={item.title} // 列标题
width={item.width} //列宽度 px, % number | string
min-width={item.minWidth} //最小列宽度;会自动将剩余空间按比例分配 number | string px, %
fixed={item.fixed} // 将列固定在左侧或者右侧(注意:固定列应该放在左右两侧的位置) left(固定左侧), right(固定右侧)
scopedSlots={{
// 显示插槽
default: (posrt) => {
let field = item.field ? this.filterField(item.field) : "";
return h(
"span",
this.$scopedSlots[field] //先判断有没有具名插槽,有就渲染
? this.$scopedSlots[field]({
row: posrt.row,
rowIndex: posrt.$rowIndex,
})
: this.$scopedSlots["default"] //判断有没有默认插槽,有就渲染默认插槽
? this.$scopedSlots["default"]({
row: posrt.row,
rowIndex: posrt.$rowIndex,
field: item.field,
text: this.itemField(posrt.row, item.field),
})
: this.itemField(posrt.row, item.field) //渲染正常的数据
);
},
content: (posrt) => {
// row 一行的数据
// rowIndex 索引
return h(
"div",
this.$scopedSlots["content"] //判断有没有默认插槽,有就渲染默认插槽
? this.$scopedSlots["content"]({
row: posrt.row,
rowIndex: posrt.$rowIndex,
field: item.field,
text: this.itemField(posrt.row, item.field),
})
: this.itemField(posrt.row, item.field) //渲染正常的数据
);
},
}}
/>
);
},
vxeTable(item, index) {
return (
<vxe-column
key={index}
title={item.title} // 列标题
field={item.field} //列字段名
width={item.width} //列宽度 px, % number | string
min-width={item.minWidth} //最小列宽度;会自动将剩余空间按比例分配 number | string px, %
fixed={item.fixed} // 将列固定在左侧或者右侧(注意:固定列应该放在左右两侧的位置) left(固定左侧), right(固定右侧)
sortable={item.sortable} // 是否开启排序
sort-type={item.sortType} //排序的字段类型,比如字符串转数值等 auto, number, string
align={item.align ? item.align : this.align} //left(左对齐), center(居中对齐), right(右对齐)
scopedSlots={{
default: (posrt) => {
// row 一行的数据
// rowIndex 索引
// console.log(this.$scopedSlots);
// console.log(item.field);
let field = item.field ? this.filterField(item.field) : "";
let text = this.itemField(posrt.row, item.field);
// console.log(this.$scopedSlots[field], field);
return h(
"span",
this.$scopedSlots[field] //先判断有没有具名插槽,有就渲染
? this.$scopedSlots[field]({
row: posrt.row,
rowIndex: posrt.$rowIndex,
field: item.field,
text,
})
: this.$scopedSlots["default"] //判断有没有默认插槽,有就渲染默认插槽
? this.$scopedSlots["default"]({
row: posrt.row,
rowIndex: posrt.$rowIndex,
field: item.field,
text,
})
: text //渲染正常的数据
);
},
}}
/>
);
},
filterField(field) {
let field_zd = field?.split(".");
return field_zd?.length === 1 ? field_zd[0] : field_zd[1];
},
},
};
</script>