el-table樹形表格表單驗證(列表生成序號)
樹形表格表單驗證預覽

樹形列表生成序號
首先需要生成一個序號用來確定表單驗證的目標row,通過廣度優(yōu)先遍歷,以1,1.1,1.1.1的規(guī)則對樹形列表生成確定唯一值的索引,因為列表自身可以做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: "上海市普陀區(qū)金沙江路 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: "上海市普陀區(qū)金沙江路 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'?`rows${getPathByKey(row.customIndex,'customIndex',form.rows)}.name`:''}`">
<el-input v-model="row.name"></el-input>
</el-form-item>
</template>
</el-table-column>
實現方式,表單校驗本質是對于目標數據的路徑查找,當el-form-item 上的prop匹配不到正確的目標是的時候就不能正常觸發(fā)校驗

因此,需要記錄每一個row上面的屬性路徑,即實現記錄每一行中屬性(name,address)路徑的方法。
getPathByKey = (value, key, arr) => {
let temppath = [];
let realPath = ""
try {
function getNodePath(node) {
temppath.push(node.currentIndex);
//找到符合條件的節(jié)點,通過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]);
}
//當前節(jié)點的子節(jié)點遍歷完依舊沒找到,則刪除路徑中的該節(jié)點
temppath.pop();
} else {
//找到葉子節(jié)點時,刪除路徑當中的該葉子節(jié)點
temppath.pop();
}
}
for (let i = 0; i < arr.length; i++) {
getNodePath(arr[i]);
}
} catch (e) {
return realPath;
}
},
將每一列需要驗證的item,路徑查找好之后,form就可以具體監(jiān)控到所有的表格輸入,并觸發(fā)正確的驗證了,如圖:

到此這篇關于el-table樹形表格表單驗證(列表生成序號)的文章就介紹到這了,更多相關el-table樹形表格表單驗證內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- Vue-cli4 配置 element-ui 按需引入操作
- Vue + Element-ui的下拉框el-select獲取額外參數詳解
- vue用elementui寫form表單時,在label里添加空格操作
- 快速解決Vue、element-ui的resetFields()方法重置表單無效的問題
- vue+ElementUI 關閉對話框清空驗證,清除form表單的操作
- vue+element獲取el-table某行的下標,根據下標操作數組對象方式
- vue el-table實現自定義表頭
- vue el-table實現行內編輯功能
- element的el-table中記錄滾動條位置的示例代碼
- VUE2.0+ElementUI2.0表格el-table實現表頭擴展el-tooltip
- vue修改Element的el-table樣式的4種方法
相關文章
vue el-date-picker 開始日期不能大于結束日期的實現代碼
這篇文章主要介紹了vue el-date-picker 開始日期不能大于結束日期的實現代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01

