实现 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)
}
相关推荐
.又是新的一天.2 分钟前
04-Fiddler详解+抓包定位问题
前端·测试工具·fiddler
克里斯蒂亚L5 分钟前
OpenLayers - 画全国轨道线路图
前端
GISer_Jing7 分钟前
小米前端面试
前端·面试·职场和发展
静西子9 分钟前
Vue标签页切换时的异步更新问题
前端·javascript·vue.js
时间的情敌9 分钟前
Vue 3.0 源码导读
前端·javascript·vue.js
自由日记11 分钟前
css属性使用手册
前端·css·html
mapbar_front30 分钟前
基层管理之“跪舔型”leader
前端·程序员
listhi5201 小时前
React Hooks 实现表单验证
前端·javascript·react.js
前端市界2 小时前
当20个并发请求拖垮你的应用:从TCP握手到HTTP/2的终极排错指南
前端
量子-Alex2 小时前
【大模型与智能体论文】REACT:协同语言模型中的推理与行动
前端·react.js·语言模型