vue2点击左侧的树节点(el-tree)定位到对应右侧树形表格(el-table)的位置,树形表格懒加载

在这里插入图片描述

左侧树代码

  <el-tree 
  	:data="treeData" 
  	node-key="id" 
  	default-expand-all="" //节点默认全部展开
  	:expand-on-click-node="false" //是否在点击节点的时候展开或者收缩节点
  	:props="defaultProps" 
  	@node-click="handleNodeClick">
    <span slot-scope="{ node, data }">
         <span>{{ node.label }}</span>
 	</span>
  </el-tree>

右侧树形表格代码

<el-table 
	ref="singleTable" 
	:data="detailsList" 
	highlight-current-row="" 
	row-key="detailId" 
	:tree-props="{children: 'children', hasChildren: 'hasChildren'}" 
	:lazy="lazy" 
	:load="load">
    <el-table-column type="index" width="80" label="序号" align="center" fixed=""> </el-table-column>
   	<el-table-column property="code" label="编码" width="160" fixed=""></el-table-column>
   	<el-table-column property="name" label="名称" width="160" fixed=""></el-table-column>
</el-table>

js代码

data: function () {
     return {
         defaultProps: {
             children: "childrenList",
             label: function (data, node) {
                 return  data.name;
             }
         },
         treeData: [],
         detailsList: [],
         lazy: true //开启懒加载
   	}
},
methods: {
	// 点击分项定位到右边表格位置
    handleNodeClick(data) {
        // 首先获取所有的行row的高度
        const rowHeightList = []; //存放所有元素的高度
        const temp = document.getElementsByClassName("el-table__row");
        for (let i = 0; i < temp.length; i++) {
            const item = temp[i];
            rowHeightList.push(item.scrollHeight);
        }
        let itemRow = {}; //存放当前行的所有数据
        let rowIndex = 0; //选中行位于第几行
        var number = 0
        let fn = dataList => {
            for (let x of dataList) {
                number++
                // 判断分项是否存在,存在则进行定位操作
                if (x.quotaName == data.firstQuotaName) {
                    // itemRow = x;
                    rowIndex = number - 1;
                    break;
                }
                if (x.children) {
                    fn(x.children);
                }
            }
        };
        fn(this.detailsList);
        let totalHeight = 0; //求出选中行之前的的高度之和,需要注意的是,当前行的高度不能包含进去
        for (let index = 0; index < rowHeightList.length; index++) {
            const row = rowHeightList[index];
            if (index < rowIndex) {
                totalHeight += row;
            }
        }
        // 滚动到指定行
        this.$refs.singleTable.bodyWrapper.scrollTop = totalHeight
        this.$refs.singleTable.setCurrentRow(itemRow);
    },
    // 懒加载数据
    load(tree, treeNode, resolve) {
         var childrenList = []
         childrenList = this.queryDetailsList(tree.detailId)	//查询节点数据
         resolve(childrenList)
         // 修改绑定的数据
         this.updateTableData(tree.detailId,childrenList)
     },
     // 查询节点数据
     queryDetailsList(detailParentId) {
	      let that = this
          let childrenList = []
          $.ajax({
              url: 接口地址,
              type: "get",
              dataType: "json",
              async: false,
              contentType: "application/json;charset=UTF-8",
              success: function (data) {
                  if (data.isOk) {
                      if (data.data) {
                         childrenList = data.data
                      }
                  } else {
                      $.Dialog.error(data.msg);
                  }
              },
          });
          return childrenList
     },
     // 修改绑定的数据
    updateTableData(detailId,childrenList) {
         let getMenu = function (data) {
             if (data.children){
                 data.children.forEach((itemChildren) => {
                     if (itemChildren.detailId == detailId) {
                         if (itemChildren.children) {
                             itemChildren.children.forEach(itemOld=>{
                                 childrenList.forEach(itemNew=>{
                                     if ((itemOld.detailId == itemNew.detailId) && itemOld.children) {
                                         itemNew.children = itemOld.children
                                     }
                                 })
                             })
                         }
                         itemChildren.children = childrenList
                     } else {
                         getMenu(itemChildren);
                     }
                 });
             }
         }
         this.detailsList.forEach(item=>{
             getMenu(item);
         })
     },
}

el-table左键双击单元格编辑内容(输入框输入计算公式可直接得出结果),右键单击展示操作菜单,可编辑单元格高亮展示

Vue3中使用Element Plus的el-table组件实现树形表格,并且希望在选中所有子节点时自动选中父节点,可以通过监听表格的选中状态变化,并结合树形数据的结构特点来实现。具体步骤如下: 1. 使用`v-model`绑定表格的选中行数据数组。 2. 通过计算属性或方法获取到所有的子节点。 3. 监听选中行数据的变化,当发现有子节点被选中时,遍历并检查其父节点是否已经在选中状态中,如果不是,则将其加入到选中数组中。 以下是一个简单的示例代码: ```html <template> <el-table :data="tableData" style="width: 100%" row-key="id" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" v-model="checkedRowKeys" > <el-table-column type="selection" width="55"> </el-table-column> <!-- 其他列 --> </el-table> </template> <script> import { ref, computed } from 'vue'; export default { setup() { const tableData = ref([...]); // 填充你的树形数据 const checkedRowKeys = ref([]); // 递归获取所有子节点 const getChildren = (data, childrenKey) => { return data.reduce((acc, item) => { const children = item[childrenKey]; if (children && children.length > 0) { acc = [...acc, ...getChildren(children, childrenKey)]; } acc.push(item); return acc; }, []); }; // 监听选中状态变化 const handleSelectionChange = (val) => { const childrenKeys = tableData.value.reduce((acc, item) => { const children = item.children; if (children && children.length > 0) { const childKeys = getChildren(children, 'children').map(child => child.id); acc = [...acc, ...childKeys]; } return acc; }, []); // 移除不需要的父节点 const filteredCheckedRowKeys = val.filter(key => !childrenKeys.includes(key)); // 添加选中的子节点对应的父节点 tableData.value.forEach((node) => { if (node.children && node.children.some(child => val.includes(child.id))) { filteredCheckedRowKeys.push(node.id); } }); checkedRowKeys.value = filteredCheckedRowKeys; }; return { tableData, checkedRowKeys, handleSelectionChange, }; }, }; </script> ``` 在上面的代码中,`tableData` 是包含树形数据的数组,`checkedRowKeys` 是用于绑定当前选中行的数组。`handleSelectionChange` 方法会在表格的选中状态变化时被调用,它会处理子节点选中时父节点的选中状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值