使用table做成樹形結(jié)構(gòu)的table
更新時間:2024年07月25日 10:20:42 作者:愛跳舞的程序員
這篇文章主要介紹了使用table做成樹形結(jié)構(gòu)的table問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
table做成樹形結(jié)構(gòu)的table

<el-table
:default-expand-all="false"
:data="list"
style="width: 100%; margin-bottom: 20px"
row-key="listId"
border
:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
lazy
:load="load"
>
<el-table-column prop="bppjName" label="部件名稱"> </el-table-column>
<el-table-column prop="stkName" label="物資名稱"> </el-table-column>
<el-table-column prop="stkCode1" label="物資編碼"> </el-table-column>
<el-table-column prop="stkCode" label="型號規(guī)格"> </el-table-column>
<el-table-column prop="metric" label="單位" width="80" align="center"> </el-table-column>
<el-table-column prop="num" label="數(shù)量" width="80" align="center"> </el-table-column>
<el-table-column prop="note" label="備注"> </el-table-column>
</el-table> getList() {
// 一開始獲取數(shù)據(jù)列表
const obj = {
listId: this.listId,
};
getList(obj).then((res) => {
this.list = res.rows;
this.list.forEach((item) => {
// 必須要設(shè)置,不然沒有下級圖標(biāo)顯示
item.hasChildren = true;
item.children = null;
});
});
},
load(tree, treeNode, resolve) {
// 獲取下一個節(jié)點(diǎn)數(shù)據(jù) 手動添加上給當(dāng)前的節(jié)點(diǎn)
getUdmBppjDataList({ listId: tree.listId }).then((res) => {
const childList = res.rows;
childList.forEach((item) => {
// 必須要設(shè)置,不然沒有下級圖標(biāo)顯示
item.hasChildren = true,
item.children = null;
});
setTimeout(() => {
resolve(childList);
}, 500);
});
},table樹形結(jié)構(gòu),獲取一個節(jié)點(diǎn)的所有父節(jié)點(diǎn)
數(shù)據(jù)
let tree = [
{
id:1,
code: '1',
fsecid:0,
children: [
{
id:11,
fsecid:1,
code:'1.1',
children: [
{
id:111,
fsecid:11,
code: '1.1.1',
}
]
},
{
id:12,
fsecid:1,
code: '1.2',
}
]
},
{
code: '2',
id:2,
fsecid:0,
children: [
{
id:21,
fsecid:2,
code: '2.1',
}
]
}
]
let result=scheduleAlgorithm(tree,11,'fsecid','children')
console.log("result=====",result);
//結(jié)果 result===== [ 1, 11 ]
標(biāo)題函數(shù)實(shí)現(xiàn)
/**判斷葉子節(jié)點(diǎn)的所有父節(jié)點(diǎn)*/
function scheduleAlgorithm(
array,//樹形數(shù)據(jù)
value,//找到valueName屬性名 所等于這個值所有的父節(jié)點(diǎn),2,3參數(shù)是關(guān)聯(lián)的
valueName = "fsecid",//與上一節(jié)點(diǎn)相關(guān)聯(lián)的值的字段名,當(dāng)前節(jié)點(diǎn)的父節(jié)點(diǎn)id
childrenName = "children",//存放子節(jié)點(diǎn)數(shù)據(jù)的數(shù)組名稱
) {
if (!value || !Array.isArray(array)) return [];
const result = [];
let valid = false;
const seek = (array, value) => {
let parentValue = "";
const up = (array, value, lastValue) => {
array.forEach(v => {
const val = v[valueName];
const child = v[childrenName];
if (val === value) {
valid = true;
parentValue = lastValue;
return;
}
if (child && child.length) up(child, value, val);
});
};
up(array, value);
if (parentValue) {
result.unshift(parentValue);
seek(array, parentValue);
}
};
seek(array, value);
return valid ? [...result, value] : [];
}
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用vue-cli創(chuàng)建項(xiàng)目
這篇文章主要介紹了Vue使用vue-cli創(chuàng)建項(xiàng)目,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
vue+vue-router轉(zhuǎn)場動畫的實(shí)例代碼
今天小編就為大家分享一篇vue+vue-router轉(zhuǎn)場動畫的實(shí)例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue+element 模態(tài)框表格形式的可編輯表單實(shí)現(xiàn)
這篇文章主要介紹了vue+element 模態(tài)框表格形式的可編輯表單實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-06-06
使用vue-cli搭建SPA項(xiàng)目的詳細(xì)過程
vue-cli是vue.js的腳手架,用于自動生成vue.js+webpack的項(xiàng)目模板,本文通過實(shí)例代碼給大家介紹vue-cli搭建SPA項(xiàng)目的詳細(xì)過程,感興趣的朋友跟隨小編一起看看吧2022-09-09
Vue中實(shí)現(xiàn)動態(tài)右鍵菜單的示例代碼
在前端開發(fā)中,實(shí)現(xiàn)自定義右鍵菜單能夠?yàn)橛脩籼峁└喙δ苓x項(xiàng),本文就來介紹了Vue中實(shí)現(xiàn)動態(tài)右鍵菜單的示例代碼,感興趣的可以了解一下2024-11-11
npm?ERR!?code?E404在vscode安裝插件時報錯的兩種解決方案
這篇文章主要給大家介紹了關(guān)于npm?ERR!?code?E404在vscode安裝插件時報錯的兩種解決方案,關(guān)于這個問題,通常是由于插件名稱輸入錯誤、網(wǎng)絡(luò)問題或插件已被刪除引起的,文中將兩種解決方法都介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04

