本人mes项目用的vue+ant design vue做的,请问要在后台mes中实现下图这种表格,表格可编辑,有数据的话会渲染到表格中,除了用a-row和a-col来配合,一点点拼凑出来表格,还有什么方法或者插件来达成需要的效果么?
data() {
this.components = {
header: {
cell: (h, props, children) => {
const { key, ...restProps } = props
// 此处的this.columns 是定义的table的表头属性变量
const col = this.columns.find((col) => {
const k = col.dataIndex || col.key
return k === key
})
if (!col || !col.width) {
return h('th', { ...restProps }, [...children])
}
const dragProps = {
key: col.dataIndex || col.key,
class: 'table-draggable-handle',
attrs: {
w: 10,
x: col.width,
z: 1,
axis: 'x',
draggable: true,
resizable: false,
},
on: {
dragging: (x, y) => {
col.width = Math.max(x, 1)
},
},
}
const drag = h('vue-draggable-resizable', { ...dragProps })
return h('th', { ...restProps, class: 'resize-table-th' }, [...children, drag])
},
},
}
return {
dataSource:[],
defColumns:[{
title: '序号',
dataIndex: 'rowIndex',
key: 'rowIndex',
width: 80,
align: 'center',
customRender: function (text, r, index) {
return parseInt(index) + 1
},
className: 'columnsColor',
},{
title: '价格状态',
dataIndex: 'customerName1',
className: 'columnsColor',
scopedSlots: { customRender: 'customerName1' },
width: 200,
}]
},
我可以为您提供一个解决方案,您可以使用vue-antd-table-editable
插件来实现可编辑的表格。
步骤如下:
npm install vue-antd-table-editable --save
import Vue from 'vue';
import VueAntdTableEditable from 'vue-antd-table-editable';
Vue.use(VueAntdTableEditable);
<template>
<div>
<a-table :columns="columns" :data="data" :components="components" rowKey="id" bordered></a-table>
<button @click="addRow">添加行</button>
<button @click="addColumn">添加列</button>
</div>
</template>
<script>
export default {
data() {
return {
columns: [
{ title: '列1', dataIndex: 'column1', key: 'column1' },
{ title: '列2', dataIndex: 'column2', key: 'column2' },
// 可以根据需要添加更多列
],
data: [
{ id: 1, column1: '内容1', column2: '内容2' },
{ id: 2, column1: '内容3', column2: '内容4' },
// 可以根据需要添加更多行
],
};
},
methods: {
addRow() {
const newId = this.data.length + 1;
this.data.push({ id: newId, column1: '', column2: '' });
},
addColumn() {
const newColumnKey = `column${this.columns.length + 1}`;
this.columns.push({ title: `列${this.columns.length + 1}`, dataIndex: newColumnKey, key: newColumnKey });
this.data.forEach((row) => {
row[newColumnKey] = '';
});
},
},
};
</script>
这样,您就可以在您的Vue+Ant Design Vue项目中实现可编辑的表格,可以渲染数据、添加新的行和列,以及编辑已有的表格内容。
请注意,这只是一个基本示例,您可以根据您的具体需求对表格进行更多的自定义和样式调整。