這篇文章將為大家詳細(xì)講解有關(guān)怎么在vue中使用element UI實(shí)現(xiàn)一個(gè)樹形表格,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)建站主要從事成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)凌源,10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18982081108
一:在component文件夾下新建如下treeTable文件夾,里面有2個(gè)文件:
eval.js:將數(shù)據(jù)轉(zhuǎn)換成樹形數(shù)據(jù)
"use strict"; import Vue from "vue"; export default function treeToArray( data, expandAll, parent = null, level = null ) { let tmp = []; Array.from(data).forEach(function(record) { if (record._expanded === undefined) { Vue.set(record, "_expanded", expandAll); } let _level = 1; if (level !== undefined && level !== null) { _level = level + 1; } Vue.set(record, "_level", _level); // 如果有父元素 if (parent) { Vue.set(record, "parent", parent); } tmp.push(record); if (record.child && record.child.length > 0) { const child = treeToArray(record.child, expandAll, record, _level); tmp = tmp.concat(child); } }); return tmp; }
index.vue:樹形表格組件
<template> <el-table ref="multipleTable" :data="formatData" :row- v-bind="$attrs"> <!-- @header-click="chooseall" --> <el-table-column :render-header="renderHeader" width="50" align="center"> <template slot-scope="scope"> <el-checkbox v-model="scope.row.checks" @change="toselect(scope.row)"></el-checkbox> </template> </el-table-column> <el-table-column v-if="columns.length===0" width="150"> <template slot-scope="scope"> <span v-for="space in scope.row._level" :key="space" class="ms-tree-space"/> <span v-if="iconShow(0,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)"> <i v-if="!scope.row._expanded" class="el-icon-plus"/> <i v-else class="el-icon-minus"/> </span> {{ scope.$index }} </template> </el-table-column> <el-table-column v-for="(column, index) in columns" v-else :key="column.value" :label="column.text" :width="column.width"> <template slot-scope="scope"> <!-- Todo --> <!-- eslint-disable-next-line vue/no-confusing-v-for-v-if --> <span v-for="space in scope.row._level" v-if="index === 0" :key="space" class="ms-tree-space"/> <span v-if="iconShow(index,scope.row)" class="tree-ctrl" @click="toggleExpanded(scope.$index)"> <i v-if="!scope.row._expanded" class="el-icon-plus"/> <i v-else class="el-icon-minus"/> </span> {{ scope.row[column.value] }} </template> </el-table-column> <slot/> </el-table> </template> <script> /** Auth: Lei.j1ang Created: 2018/1/19-13:59 */ import treeToArray from "./eval"; export default { name: "TreeTable", data() { return { chooseson: true, //全選 key: true //單個(gè)點(diǎn)擊直到全部選中 }; }, props: { /* eslint-disable */ data: { type: [Array, Object], required: true }, columns: { type: Array, default: () => [] }, evalFunc: Function, evalArgs: Array, expandAll: { type: Boolean, default: false } }, computed: { // 格式化數(shù)據(jù)源 formatData: function() { let tmp; if (!Array.isArray(this.data)) { tmp = [this.data]; } else { tmp = this.data; } const func = this.evalFunc || treeToArray; const args = this.evalArgs ? [tmp, this.expandAll].concat(this.evalArgs) : [tmp, this.expandAll]; return func.apply(null, args); } }, methods: { showRow: function(row) { const show = row.row.parent ? row.row.parent._expanded && row.row.parent._show : true; row.row._show = show; return show ? "animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;" : "display:none;"; }, // 切換下級(jí)是否展開 toggleExpanded: function(trIndex) { const record = this.formatData[trIndex]; record._expanded = !record._expanded; }, // 圖標(biāo)顯示 iconShow(index, record) { return index === 0 && record.child && record.child.length > 0; }, //設(shè)置表頭全選 renderHeader(h, data) { return h("span", [ h("input", { attrs: { id: "chooseall", type: "checkbox", style: "border: 1px solid #dcdfe6;border-radius: 2px;box-sizing: border-box;width: 14px;height: 14px;background-color: #fff;" } }) ]); }, //功能函數(shù):選中部分子集 setchildtobeselect(arr, key) { arr.forEach((v, i) => { v.checks = key; // v._expanded = key;//選中后展開子項(xiàng) if (v.child) { this.setchildtobeselect(v.child, v.checks); } }); }, //是否所有的都被選中 isallchecked(arr) { arr.forEach((v, i) => { if (!v.checks) { this.key = false; } if (v.child) { this.isallchecked(v.child); } }); }, //設(shè)置父級(jí)為 未選中狀態(tài)(父級(jí)的父級(jí)沒改變-有bug) setparentfalse(arr, id, level) { arr.forEach((v, i) => { if (v._level == level - 1 && v.child) { v.child.forEach((val, ind) => { if (val.id == id) { v.checks = false; return false; //終止此次循環(huán),減少循環(huán)次數(shù) } }); } if (v.child) { this.setparentfalse(v.child, id, level); } }); }, //設(shè)置父級(jí)為 選中狀態(tài) setparenttrue(arr, id, level) { arr.forEach((v, i) => { if (v._level == level - 1 && v.child) { let key = true; let sameidkey = false; v.child.forEach((val, ind) => { if (val.id == id) { //確保當(dāng)前點(diǎn)擊的在該父級(jí)內(nèi) sameidkey = true; } if (!val.checks) { key = false; } }); if (key && sameidkey) { v.checks = true; } } if (v.child) { this.setparentfalse(v.child, id, level); } }); }, //某個(gè)復(fù)選框被點(diǎn)擊時(shí) toselect(row) { console.log(row); // row._expanded = row.checks;//選中后是否展開 //1、若有子集先讓子選中 if (row.child) { this.setchildtobeselect(row.child, row.checks); } //2、然后判斷是否全選中 this.key = true; //重置為true,防止上次已經(jīng)是false的狀態(tài) this.isallchecked(this.formatData); //3、設(shè)置多選框的狀態(tài) if (!row.checks) { this.setparentfalse(this.formatData, row.id, row._level); //設(shè)置父級(jí)選中的狀態(tài)為false document.getElementById("chooseall").checked = false; //設(shè)置全選框的狀態(tài) } else { this.setparenttrue(this.formatData, row.id, row._level); //設(shè)置父級(jí)選中的狀態(tài)為true } if (this.key) { document.getElementById("chooseall").checked = true; //設(shè)置全選框的狀態(tài) } } }, mounted() { this.$nextTick(() => { var that = this; const all = document.getElementById("chooseall"); all.onchange = function(e) { console.log(all.checked); if (all.checked == true) { that.setchildtobeselect(that.formatData, true); } else { that.setchildtobeselect(that.formatData, false); } }; }); } }; </script> <style rel="stylesheet/css"> @keyframes treeTableShow { from { opacity: 0; } to { opacity: 1; } } @-webkit-keyframes treeTableShow { from { opacity: 0; } to { opacity: 1; } } </style> <style scoped> .ms-tree-space { position: relative; top: 1px; display: inline-block; font-style: normal; font-weight: 400; line-height: 1; width: 18px; height: 14px; } .ms-tree-space::before { content: ""; } .processContainer { width: 100%; height: 100%; } table td { line-height: 26px; } .tree-ctrl { position: relative; cursor: pointer; color: #2196f3; margin-left: -18px; } </style>
二:在需要的地方引入該組件:
例如:在component文件夾下新建a.vue:
<tree-table :data="data" :columns="columns" border/>
import treeTable from "./TreeTable"; components: { treeTable }, data() { return { columns: [ { text: "事件", value: "event", width: 200 }, { text: "ID", value: "id" } ], data: [ { id: 0, event: "事件1", timeLine: 50, comment: "無" }, { id: 1, event: "事件1", timeLine: 100, comment: "無", children: [ { id: 2, event: "事件2", timeLine: 10, comment: "無" }, { id: 3, event: "事件3", timeLine: 90, comment: "無", children: [ { id: 4, event: "事件4", timeLine: 5, comment: "無" }, { id: 5, event: "事件5", timeLine: 10, comment: "無" }, { id: 6, event: "事件6", timeLine: 75, comment: "無", children: [ { id: 7, event: "事件7", timeLine: 50, comment: "無", children: [ { id: 71, event: "事件71", timeLine: 25, comment: "xx" }, { id: 72, event: "事件72", timeLine: 5, comment: "xx" }, { id: 73, event: "事件73", timeLine: 20, comment: "xx" } ] }, { id: 8, event: "事件8", timeLine: 25, comment: "無" } ] } ] } ] } ] }; },
關(guān)于怎么在vue中使用element UI實(shí)現(xiàn)一個(gè)樹形表格就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
文章題目:怎么在vue中使用elementUI實(shí)現(xiàn)一個(gè)樹形表格
分享URL:http://chinadenli.net/article22/ppcicc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、用戶體驗(yàn)、品牌網(wǎng)站設(shè)計(jì)、App設(shè)計(jì)、響應(yīng)式網(wǎng)站、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)