首页 >> js开发 >> jsel-table树形表格表单验证(列表生成序号)js大全
jsel-table树形表格表单验证(列表生成序号)js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
树形表格表单验证预览树形列表生成序号树形列表生成序号首先需要生成一个序号用来确定表单验证的目标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: "2021-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: "2021-05-04",
name: "王小虎2",
address: "上海市普陀区金沙江路 1517 弄",
children: []
},
]
setIndex(rows)
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: "2021-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: "2021-05-04",
name: "王小虎2",
address: "上海市普陀区金沙江路 1517 弄",
children: []
},
]
setIndex(rows)
表单校验表单校验要想实现对表格的表单验证,需要用form-item将整个表格包裹,然后在以子集的方式将每一列用form-item包裹。
prop="name"
label="姓名"
sortable
width="180">
{{`rows${getPathByKey(row.customIndex,"customIndex",form.rows)}.name`}}
:prop="`${row.customIndex!=='tempIndex'?`rows${getPathByKey(row.customIndex,'customIndex',form.rows)}.name`:''}`">
prop="name"
label="姓名"
sortable
width="180">
{{`rows${getPathByKey(row.customIndex,"customIndex",form.rows)}.name`}}
:prop="`${row.customIndex!=='tempIndex'?`rows${getPathByKey(row.customIndex,'customIndex',form.rows)}.name`:''}`">
实现方式,表单校验本质是对于目标数据的路径查找,当el-form-item 上的prop匹配不到正确的目标是的时候就不能正常触发校验
el-form-item
因此,需要记录每一个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;
}
},
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参考demo参考
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: "2021-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: "2021-05-04",
name: "王小虎2",
address: "上海市普陀区金沙江路 1517 弄",
children: []
},
]
setIndex(rows)
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: "2021-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: "2021-05-04",
name: "王小虎2",
address: "上海市普陀区金沙江路 1517 弄",
children: []
},
]
setIndex(rows)
表单校验表单校验要想实现对表格的表单验证,需要用form-item将整个表格包裹,然后在以子集的方式将每一列用form-item包裹。
prop="name"
label="姓名"
sortable
width="180">
{{`rows${getPathByKey(row.customIndex,"customIndex",form.rows)}.name`}}
:prop="`${row.customIndex!=='tempIndex'?`rows${getPathByKey(row.customIndex,'customIndex',form.rows)}.name`:''}`">
prop="name"
label="姓名"
sortable
width="180">
{{`rows${getPathByKey(row.customIndex,"customIndex",form.rows)}.name`}}
:prop="`${row.customIndex!=='tempIndex'?`rows${getPathByKey(row.customIndex,'customIndex',form.rows)}.name`:''}`">
el-form-item
因此,需要记录每一个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;
}
},
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参考demo参考
相关文章:
- jsAngular利用HTTP POST下载流文件的步骤记录js大全
- js解决父组件将子组件作为弹窗调用只执行一次created的问题js大全
- jsvue+element-ui JYAdmin后台管理系统模板解析js大全
- jsJS的时间格式化和时间戳转换函数示例详解js大全
- jsJS实现audio音频剪裁剪切复制播放与上传(步骤详解)js大全
- js代码处理JavaScript值为undefined的7个小技巧
- jsVue组件跨层级获取组件操作js大全
- js解决VUE mounted 钩子函数执行时 img 未加载导致页面布局的问题js大全
- js解决vue页面渲染但dom没渲染的操作js大全
- js谈一谈vue请求数据放在created好还是mounted里好js大全