ant design vue table是输入框形式 然后增加行 减去行在vue3中
时间: 2025-06-21 07:29:34 浏览: 13
### 实现带有输入框的可增减行列表格
在 Vue 3 中使用 Ant Design Vue 创建一个支持动态增加和减少行的表格组件,可以通过以下方式实现:
#### 安装依赖库
首先确保安装了必要的依赖项:
```bash
npm install ant-design-vue@next vue@next
```
#### 初始化项目结构
创建一个新的 Vue 组件 `DynamicTable.vue` 来承载此功能。
#### 编写 DynamicTable.vue 文件
```vue
<template>
<a-table :columns="columns" :data-source="dataSource">
<!-- 自定义列模板 -->
<template #bodyCell="{ column, record, index }">
<div v-if="column.dataIndex === 'name' || column.dataIndex === 'age'" style="display: flex;">
<a-input v-model:value="record[column.dataIndex]" />
</div>
<div v-else-if="column.key === 'operation'">
<span>
<a-button type="link" @click="remove(index)">Delete</a-button>
</span>
</div>
</template>
<template #footer>
<a-button type="primary" @click="addRow">Add Row</a-button>
</template>
</a-table>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue';
import { Input as AInput, Table as ATable, Button as AButton } from 'ant-design-vue';
const columns = [
{
title: 'Name',
dataIndex: 'name'
},
{
title: 'Age',
dataIndex: 'age'
},
{
key: 'operation',
fixed: 'right',
width: 100,
align: "center",
title: 'Action'
}
];
let dataSource = ref([
{ name: '', age: '' }
]);
function addRow() {
const newRow = { name: '', age: '' };
dataSource.value.push(newRow);
}
function remove(index) {
dataSource.value.splice(index, 1);
}
</script>
```
上述代码实现了如下特性[^1]:
- 使用 `<a-table>` 和其插槽来定制每一行的内容。
- 表格中的每行有两个字段:“姓名” (name) 和 “年龄” (age),均通过 `<a-input>` 控件编辑。
- 提供了一个按钮用于移除当前行。
- 在表格底部提供了一个“添加新行”的按钮。
#### 关键点说明
- 动态绑定的数据源 (`dataSource`) 是响应式的数组对象集合。
- 利用了 Ant Design 的 slot API 对特定单元格进行了自定义渲染。
- 添加/删除操作直接修改了数据源,触发视图自动更新。
阅读全文
相关推荐















