element ui改写实现两棵树

使用element ui组件库实现一个table的两棵树的效果

效果如下,左边树自动展开一级,右边树默认显示楼层,然后可以一个个展开

代码如下

 <el-table :data="relativeData"
              :fit="isFit"
              height="700px"
              :row-style="showTr"
              :row-class-name="tableRowClassName"
              :header-row-class-name="tableRowClassName"
              size="small"
              highlight-current-row>
      <el-table-column fixed
                       width="310"
                       show-overflow-tooltip
                       style="max-width:310px;">
        <template slot="header">
          <div class="monitor_header">
            <div class="monitor_item">分项名称</div>
            <div class="line_item"></div>
            <div class="building_item">建筑空间</div>
          </div>
        </template>
        <template slot-scope="scope">
          <template v-if="spaceIconShow()">
            <span v-for="(space, levelIndex) in scope.row._level"
                  class="ms-tree-space"
                  :key="levelIndex"></span>
          </template>

          <span class="button"
                v-if="toggleIconShow(scope.row)"
                @click="toggle(scope.$index)">
            <i v-if="!scope.row._expanded"
               class="el-icon-plus"
               aria-hidden="true"></i>
            <i v-if="scope.row._expanded"
               class="el-icon-minus"
               aria-hidden="true"></i>
          </span>
          <span class="ms-tree-space"></span>
          {{scope.row.name}}
          <div class="table_tree_icons">

            <i class="plus_icon"
               @click.stop="addMonitorDailog(scope.row,$event)"></i>
            <i class="edit_icon"
               @click.stop="updateMonitorDailog(scope.row,$event)"></i>
            <i class="delete_icon"
               v-if="scope.row.parentNodeId!=‘0‘"
               @click.stop="
                   deleteMonitor(scope.row,$event)"></i></div>
        </template>
      </el-table-column>
      <el-table-column v-for="(building,index) in spaceTreeData2"
                       :key="index"
                       show-overflow-tooltip
                       :label="building.name">
        <template slot="header">
          <!-- <i class="el-icon-plus"
                   style="color:#ddd;margin-right:5px;cursor:pointer"
                   @click="toggleExpandFloor(building.id,$event)"></i> -->
          <div>{{building.name}}</div>
        </template>
        <el-table-column width="120"
                         show-overflow-tooltip>
          <el-table-column width="120"
                           show-overflow-tooltip>
            <template slot-scope="scope">
              <template v-if="!(scope.row.relatives.some(item => {if(item.spaceId==building.id){return true;}}))">
                <span class="row-column-express"
                      @click.stop="addExpress(scope.row,building.id,$event)">--</span>
              </template>
              <template v-for=" (item,index) in scope.row.relatives">
                <span class="row-column-express"
                      :key="index"
                      v-if="item.spaceId==building.id"
                      @click.stop="updateExpress(item,$event)">{{ item.expression }}</span>
              </template>
            </template>
          </el-table-column>
        </el-table-column>
        <template v-if="showFloor &&building.children&& building.children.length>0">
          <!--  :render-header="renderHeaderSecond" -->
          <el-table-column v-for="(floor,findex) in building.children"
                           :key="findex"
                           show-overflow-tooltip
                           :label="floor.name"
                           width="120">
            <template slot="header"
                      slot-scope="scope">
              <div><i class="el-icon-plus"
                   @click="expandFloorInfo(scope,floor,$event)"></i><span>{{floor.name}}</span></div>
            </template>
            <el-table-column width="120"
                             show-overflow-tooltip>
              <template slot-scope="scope">
                <template v-if="!(scope.row.relatives.some(item => {if(item.spaceId==floor.id){return true;}}))">
                  <span class="row-column-express"
                        @click.stop="addExpress(scope.row,floor.id,$event)">--</span>
                </template>
                <template v-for=" (item,index3) in scope.row.relatives">
                  <span class="row-column-express"
                        :key="index3"
                        v-if="item.spaceId==floor.id"
                        @click.stop="updateExpress(item,$event)">{{ item.expression }}</span>
                </template>

              </template>
            </el-table-column>
            <template v-if="floor.children && floor.children.length>0">
              <el-table-column v-for="(room,index2) in floor.children"
                               show-overflow-tooltip
                               :key="index2"
                               :label="room.name"
                               width="120">
                <template slot-scope="scope">
                  <template v-if="!(scope.row.relatives.some(item => {if(item.spaceId==room.id){return true;}}))">
                    <span class="row-column-express"
                          @click.stop="addExpress(scope.row,room.id,$event)">--</span>
                  </template>
                  <template v-for=" (item,index4) in scope.row.relatives">
                    <span class="row-column-express"
                          :key="index4"
                          v-if="item.spaceId==room.id"
                          @click.stop="updateExpress(item,$event)">{{ item.expression }}</span>
                  </template>

                </template>
              </el-table-column>

            </template>
          </el-table-column>
        </template>
      </el-table-column>
    </el-table>

数据层

  props: {
    //左边分项树的内容
    monitorRelativeData: {
      type: Array
    },
   //头部空间树的数据
    spaceTreeData: {
      type: Array
    }
  },
data() {
    return {
      expandRowKeys: [], //默认展开的节点
      isFit: false, //table是否自适应
      roomList: []//为了控制房间的显示,存储房间集合
    }
  },
computed: {
    //数据的处理,每条数据是否展开,父节点信息等
    relativeData() {
      let data = MSDataTransfer.treeToArray(this.monitorRelativeData, null, null, true)
      return data
    },
    //只存储楼层,房间另外存储
    spaceTreeData2() {
      var res = this.spaceTreeData
      var roomList = []
      if (res.length == 0 || !res[0].children) {
        return []
      }
      res[0].children = this.spaceTreeData[0].children.map(function(item) {
        item.len = item.children ? item.children.length : 0
        if (item.len > 0) {
          roomList = roomList.concat(item.children)
        }
        item.children = []
        return item
      })
      this.roomList = roomList
      return res
    },
    buildingId() {
      return this.$store.getters.buildingId
    }
  },
            
MSDataTransfer.treeToArray是把tree的层级关系进行处理  加入 _expanded属性,_parent父节点属性,_level层级关系
// 数据转换
DataTransfer.treeToArray = function(data, parent, level, expandedAll) {
  let tmp = [];
  Array.from(data).forEach(function(record) {
    if (record._expanded === undefined) {
      Vue.set(record, "_expanded", expandedAll);
    }
    if (parent) {
      Vue.set(record, "_parent", parent);
    }
    let _level = 0;
    if (level !== undefined && level !== null) {
      _level = level + 1;
    }
    Vue.set(record, "_level", _level);
    tmp.push(record);
    if (record.children && record.children.length > 0) {
      let children = DataTransfer.treeToArray(
        record.children,
        record,
        _level,
        expandedAll
      );
      tmp = tmp.concat(children);
    }
  });

  return tmp;
};
MSDataTransfer.treeToArray处理左边树的时候是,默认展开所有,然后在每次操作的时候,都对树的是否展开状态进行更改
 updated() {
    this.toggleAll()
  },
  methods: {
    //test add
    spaceIconShow(index) {
      return true
    },
    toggleIconShow(record) {
      if (record.children && record.children.length > 0) {
        return true
      }
      return false
    },
    toggle(trIndex) {
      let record = this.relativeData[trIndex]
      record._expanded = !record._expanded
      //展开的数据节点
      if (record._expanded) {
        this.expandRowKeys.push(record.id)
      } else {
        var expandRowKeys = []
        this.expandRowKeys.forEach(function(item) {
          if (item != record.id) {
            expandRowKeys.push(item)
          }
        })
        this.expandRowKeys = expandRowKeys
      }
    },
    showTr(rows, index) {
      let row = rows.row
      let show = row._parent ? row._parent._expanded && row._parent._show : true
      row._show = show
      return show ? ‘‘ : ‘display:none;‘
    },
    toggleAll() {
      /**
       * i 从1开始,不收起第一级节点
       */
      for (let i = 1; i < this.relativeData.length; i++) {
        let record = this.relativeData[i]
        if (this.expandRowKeys.indexOf(record.id) == -1) {
          record._expanded = false
        }
      }
      console.log(‘toggleAll‘, this.relativeData)
    },/**关闭房间 */
    expandFloorInfo(row, floor, event) {
      if (floor.len > 0 && floor.children.length == 0) {
        var roomList = this.roomList
        for (var i = 0; i < roomList.length; i++) {
          if (roomList[i].parentNodeId && roomList[i].parentNodeId == floor.nodeId) {
            floor.children.push(roomList[i])
            if (floor.len == floor.children.length) {
              break
            }
          }
        }
        this.expandFloorInfo(row, floor, event)
        return
      }
      var column = row.column
      console.log(‘column‘, column)
      var len = floor.children.length
      var columnArr = column.id.split(‘_‘)
      var num = columnArr[columnArr.length - 1]
      columnArr.pop()
      columnArr.pop()
      var tableClass = columnArr.join(‘_‘)
      console.log(‘tableClass‘, tableClass)
      num++
      //展开
      if (event.target.className == ‘el-icon-plus‘) {
        event.target.className = ‘el-icon-minus‘
        event.target.parentNode.parentNode.parentNode.colSpan = len + 1
        JQuery(‘.‘ + tableClass)[0].colSpan += len
        JQuery(‘.el-table table.el-table__header‘).css(‘cssText‘, ‘width:310px !important‘)
        JQuery(‘.el-table table.el-table__body‘).css(‘cssText‘, ‘width:310px!important‘)
        JQuery(‘[name^=‘ + column.id + ‘_]‘).show()
        JQuery(‘[class^=‘ + column.id + ‘_]‘).show()
      } else {
        //收缩
        event.target.className = ‘el-icon-plus‘
        JQuery(‘[name^=‘ + column.id + ‘_]‘).hide()
        JQuery(‘[class^=‘ + column.id + ‘_]‘).hide()
        JQuery(‘[name^=‘ + column.id + ‘_column_‘ + num + ‘]‘).show()
        JQuery(‘[class^=‘ + column.id + ‘_column_‘ + num + ‘]‘).show()
        event.target.parentNode.parentNode.parentNode.colSpan = 1
        JQuery(‘.‘ + tableClass)[0].colSpan -= len
        JQuery(‘.el-table table.el-table__body‘).css(‘cssText‘, ‘width:310px !important‘)
        JQuery(‘.el-table table.el-table__header‘).css(‘cssText‘, ‘width:310px !important‘)
      }
    },}

原文地址:https://www.cnblogs.com/xuqp/p/11551790.html

时间: 2024-10-27 17:24:06

element ui改写实现两棵树的相关文章

【LeetCode-面试算法经典-Java实现】【100-Same Tree(两棵树是否相同)】

[100-Same Tree(两棵树是否相同)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 题目大

数据结构 &amp;&amp; ACM :比较两棵树是否相等。

题目: 有如下数据结构: typedef struct TreeNode{ char c; TreeNode *leftChild; TreeNode *rightChild; }; 现在实现函数:int CompTree(TreeNode *tree1, TreeNode *tree2); 比较两棵树是否相等 代码: typedef struct TreeNode{ char c; TreeNode *leftChild; TreeNode *rightChild; }; //两棵树相等的话返

LeetCode&mdash;&mdash;Same Tree(判断两棵树是否相同)

问题: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value.   分析: 考虑使用深度优先遍历的方法,同时遍历两棵树,遇到不等的就返回. 代码如下: /** * Definition f

两棵树,你砍哪一棵?

老教授问:“如果你去山上砍树,正好面前有两棵树,一棵粗,另一棵细,你会砍哪一棵?” 问题一出,大家都说:“当然砍那棵粗的了.” 老教授一笑,说:“那棵粗的不过是一棵普通的杨树,而那棵细的却是红松,现在你们会砍哪一棵?” 我们一想,红松比较珍贵,就说:“当然砍红松了,杨树又不值钱!” 老教授带着不变的微笑看着我们,问:“那如果杨树是笔直的,而红松却七歪八扭,你们会砍哪一棵?” 我们觉得有些疑惑,就说:“如果这样的话,还是砍杨树.红松弯弯曲曲的,什么都做不了!” 老教授目光闪烁着,我们猜想他又要加条

[51nod1325]两棵树的问题

description 题面 solution 点分治+最小割. 点分必选的重心,再在树上dfs判交,转化为最大权闭合子图. 可以做\(k\)棵树的情况. code #include<iostream> #include<cstdlib> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<queue> #defin

用递归方法判断两棵树是否相等

#include<iostream> #include<vector> #include<stack> #include<string> #include<queue> #include<algorithm> #include<numeric> using namespace std; class node{ public: int val; node* left; node* right; node():val(0),l

比较两棵二叉树--(比较两棵二叉树是否相同/判断一棵二叉树是否是另一棵二叉树的子树)

一,问题介绍 本文章讨论两个问题: ①如何判断两棵二叉树的结构是一样的.对应的每个结点都有着相同的值.--即判断两棵二叉树是一样的 ②给定两棵二叉树,如何判断一棵二叉树是另一棵二叉树的子结构 ③给定两棵二叉树,如何判断一棵二叉树是另一棵二叉树的子树 注意,子结点与子树有那么一点点不同. 上面的二叉树B 是二叉树A 的子结构,但是不能说是二叉树A的子树.但是二叉树C 是 二叉树A的子树. 二,问题分析 1,如何判断两棵二叉树的结构是一样的.且对应的每个结点都有着相同的值. 对于①如何判断两棵二叉树

LeetCode:Same Tree - 判断两颗树是否相等

1.题目名称 Same Tree(判断两棵树是否相等) 2.题目地址 https://leetcode.com/problems/same-tree/ 3.题目内容 英文:Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes h

hdu-3015 Disharmony Trees---离散化+两个树状数组

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3015 题目大意: 有一些树,这些树的高度和位置给出.现在高度和位置都按从小到大排序,对应一个新的rank,任意两棵树的值为min(高度的rank) * abs(位置差的绝对值).问所有任意两棵树的值的和是多少. 解题思路: 按照题意离散化,然后对H从大到小排序,这样可以保证前面的树高度都比当前的高(或者相等).在计算的时候就可以使用当前的H. 和POJ-1990类似 1 #include<iost