解决el-table加载树形数据假死,前端实现懒加载

elementUI文档


    <el-table
      :load="getChildren"
      :data="tableList"
      row-key="deptId"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      lazy
    >
/*
 v-bind:load  展开数据项调用懒加载方法
 row-key  懒加载必填项,对象id
 v-bind:tree-props={
	children  	下级数据集
	hasChildren    判断是否有下级数据并显示展开按钮
 }
 lazy 是否懒加载
*/


<script>
export default {
  name: "Dept",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 表格树数据
      deptList: [],
      tableList: [],
      lazyTreeNodeMap: new Map(),
      
      // 分页数据
      page: 1,
      size: 10,
      total: 0,
    }
  },
  created: {
  
    this.getList();
     try {
       this.$refs['table'].store.states.lazyTreeNodeMap = {}
     } catch (e) {

     }
	listDept(this.queryParams).then(response => {
       this.deptList = response.data
       this.tableList = this.mapChildren(this.deptList)
     })
  },
  methods: {
  	// 请求表格数据
    getList() {
      this.loading = true;
      this.deptList = []
      this.tableList = []
      this.lazyTreeNodeMap = {}
     // 刷新el-table组件保存的懒加载数据
      try {
        this.$refs['table'].store.states.lazyTreeNodeMap = {}
      } catch (e) {

      }
      listDept(this.queryParams).then(response => {
        this.deptList = response.data
 //		分页处理
 //     this.total = response.data.length
 //     this.tableList = this.mapChildren(this.deptList).splice(0, this.size)
        this.tableList = this.mapChildren(this.deptList)
        this.loading = false;
      }).catch(() => {
        this.loading = false
      });
    },
    /** 递归树结构*/
    mapChildren(arr) {
      const newArr = []
      arr.forEach(item => {
        const obj = item
        if(item.children) {
          if (!item.children.length) {
            obj.hasChildren = false
          } else {
            // 保存对象children
            this.lazyTreeNodeMap[item.deptId] = this.mapChildren(item.children)
            obj.hasChildren = true
          }
          delete obj.children
        }
        newArr.push(obj)
      })
      return newArr
    },
    /* el-table懒加载调用方法,返回对象id下的children */
    getChildren(tree, treeNode, resolve) {
      resolve(this.lazyTreeNodeMap[tree.deptId] || [])
    },
  }
  // 分页
  
    handleSizeChange(size) {
      this.size = size
      const start = 0 + (this.page - 1) * this.size
      this.tableList = this.mapChildren(this.deptList).splice(start, this.size)
    },

    handleCurrentChange(page) {
      this.page = page
      const start = 0 + (this.page - 1) * this.size
      this.tableList = this.mapChildren(this.deptList).splice(start, this.size)
    },
}
     
</script>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值