Vue?elementUI實現(xiàn)樹形結構表格與懶加載
1、實現(xiàn)效果

2、后端實現(xiàn)
2.1 實體類
@Data
@ApiModel(description = "數(shù)據(jù)字典")
@TableName("dict")
public class Dict {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "創(chuàng)建時間")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
@TableField("create_time")
private Date createTime;
@ApiModelProperty(value = "更新時間")
@TableField("update_time")
private Date updateTime;
@ApiModelProperty(value = "邏輯刪除(1:已刪除,0:未刪除)")
@TableLogic
@TableField("is_deleted")
private Integer isDeleted;
@ApiModelProperty(value = "其他參數(shù)")
@TableField(exist = false)
private Map<String,Object> param = new HashMap<>();
@ApiModelProperty(value = "上級id")
@TableField("parent_id")
private Long parentId;
@ApiModelProperty(value = "名稱")
@TableField("name")
private String name;
@ApiModelProperty(value = "值")
@TableField("value")
private String value;
@ApiModelProperty(value = "編碼")
@TableField("dict_code")
private String dictCode;
@ApiModelProperty(value = "是否包含子節(jié)點")
@TableField(exist = false)
private boolean hasChildren;
}
上面必須包含一個hasChildren屬性,即使數(shù)據(jù)庫中沒有該屬性,這是element框架要求的。
2.2 數(shù)據(jù)庫中的數(shù)據(jù)結構

2.3 后端接口
如果完全用后端實現(xiàn)的話,那寫個遞歸把所有數(shù)據(jù)按照層次結構查詢完并封裝好即可。但element的table組件給我們封裝好了一些東西,我們只需要在這里根據(jù)上級id查詢子數(shù)據(jù)列表即可。
controller代碼:
//根據(jù)上級id查詢子數(shù)據(jù)列表
@ApiOperation(value = "根據(jù)上級id查詢子數(shù)據(jù)列表")
@GetMapping("findChildData/{id}")
public Result findChildData(@PathVariable Long id){
List<Dict> list = dictService.findChildData(id);
return Result.ok(list);
}
service

service實現(xiàn)類
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
//根據(jù)上級id查詢子數(shù)據(jù)列表
@Override
public List<Dict> findChildData(Long id) {
QueryWrapper<Dict> wrapper=new QueryWrapper<>();
wrapper.eq("parent_id",id);
List<Dict> list = baseMapper.selectList(wrapper);
//向list集合中的每個dict對象中設置hasChildren
list.forEach(x->{
Long dictId = x.getId();
boolean isChild = this.isChildren(dictId);
x.setHasChildren(isChild);
});
return list;
}
//判斷id下面是否有子數(shù)據(jù)
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測試后端結構功能是否正常

3、前端實現(xiàn)
3.1 頁面中引入el-table組件
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="名稱" width="230" align="left">
<template slot-scope="scope">
<span>{{ scope.row.name }}</span>
</template>
</el-table-column>
<el-table-column label="編碼" width="220">
<template slot-scope="{row}">
{{ row.dictCode }}
</template>
</el-table-column>
<el-table-column label="值" width="230" align="left">
<template slot-scope="scope">
<span>{{ scope.row.value }}</span>
</template>
</el-table-column>
<el-table-column label="創(chuàng)建時間" 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:[], //數(shù)據(jù)字典列表數(shù)組
dialogImportVisible:false, //設置彈框是否彈出
}
},
created() {
this.getDictList(1)
},
methods:{
//數(shù)據(jù)字典列表
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>
上面關鍵的方法是getChildrens這個方法,在每一層調(diào)用后端接口將子節(jié)點數(shù)據(jù)查詢出來,并加入樹形結構的表格數(shù)據(jù)中。
調(diào)用后端結構的工具js文件 dict.js
import request from '@/utils/request'
export default {
//數(shù)據(jù)字典列表
dictList(id){
return request({
url: `/admin/cmn/dict/findChildData/${id}`,
method: 'get'
})
},
}
3.2 實現(xiàn)效果

前后端測試都沒有問題,由于使用的是懶加載,只有去點擊父節(jié)點的時候,子節(jié)點的數(shù)據(jù)才會被加載,避免因數(shù)據(jù)量太大導致系統(tǒng)卡頓。
到此這篇關于Vue elementUI實現(xiàn)樹形結構表格與懶加載的文章就介紹到這了,更多相關Vue elementUI內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決vue數(shù)據(jù)更新但table內(nèi)容不更新的問題
這篇文章主要給大家介紹了vue數(shù)據(jù)更新table內(nèi)容不更新解決方法,文中有詳細的代碼示例供大家作為參考,感興趣的同學可以參考閱讀一下2023-08-08
springboot?vue接口測試前端動態(tài)增刪表單功能實現(xiàn)
這篇文章主要為大家介紹了springboot?vue接口測試前端動態(tài)增刪表單功能實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05
詳解解決使用axios發(fā)送json后臺接收不到的問題
這篇文章主要介紹了詳解解決使用axios發(fā)送json后臺接收不到的問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
nuxt.js 在middleware(中間件)中實現(xiàn)路由鑒權操作
這篇文章主要介紹了nuxt.js 在middleware(中間件)中實現(xiàn)路由鑒權操作,具有很好的參考價值,希望大家有所幫助。一起跟隨小編過來看看吧2020-11-11
vue中使用[provide/inject]實現(xiàn)頁面reload的方法
這篇文章主要介紹了在vue中使用[provide/inject]實現(xiàn)頁面reload的方法,文中給大家提到了在vue中實現(xiàn)頁面刷新不同的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
vue登錄頁實現(xiàn)使用cookie記住7天密碼功能的方法
這篇文章主要介紹了vue登錄頁實現(xiàn)使用cookie記住7天密碼功能的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02
Vue組件開發(fā)之LeanCloud帶圖形校驗碼的短信發(fā)送功能
Vue是目前使用較廣泛的三大前端框架之一,其數(shù)據(jù)驅動及組件化的特性使得前端開發(fā)更為快捷便利。本文在LeanCloud 短信轟炸與圖形校驗碼官方文檔 基礎上,從封裝需要出發(fā)開發(fā)一個簡單的短信圖形驗證Vue組件,具體內(nèi)容詳情大家參考下本文2017-11-11
vue2+springsecurity權限系統(tǒng)的實現(xiàn)
本文主要介紹了vue2+springsecurity權限系統(tǒng)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05

