Vue elementUI implements tree structure table and lazy loading

Vue elementUI implements tree structure table and lazy loading

1. Achieve results

2. Backend implementation

2.1 Entity Class

 @Data
@ApiModel(description = "data dictionary")
@TableName("dict")
public class Dict {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    private Long id;

    @ApiModelProperty(value = "creation time")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField("create_time")
    private Date createTime;

    @ApiModelProperty(value = "update time")
    @TableField("update_time")
    private Date updateTime;

    @ApiModelProperty(value = "Logical deletion (1: deleted, 0: not deleted)")
    @TableLogic
    @TableField("is_deleted")
    private Integer isDeleted;

    @ApiModelProperty(value = "other parameters")
    @TableField(exist = false)
    private Map<String,Object> param = new HashMap<>();

    @ApiModelProperty(value = "superior id")
    @TableField("parent_id")
    private Long parentId;

    @ApiModelProperty(value = "name")
    @TableField("name")
    private String name;

    @ApiModelProperty(value = "value")
    @TableField("value")
    private String value;

    @ApiModelProperty(value = "encoding")
    @TableField("dict_code")
    private String dictCode;

    @ApiModelProperty(value = "Whether to include child nodes")
    @TableField(exist = false)
    private boolean hasChildren;

}

The above must include a hasChildren property, even if it does not exist in the database. This is required by the element framework.

2.2 Data Structure in the Database

2.3 Backend interface

If it is implemented entirely on the backend, then just write a recursive query to query all the data according to the hierarchical structure and encapsulate it. But the table component of element has encapsulated some things for us. We only need to query the sub-data list according to the parent ID here.

Controller code:

 //Query the sub-data list according to the parent id @ApiOperation(value = "Query the sub-data list according to the parent id")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }

service

Service implementation class

 @Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    //Query the sub-data list according to the parent id @Override
    public List<Dict> findChildData(Long id) {
        QueryWrapper<Dict> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Dict> list = baseMapper.selectList(wrapper);
        //Set hasChildren to each dict object in the list collection
        list.forEach(x->{
            Long dictId = x.getId();
            boolean isChild = this.isChildren(dictId);
            x.setHasChildren(isChild);
        });
        return list;
    }

    //Judge whether there is child data under id private boolean isChildren(Long id){
        QueryWrapper<Dict> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        Integer count = baseMapper.selectCount(wrapper);
        return count > 0;
    }
}

2.4 Swagger tests whether the backend structure and function are normal

3. Front-end implementation

3.1 Introducing el-table component into the page

list.vue

 <template>
  <div class="app-container">

    <el-table
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      lazy
      :load="getChildrens"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="Name" width="230" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="Encoding" width="220">
        <template slot-scope="{row}">
          {{ row.dictCode }}
        </template>
      </el-table-column>
      <el-table-column label="value" width="230" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.value }}</span>
        </template>
      </el-table-column>
      <el-table-column label="Creation time" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>

</template>

<script>
import dict from '@/api/dict'
export default {
  name: 'list',
  data(){
    return {
      list:[], //data dictionary list array dialogImportVisible:false, //set whether the pop-up box pops up}
  },
  created() {
    this.getDictList(1)
  },
  methods:{
    //Data dictionary list getDictList(id){
      dict.dictList(id)
        .then(response=>{
            this.list=response.data
        })
    },
    getChildrens(tree, treeNode, resolve) {
      dict.dictList(tree.id).then(response => {
        resolve(response.data)
      })
    },
  }
}
</script>

<style scoped>

</style>

The key method above is the getChildrens method, which calls the backend interface at each layer to query the child node data and add it to the table data in the tree structure.

The tool js file dict.js that calls the backend structure

 import request from '@/utils/request'

export default {
  //Data dictionary list dictList(id){
    return request({
      url: `/admin/cmn/dict/findChildData/${id}`,
      method: 'get'
    })
  },
}

3.2 Implementation Effect

There are no problems with the front-end and back-end tests. Since lazy loading is used, the data of the child node will only be loaded when the parent node is clicked, avoiding system freezes due to excessive data volume.

This is the end of this article about Vue elementUI's implementation of tree structure tables and lazy loading. For more related Vue elementUI content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue elementUI form nested table and verification of each row detailed explanation
  • Vue component library ElementUI realizes the paging effect of table list
  • Vue+ElementUI table realizes table paging
  • Vue+elementUI realizes table keyword filtering and highlighting
  • Vue2.0+ElementUI implements table page turning example
  • Detailed explanation of ElementUI table based on Vue

<<:  What are the new CSS :where and :is pseudo-class functions?

>>:  HTML page adaptive width table

Recommend

MySQL data operation-use of DML statements

illustrate DML (Data Manipulation Language) refer...

React antd realizes dynamic increase and decrease of form

I encountered a pitfall when writing dynamic form...

Summary of practical experience of HTML knowledge points

1. The table tag is table, tr is row, td is cell, ...

Example of troubleshooting method to solve Nginx port conflict

Problem Description A Spring + Angular project wi...

Summary of methods for writing judgment statements in MySQL

How to write judgment statements in mysql: Method...

CSS3 achieves cool sliced ​​image carousel effect

Today we will learn how to use CSS to create a co...

Detailed explanation of Vue3 sandbox mechanism

Table of contents Preface Browser compiled versio...

MySQL database transaction example tutorial

Table of contents 1. What is a transaction? 2. Th...

Analysis of a MySQL deadlock scenario example

Preface Recently I encountered a deadlock problem...

Learn how to use the supervisor watchdog in 3 minutes

Software and hardware environment centos7.6.1810 ...

js implements the classic minesweeper game

This article example shares the specific code of ...

Detailed explanation of the four transaction isolation levels in MySQL

The test environment of this experiment: Windows ...

Vue two-choice tab bar switching new approach

Problem Description When we are working on a proj...

MySQL slow query pitfalls

Table of contents 1. Slow query configuration 1-1...