树形表格表单验证预览
树形列表生成序号
首先需要生成一个序号用来确定表单验证的目标row,通过广度优先遍历,以1,1.1,1.1.1的规则对树形列表生成确定唯一值的索引,因为列表自身可以做CURD,因此需要每次列表的item增加或减少时重新调用生成索引的方法。
setIndex = (data) => { let queue = [...data]; let loop = 0; while (queue.length > 0) { loop++ [...queue].forEach((child, i) => { queue.shift() if (loop == 1) { child.customIndex = i + 1 + ""; child.currentIndex = i; } if (child.children && child.children.length > 0) { child.dataType = 1 for (let ci = 0; ci < child.children.length; ci++) { child.children[ci].currentIndex = ci child.children[ci].customIndex = child.customIndex + "." + (ci + 1) } queue.push(...child.children) } else { child.dataType = 2 } }) } } const rows = [ { id: "1", date: "2016-05-02", name: "王小虎1", address: "上海市普陀区金沙江路 1518 弄", children: [ { name: "233", customIndex: "1.1", children: [{name: "9"}] }, { name: "7771", customIndex: "1.2", children: [ { name: "9" }, { name: "9", }] } ] }, { id: "2", date: "2016-05-04", name: "王小虎2", address: "上海市普陀区金沙江路 1517 弄", children: [] }, ] setIndex(rows)
表单校验
要想实现对表格的表单验证,需要用form-item将整个表格包裹,然后在以子集的方式将每一列用form-item包裹。
<el-form ref="form" :model="form" label-width="80px" :rules="rules"> <el-form-item label-width="0" prop="rows"> <el-table> </el-table> </el-form-item> </el-form>
<el-table-column prop="name" label="姓名" sortable width="180"> <template v-slot="{$index, row}"> {{`rows${getPathByKey(row.customIndex,"customIndex",form.rows)}.name`}} <el-form-item label-width="0" :rules="rules.name" :prop="`${row.customIndex!=='tempIndex'"> <el-input v-model="row.name"></el-input> </el-form-item> </template> </el-table-column>
实现方式,表单校验本质是对于目标数据的路径查找,当el-form-item 上的prop匹配不到正确的目标是的时候就不能正常触发校验
因此,需要记录每一个row上面的属性路径,即实现记录每一行中属性(name,address)路径的方法。
getPathByKey = (value, key, arr) => { let temppath = []; let realPath = "" try { function getNodePath(node) { temppath.push(node.currentIndex); //找到符合条件的节点,通过throw终止掉递归 if (node[key] === value) { temppath.forEach((v, i) => { if (i == 0) { realPath += "." + v } else { realPath += `.children.${v}` } }) // temppath = temppath.join(",") throw ("GOT IT!"); // return; } if (node.children && node.children.length > 0) { for (var i = 0; i < node.children.length; i++) { getNodePath(node.children[i]); } //当前节点的子节点遍历完依旧没找到,则删除路径中的该节点 temppath.pop(); } else { //找到叶子节点时,删除路径当中的该叶子节点 temppath.pop(); } } for (let i = 0; i < arr.length; i++) { getNodePath(arr[i]); } } catch (e) { return realPath; } },
将每一列需要验证的item,路径查找好之后,form就可以具体监控到所有的表格输入,并触发正确的验证了,如图:
demo参考
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- 《年度十大发烧唱片精选 唱片榜获奖作品 2CD》[WAV/分轨][1GB]
- 归龙潮三枚天灯碎片位置在哪 笼律鸟任务天灯图文位置
- 归龙潮新手开荒阵容怎么搭配 新手前期阵容养成攻略
- 归龙潮中浦城区福鼠位置在哪 中浦城区福鼠收集位置大全
- MatteoPasqualini-JohannSebastianBachItalienischesKonzertFranzsischeOuvertureVierDue
- 【雨果唱片】陈百强《梦里人LPCD45》2012[WAV+CUE]
- 王菲.1994-《迷路》美卡引进版[WAV+CUE]
- 老外遇到八戒裸体Bug 网友:我需要用它对付蜘蛛精
- 《黑神话》超强越化补丁:游戏里有文字的地都改了
- 玩家吐槽《黑神话:悟空》唐僧相貌:一脸衰样 没半点气质
- 群星.1994-烧得厉害VOL.2我不是一个人拙滚石】【WAV+CUE】
- 群星.1994-烧得厉害VOL.3风笑痴【滚石】【WAV+CUE】
- 群星.1995-烧得厉害VOL.4两个世界【滚石】【WAV+CUE】
- 归龙潮中浦城区宝箱位置收集大全 中浦区宝箱解密攻略
- 黑神话悟空上品幽灯鬼精魄获取方法一览|上品幽灯鬼精魄收集攻略