el-tree动态加载大量数据

临时记录

  • 前言


前言

element-ui组件库中的tree组件如何动态加载大量数据。


el-tree动态加载大量数据_第1张图片

<el-tree
  :props="props"
  :load="loadNode"
  lazy
  show-checkbox
  @check-change="handleCheckChange">
</el-tree>

<script>
  export default {
   
    data() {
   
      return {
   
        props: {
   
          label: 'name',
          children: 'zones'
        },
        count: 1
      };
    },
    methods: {
   
      handleCheckChange(data, checked, indeterminate) {
   
        console.log(data, checked, indeterminate);
      },
      handleNodeClick(data) {
   
        console.log(data);
      },
      loadNode(node, resolve) {
   
        if (node.level === 0) {
   
          return resolve([{
    name: 'region1' }, {
    name: 'region2' }]);
        }
        if (node.level > 3) return resolve([]);

        var hasChild;
        if (node.data.name === 'region1') {
   
          hasChild = true;
        } else if (node.data.name === 'region2') {
   
          hasChild = false;
        } else {
   
          hasChild = Math.random() > 0.5;
        }

        setTimeout(() => {
   
          var data;
          if (hasChild) {
   
            data = [{
   
              name: 'zone' + this.count++
            }, {
   
              name: 'zone' + this.count++
            }];
          } else {
   
            data = [];
          }

          resolve(data);
        }, 500);
      }
    }
  };
</script>
<template>
  <div id

你可能感兴趣的:(前端工程师,学习笔记,javascript,css,vue.js,html)