黑客入门技能
2026/6/22 13:53:41
背景说明:为什么要“选中行”?
在后台管理系统中,经常有这种需求点击【批量分配】把选中的患者 ID 传给后端批量删除患者、批量出院
用 el-table 的 多选功能 + selection-change 事件
代码如下(示例):
<el-table:data="tableData"@selection-change="handleSelectionChange"><!--多选框列--><el-table-column type="selection"width="55"/><el-table-column prop="name"label="姓名"/><el-table-column prop="age"label="年龄"/></el-table><el-table-column type="selection"/>>作用: 👉 自动在最前面生成 ☑️ 勾选框conststate=reactive({sels:[]as any[]// 存放选中的行})该处使用的url网络请求的数据。
@selection-change="handleSelectionChange"<el-table:data="tableData"@selection-change="handleSelectionChange">consthandleSelectionChange=(rows:any[])=>{state.sels=rows}这一步在干嘛?
每次:
勾选
取消
全选
都会触发这个函数。
并且:
rows = 当前所有选中的行数据
标准写法(安全版)
constids=state.sels.map(r=>r?.id).filter(Boolean)map → 取 id
filter → 删除空值
用 selection 拿行
用数组存
用 map 取 id
调接口