使用vue+antdv制作一个带行编辑的表格
时间: 2024-01-14 16:05:02 浏览: 47
好的,下面是一个简单的示例代码,你可以根据自己的需求进行修改:
```html
<template>
<div>
<a-table :columns="columns" :dataSource="dataSource" :pagination="false">
<template #name="text, record">
<a-input v-model="record.name" />
</template>
<template #age="text, record">
<a-input-number v-model="record.age" :min="0" />
</template>
<template #address="text, record">
<a-input v-model="record.address" />
</template>
</a-table>
</div>
</template>
<script>
export default {
data() {
return {
columns: [
{
title: '姓名',
dataIndex: 'name',
key: 'name',
slots: { customRender: 'name' }
},
{
title: '年龄',
dataIndex: 'age',
key: 'age',
slots: { customRender: 'age' }
},
{
title: '地址',
dataIndex: 'address',
key: 'address',
slots: { customRender: 'address' }
}
],
dataSource: [
{
name: '张三',
age: 18,
address: '北京市朝阳区'
},
{
name: '李四',
age: 20,
address: '上海市浦东新区'
}
]
};
}
};
</script>
```
在上面的代码中,我们使用了 Ant Design Vue 的 `a-table` 和 `a-input`、`a-input-number` 组件,通过 `v-model` 指令实现了行编辑的功能。
注意,为了让年龄字段实现数字类型的双向绑定,我们使用了 `a-input-number` 组件,并设置了 `min` 属性。另外,为了让表格自适应宽度,我们设置了 `:pagination="false"`,即不显示分页。