国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

優雅的將ElementUI表格變身成樹形表格的方法步驟

2019-11-19 11:48:18
字體:
來源:轉載
供稿:網友

由于ElementUI目前還未開發樹形表格組件,也參閱了網絡上部分基于ElementUI表格封裝的開源樹形組件,如果想進行二次開發的話都不太理想,所以就萌生了自行開發樹形表格。

本示例提供開發思路,移除了多余的樣式,比較適合新手入門學習,如果應用于實際項目還請自行封裝。

目前還僅僅實現了視覺的樹結構的層級效果和控制結構的顯示隱藏,后續還會進行不斷的完善和優化,有必要的話會對組件進行二次封裝,有點在重復造論的感覺哈。

效果圖

完整代碼

頁面(tree-table.vue)

<template> <div>  TreeTable  <el-table :data="list" :row-style="tableRowStyle" border>   <el-table-column type="selection" width="55"></el-table-column>   <el-table-column prop="id" label="ID" width="180">    <template slot-scope="scope">     <span class="collapse"        :class="collapseClass(scope.row)"        :style="tableRowPaddingStyle(scope.row)"        @click="handleCollapseClick(scope.row)">     </span>     <span>{{ scope.row.id }}</span>    </template>   </el-table-column>   <el-table-column prop="name" label="NAME"></el-table-column>  </el-table> </div></template><script lang="ts">  import {Component, Vue} from 'vue-property-decorator'  // 引入兩個封裝好的工具方法  import { arrayToTree } from './utils/array.js'  import { ergodicTree } from './utils/tree.js'  @Component  export default class TreeTable extends Vue {    private list: object[] = [];    private tree: object[] = [];    created() {      // 準備一組含有父子級關系的一維數組方便示例測試      // 在實際項目應用中,理應通過后端接口返回      let _list = [        {          id: 'a',          pid: '',          name: '部門a',          children: []        },        {          id: 'a1',          pid: 'a',          name: '子部門a1',          children: []        },        {          id: 'a2',          pid: 'a',          name: '子部門a2',          children: []        },        {          id: 'a2-1',          pid: 'a2',          name: '子部門a2-1',          children: []        },        {          id: 'a2-2',          pid: 'a2',          name: '子部門a2-2',          children: []        },        {          id: 'a3',          pid: 'a',          name: '子部門a3',          children: []        },        {          id: 'a3-1',          pid: 'a3',          name: '子部門a3-1',          children: []        },        {          id: 'b',          pid: '',          name: '部門b',          children: []        },        {          id: 'b1',          pid: 'b',          name: '子部門b1',          children: []        },        {          id: 'c',          pid: '',          name: '部門c',          children: []        },      ];            // 將一維數組轉成樹形結構并存儲于tree變量      this.tree = arrayToTree(_list);            // 考慮到實際應用過程中接口返回的數據是無序的,所以我們對tree進行先序遍歷將節點一一插入到list變量      this.list = [];      ergodicTree(this.tree, (node: any) => {        this.list.push(node);                // 遍歷過程中并對每個節點掛載open和show屬性        // open:控制節點的打開和關閉        // show:控制節點的顯示和隱藏        this.$set(node, 'open', true);        this.$set(node, 'show', true)      })    }    // 控制行的顯示和隱藏    tableRowStyle(scope: any) {      return {        'display': scope.row.show ? '' : 'none'      }    }    // 通過每個節點的深度,設置行的縮進實現視覺上的層級效果    tableRowPaddingStyle(row: any) {      return {        'margin-left': `${(row._depth - 1) * 24}px`      }    }    // 控制展開按鈕的展開和關閉狀態    collapseClass(row: any) {      return {        'collapse--open': row.open == false && row.children && row.children.length > 0,        'collapse--close': row.open == true && row.children && row.children.length > 0      }    }    // 處理展開按鈕的點擊事件    handleCollapseClick(row: any) {      const _open = row.open;      // 通過節點訪問路徑控制節點的顯示隱藏,由于內存指針的關系list和tree的節點操作都會相互影響      ergodicTree(this.tree, (node: any) => {        node._idPath.forEach((pathId: any) => {          if (pathId == row.id) {            this.$set(node, 'show', !_open);            this.$set(node, 'open', !_open)          }        })      });      row.show = true;      row.open = !_open;    }  }</script><style lang="scss" scoped> .collapse {  display: inline-block;  width: 8px;  cursor: pointer;  margin-right: 8px; } .collapse--open:before {  content: '+'; } .collapse--close:before {  content: '-'; }</style>

工具方法

考慮數組轉樹和遍歷樹都是在實際項目中都是非常常用的,所以這邊對這兩個方法進行了封裝。

數組轉樹結構(./utils/array.ts)

export function arrayToTree(list: object[], props = {id: 'id', pid: 'pid', children: 'children'}) {      let tree: object[] = [];      let map: any = {};      let listLength = list.length;      for (let i = 0; i < listLength; i++) {        let node: any = list[i];        let nodeId: any = node[props.id];        map[nodeId] = node;      }      for (let i = 0; i < listLength; i++) {        let node: any = list[i];        let nodePid: any = node[props.pid];        let parentNode: any = map[nodePid];        if (parentNode) {          parentNode[props.children] = parentNode[props.children] || [];          parentNode[props.children].push(node)        } else {          tree.push(node)        }      }      return tree    }

遍歷樹結構(./utils/tree.ts)

結合實際項目應用,我們采用了先序遍歷法對樹進行遍歷,為了方便在業務代碼里的應用,在遍歷過程中會對每個節點掛載節點訪問路徑 _idPath 屬性和節點深度 _depth 屬性。

export function ergodicTree(tree: object[], callback: any = () => {}, props = {id: 'id', pid: 'pid', children: 'children'}) {      function _ergodicTree(tree: object[], parentIdPath?: any[], depth: number = 0) {        const treeLength = tree.length;        for (let i = 0; i < treeLength; i++) {          let node: any = tree[i];          const _idPath: any[] = parentIdPath ? [...parentIdPath, node[props.id]] : [node[props.id]];          const _depth: number = depth + 1;          node._idPath = _idPath;          node._depth = _depth;          callback(node);          if (node[props.children] && node[props.children] instanceof Array) {            _ergodicTree(node[props.children], _idPath, _depth)          }        }      }      _ergodicTree(tree);      return tree;    }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青州市| 临沧市| 长子县| 探索| 龙游县| 城固县| 偃师市| 福安市| 铜陵市| 离岛区| 永顺县| 新和县| 高台县| 休宁县| 雅江县| 南乐县| 沈阳市| 濉溪县| 顺义区| 新闻| 沁阳市| 措勤县| 广南县| 许昌市| 拜城县| 宕昌县| 雷州市| 亚东县| 蕲春县| 育儿| 涞源县| 祁阳县| 梧州市| 同仁县| 仁怀市| 富民县| 柳林县| 临洮县| 依安县| 邓州市| 松溪县|