实现 Table 的增加和删除,不依赖后端数据回显

需求

  • 删除前

  • 删除后

分析

首先写一个 Table

javascript 复制代码
 <a-card style="width:100%">
     <template#extra>
         <a-button type="text" @click="addSelectItem" style="margin-right: 5px">
             添加
         </a-button>
         <a-button type="text" @click="packUpOrCloseVisible = !packUpOrCloseVisible">{{
packUpOrCloseVisible ? "收起" : "展开" }}
         </a-button>
     </template>
     <a-table v-if="packUpOrCloseVisible" :data="editForm.partyMemberList" :bordered="false"
         :pagination="false" style="width:100%">
         <template #columns>
             <a-table-column title="姓名" align="center">
                 <template #cell="{ record }">
                     <el-input v-model="record.memberName" placeholder="请选择人员" />
                 </template>
             </a-table-column>
             <a-table-column title="电话" align="center">
                 <template #cell="{ record }">
                     {{ record.fundsReportId || '暂无' }}
                 </template>
             </a-table-column>
             <a-table-column title="操作" align="center">
                 <template #cell="{ record, rowIndex }">
                     <a-button type="text" @click="deleteSelectItem(record, rowIndex)"
                         status="danger">
                         <template #icon><icon-delete /></template>
                         删除
                     </a-button>
                 </template>
             </a-table-column>
         </template>
     </a-table>
 </a-card>

我们可以对数组进行操作,首先是增加操作

javascript 复制代码
function addSelectItem() {
    editForm.value.partyMemberList.push({
        memberName: '',
        phone: ''
    })
}

删除指定元素

javascript 复制代码
// 参会人员删除
function deleteSelectItem(data, index) {
    console.log(data, index)
    editForm.value.partyMemberList.splice(index, 1)
}
相关推荐
stella·17 小时前
后端二进制文件,现代前端如何下载
前端·ajax·状态模式·axios·request·buffer·download
奋斗猿17 小时前
Less vs Scss 全解析:从语法到实战的前端样式预处理器指南
前端
Web - Anonymous17 小时前
使用Vue3 + Elementplus + Day.js 实现日期选择器(包括日、周、月、年、自定义) - 附完整示例
前端·javascript·vue.js
冴羽17 小时前
2025 年 HTML 年度调查报告亮点速览!
前端·javascript·html
张元清17 小时前
浏览器硬导航优化:提升用户体验的关键
前端·javascript·面试
程序员爱钓鱼17 小时前
Node.js 编程实战:博客系统 —— 用户注册登录与文章管理
前端·后端·node.js
xkxnq18 小时前
第二阶段:Vue 组件化开发(第 23天)
前端·javascript·vue.js
zcz160712782118 小时前
nmcli常见操作
前端·chrome
晴栀ay18 小时前
JS的超集——TypeScript
前端·react.js·typescript
EndingCoder18 小时前
高级类型:联合类型和类型别名
linux·服务器·前端·ubuntu·typescript