elementui 樹形數(shù)據(jù)遞歸table_第1頁
elementui 樹形數(shù)據(jù)遞歸table_第2頁
elementui 樹形數(shù)據(jù)遞歸table_第3頁
elementui 樹形數(shù)據(jù)遞歸table_第4頁
elementui 樹形數(shù)據(jù)遞歸table_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

elementui樹形數(shù)據(jù)遞歸table在Vue.js的開發(fā)中,使用ElementUI庫來創(chuàng)建交互性的組件是一種常見做法。ElementUI提供了豐富的組件,其中包括了樹形結(jié)構(gòu)的展示組件,并且還提供了遞歸表格(RecursiveTable)的實(shí)現(xiàn)方式。本文將介紹如何使用ElementUI來創(chuàng)建樹形數(shù)據(jù)的遞歸表格。

首先,我們需要準(zhǔn)備一個有層級關(guān)系的數(shù)據(jù)。這個數(shù)據(jù)可以是一個數(shù)組,每個元素代表一條數(shù)據(jù),并且每個元素都包含一個children屬性,用來表示當(dāng)前數(shù)據(jù)的子節(jié)點(diǎn)。以下是一個示例數(shù)據(jù):

```javascript

consttreeData=[

{

id:1,

name:'節(jié)點(diǎn)1',

children:[

{

id:11,

name:'節(jié)點(diǎn)1-1',

children:[]

},

{

id:12,

name:'節(jié)點(diǎn)1-2',

children:[

{

id:121,

name:'節(jié)點(diǎn)1-2-1',

children:[]

},

{

id:122,

name:'節(jié)點(diǎn)1-2-2',

children:[]

}

]

}

]

},

{

id:2,

name:'節(jié)點(diǎn)2',

children:[]

}

];

```

接下來,我們需要在Vue組件中使用ElementUI的`<el-table>`和`<template>`來創(chuàng)建遞歸表格。具體實(shí)現(xiàn)如下:

```html

<template>

<div>

<el-table:data="tableData"border>

<el-table-columntype="index"></el-table-column>

<el-table-columnprop="name"label="名稱"></el-table-column>

<el-table-columnlabel="操作">

<templateslot-scope="scope">

<el-buttontype="primary"size="mini">編輯</el-button>

<el-buttontype="danger"size="mini">刪除</el-button>

</template>

</el-table-column>

<templatev-for="colindynamicColumns">

<el-table-column:label="col.label":prop="p"></el-table-column>

</template>

</el-table>

</div>

</template>

<script>

exportdefault{

data(){

return{

tableData:[],

dynamicColumns:[]

};

},

mounted(){

this.tableData=this.normalizeData(treeData);

this.dynamicColumns=this.getDynamicColumns(treeData);

},

methods:{

normalizeData(data){

letresult=[];

data.forEach(item=>{

result.push(item);

if(item.children.length>0){

result=result.concat(this.normalizeData(item.children));

}

});

returnresult;

},

getDynamicColumns(data){

letcolumns=[];

data.forEach(item=>{

if(item.children.length>0){

columns.push({

label:'子級名稱',

prop:`name${item.id}`

});

columns=columns.concat(this.getDynamicColumns(item.children));

}

});

returncolumns;

}

}

};

</script>

```

在上面的代碼中,我們使用了Vue的計(jì)算屬性`tableData`和`dynamicColumns`來獲取處理后的表格數(shù)據(jù)和動態(tài)列數(shù)據(jù)。在`mounted`鉤子函數(shù)中,我們分別調(diào)用了`normalizeData`和`getDynamicColumns`方法來處理原始數(shù)據(jù),使其適應(yīng)于ElementUI的表格的要求。

`normalizeData`方法使用遞歸的方式將層級關(guān)系的數(shù)據(jù)展開成一維數(shù)組。`getDynamicColumns`方法同樣使用遞歸的方式獲取所有層級的動態(tài)列數(shù)據(jù)。

在模板中,我們使用了`<el-table>`來創(chuàng)建表格,并通過`:data`將處理后的數(shù)據(jù)傳遞給表格組件。然后,我們使用`<el-table-column>`來創(chuàng)建表格的列。在示例中,我們創(chuàng)建了兩種類型的列,一種是普通的列,一種是動態(tài)列。

普通的列使用`prop`屬性來指定要顯示的數(shù)據(jù)字段。動態(tài)列使用`v-for`指令來遍歷動態(tài)列數(shù)據(jù),并使用`:label`和`:prop`來指定列的標(biāo)題和數(shù)據(jù)字段。

除了基礎(chǔ)的表格操作,我們還可以在`<template>`中添加其他的HTML內(nèi)容,例如上述代碼中的操作按鈕。通過使用`slot-

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論