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

Native JS realizes the special effect of spreading love by mouse sliding

This article shares with you a js special effect ...

Vue Router loads different components according to background data

Table of contents Requirements encountered in act...

I have compiled a few cool design sites that I think are good.

You must have inspiration to design a website. Goo...

Introduction to document.activeELement focus element in JavaScript

Table of contents 1. The default focus is on the ...

IE6 web page creation reference IE6 default style

This is not actually an official document of IE. I...

js implements form validation function

This article example shares the specific code of ...

IIS7 IIS8 http automatically jumps to HTTPS (port 80 jumps to port 443)

IIS7 needs to confirm whether the "URL REWRI...

js method to delete a field in an object

This article mainly introduces the implementation...

React native ScrollView pull down refresh effect

This article shares the specific code of the pull...

Detailed tutorial on installing phpMyAdmin on Ubuntu 18.04

We will install phpMyAdmin to work with Apache on...

Building an image server with FastDFS under Linux

Table of contents Server Planning 1. Install syst...

36 principles of MySQL database development (summary)

Preface These principles are summarized from actu...

Vue uses openlayers to load Tiandi Map and Amap

Table of contents 1. World Map 1. Install openlay...

A complete tutorial on using axios encapsulation in vue

Preface Nowadays, in projects, the Axios library ...