焦作网站建设哪家便宜,外链工具在线,长沙网站设计公司重庆标志,淄博高端网站建设公司前言
有时遇到一些需求就是在使用树形控件时#xff0c;服务端并没有一次性返回所有数据#xff0c;而是返回首层节点列表。然后点击展开首层节点中的某个节点#xff0c;再去请求该节点的子节点列表#xff0c;那么就得用上懒加载的机制了。在此以ElementPlus的树形控件为…前言
有时遇到一些需求就是在使用树形控件时服务端并没有一次性返回所有数据而是返回首层节点列表。然后点击展开首层节点中的某个节点再去请求该节点的子节点列表那么就得用上懒加载的机制了。在此以ElementPlus的树形控件为例实现一个具有懒加载的树形控件的示例页面。
传送门https://element-plus.gitee.io/zh-CN/component/tree.html
一、示例代码
1/src/views/Example/ElTreeLazy/index.vue
templateel-scrollbar v-loadingtreeLoading element-loading-text数据正在渲染中... classelement-plus-treeel-treelazyreftreeRef:propsdefaultProps:datatreeData:loadloadNode:show-checkboxfalse:default-expand-allfalse:highlight-currenttrue:expand-on-click-nodefalsenode-clickhandleNodeClicktemplate #default{ node, data }span v-if!data.leaf classnode-folderel-icon v-ifnode.expanded stylemargin: 0 6px 0 0px; size16FolderOpened //el-iconel-icon v-else stylemargin: 0 6px 0 0px; size16Folder //el-iconsmall :titlenode.label{{ node.label }}/small/spanspan v-else classnode-fileel-icon stylemargin: 0 6px 0 0px; size16Document //el-iconsmall :titlenode.label{{ node.label }}/small/span/template/el-tree/el-scrollbar
/templatescript setup
import { onMounted, onUnmounted, ref, getCurrentInstance, defineExpose } from vue// 代理对象
const { proxy } getCurrentInstance()// 树组件实例
const treeRef ref(null)// 树组件数据
const treeData ref([])// 加载中
const treeLoading ref(true)// 树节点属性映射关系
const defaultProps {children: children,label: name,isLeaf: leaf,
}/*** 加载节点*/
const loadNode async (node, resolve) {// 首层节点即根节点if (node.level 0) {resolve(node.data)}// 第二层节点if (node.level 1) {// 判断某个节点的子节点列表是否非空非空则不调用接口否则调用接口查询该节点的列表if (node.data.children) {resolve(node.data.children)} else {setTimeout(() {const res { success: true, data: [] }if (res.success) {const nodeNum parseInt(Math.random() * 10) 1 // 1 ~ 10for (let i 1; i nodeNum; i) {res.data.push({name: 哈哈哈-${i},})}const hasChild Math.random() 0.5if (hasChild) {const childNum parseInt(Math.random() * 5) 1 // 1 ~ 5for (let vo of res.data) {vo.children []for (let i 1; i childNum; i) {vo.children.push({name: 嘻嘻嘻-${i},})}}}console.log(叶子节点加工前 , res)handleJudgeLeafrecursion(res.data)console.log(叶子节点加工后 , res)} else {proxy.$message({ message: 请求失败, type: error, duration: 3000 })}node.data.children res.dataresolve(node.data.children)}, 200)}}// 第三层节点以及其它层节点if (node.level 1) {if (node.data.children node.data.children.length 0) {resolve(node.data.children)} else {resolve([])}}
}/*** 获取首层节点数据列表*/
const getFirstLevelNodeData () {const res {success: true,data: [香烟Wifi啤酒, 火腿iPad泡面, 骑着蜗牛散步, 都随它大小便吧, 没有强度全是手法]}const list []setTimeout(() {if (res.success) {for (let i 0; i res.data.length; i) {list.push({id: i 1,name: res.data[i],})}} else {proxy.$message({ message: 请求失败, type: error, duration: 3000 })}treeData.value listtreeLoading.value false}, 200)
}/*** 判断叶子节点递归方法*/
const handleJudgeLeafrecursion (list) {for (let vo of list) {// 若无 children 或 children 大小为零即判定该节点为叶子节点if (vo.children null || vo.children.length 0) {vo.leaf true} else {handleJudgeLeafrecursion(vo.children)}}
}/*** 树节点点击事件句柄方法*/
const handleNodeClick (data) {console.log(handleNodeClick , data)
}// 子组件通过 defineExpose 暴露指定的属性给父组件
defineExpose({treeRef, // 暴露树组件实例
})onMounted(() {getFirstLevelNodeData()
})onUnmounted(() {// ...
})
/scriptstyle langless scoped.element-plus-tree {height: 100%;:deep(.el-tree) {padding-left: 5px;/* ---- ---- ---- ---- ^节点对齐---- ---- ---- ---- */.el-tree-node {/* ^ 所有节点 */i.el-tree-node__expand-icon {padding: 6px;margin-right: 5px;::before {font-family: element-ui-icons;font-style: normal;content: \e6d9;color: #000000;border: 1px solid #606266;border-radius: 2px;}svg {display: none; // 隐藏所有节点的 svg 图标}}/* / 所有节点 *//* ^ 已展开的父节点 */i.el-tree-node__expand-icon.expanded {transform: rotate(0deg); // 取消旋转-webkit-transform: rotate(0deg); // 取消旋转::before {font-family: element-ui-icons;font-style: normal;content: \e6d8;color: #000000;border: 1px solid #606266;border-radius: 2px;}}/* / 已展开的父节点 *//* ^ 叶子节点 */i.el-tree-node__expand-icon.is-leaf {::before {display: none;}}/* / 叶子节点 *//* ^ 复选框 */.el-checkbox {margin: 0 7px 0 2px;.el-checkbox__inner {width: 14px;height: 14px;border-radius: 2px;border: 1px solid #bbb;}.el-checkbox__input.is-checked .el-checkbox__inner,.el-checkbox__input.is-indeterminate .el-checkbox__inner {border: 1px solid #5e7ce0;}}/* / 复选框 */.el-tree-node__content {small {font-size: 13px;padding-right: 5px;transition: all ease 0.1s;}}}/* ---- ---- ---- ---- /节点对齐---- ---- ---- ---- *//* ---- ---- ---- ---- ^文字/背景高亮---- ---- ---- ---- */.el-tree-node {.el-tree-node__content {background-color: transparent;.node-root {display: flex;align-items: center;small {font-weight: bold;color: #40485c;transition: all ease 0.3s;}}.node-folders {display: flex;align-items: center;small {font-weight: bold;color: #40485c;transition: all ease 0.3s;}}.node-folder {display: flex;align-items: center;small {font-weight: bold;color: #40485c;transition: all ease 0.3s;}}.node-file {display: flex;align-items: center;small {font-weight: normal;color: #40485c;transition: all ease 0.3s;}}:hover small {color: #5e7ce0;}}}.el-tree-node.is-current {.el-tree-node__content {small {color: #5e7ce0;}}.el-tree-node__children {small {color: unset;}}}.el-tree--highlight-current .el-tree-node.is-current .el-tree-node__content {background-color: #eff2fc;// background-color: #b6c7ff;i::before, i::after {// border-color: #fff;// color: #fff;border-color: #002293;color: #002293;}small {// color: #fff;color: #002293;}}/* ---- ---- ---- ---- /文字/背景高亮---- ---- ---- ---- *//* ---- ---- ---- ---- ^新增辅助线---- ---- ---- ---- *//* ^ 树节点 */.el-tree-node {position: relative;width: auto;width: max-content; // 显示文字宽度padding-left: 13px;::before {width: 1px;height: 100%;content: ;position: absolute;top: -38px;bottom: 0;left: 0;right: auto;border-width: 1px;border-left: 1px solid #b8b9bb;}::after {width: 13px;height: 13px;content: ;position: absolute;z-index: 0;left: 0;right: auto;top: 12px;bottom: auto;border-width: 1px;border-top: 1px solid #b8b9bb;}.el-tree-node__content {position: relative;z-index: 1;padding-left: 0 !important;/* ^ 复选框 */.el-checkbox {margin: 0 10px 0 0.5px;}/* / 复选框 */}.el-tree-node__children {padding-left: 12px;}:last-child::before {height: 50px;}}/* / 树节点 *//* ^ 第一层节点 */ .el-tree-node {padding-left: 0;::before {border-left: none;}::after {border-top: none;}.el-tree-node__content {i.el-tree-node__expand-icon.is-leaf {margin-right: 5px;}}}/* / 第一层节点 *//* ---- ---- ---- ---- /新增辅助线---- ---- ---- ---- */}}
/style二、运行效果
// Todo 文章转载自: http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.drpbc.cn.gov.cn.drpbc.cn http://www.morning.fktlr.cn.gov.cn.fktlr.cn http://www.morning.xdjwh.cn.gov.cn.xdjwh.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.ktnmg.cn.gov.cn.ktnmg.cn http://www.morning.ndmbd.cn.gov.cn.ndmbd.cn http://www.morning.qlry.cn.gov.cn.qlry.cn http://www.morning.vibwp.cn.gov.cn.vibwp.cn http://www.morning.pzlcd.cn.gov.cn.pzlcd.cn http://www.morning.lbxhy.cn.gov.cn.lbxhy.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.jjwt.cn.gov.cn.jjwt.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.fnmgr.cn.gov.cn.fnmgr.cn http://www.morning.qphdp.cn.gov.cn.qphdp.cn http://www.morning.qrsm.cn.gov.cn.qrsm.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.hxwrs.cn.gov.cn.hxwrs.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.npqps.cn.gov.cn.npqps.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.nsrtvu.com.gov.cn.nsrtvu.com http://www.morning.jikuxy.com.gov.cn.jikuxy.com http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.dmzzt.cn.gov.cn.dmzzt.cn http://www.morning.jlktz.cn.gov.cn.jlktz.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.btpzn.cn.gov.cn.btpzn.cn http://www.morning.gjmbk.cn.gov.cn.gjmbk.cn http://www.morning.dpfr.cn.gov.cn.dpfr.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.zbhfs.cn.gov.cn.zbhfs.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.bnpcq.cn.gov.cn.bnpcq.cn http://www.morning.wfcqr.cn.gov.cn.wfcqr.cn http://www.morning.knqck.cn.gov.cn.knqck.cn http://www.morning.prhfc.cn.gov.cn.prhfc.cn http://www.morning.txgjx.cn.gov.cn.txgjx.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.wdlyt.cn.gov.cn.wdlyt.cn http://www.morning.qkbwd.cn.gov.cn.qkbwd.cn http://www.morning.cwfkm.cn.gov.cn.cwfkm.cn http://www.morning.fyxtn.cn.gov.cn.fyxtn.cn http://www.morning.hmqwn.cn.gov.cn.hmqwn.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.smpb.cn.gov.cn.smpb.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.tmnyj.cn.gov.cn.tmnyj.cn http://www.morning.xzkgp.cn.gov.cn.xzkgp.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.hbqfh.cn.gov.cn.hbqfh.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.wchcx.cn.gov.cn.wchcx.cn http://www.morning.gzxnj.cn.gov.cn.gzxnj.cn http://www.morning.dyrzm.cn.gov.cn.dyrzm.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.fsnhz.cn.gov.cn.fsnhz.cn http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn http://www.morning.ypktc.cn.gov.cn.ypktc.cn http://www.morning.wkknm.cn.gov.cn.wkknm.cn http://www.morning.sgbsr.cn.gov.cn.sgbsr.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.daidudu.com.gov.cn.daidudu.com http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.gtdf.cn.gov.cn.gtdf.cn http://www.morning.jcfqg.cn.gov.cn.jcfqg.cn http://www.morning.spbp.cn.gov.cn.spbp.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.npbnc.cn.gov.cn.npbnc.cn http://www.morning.mbnhr.cn.gov.cn.mbnhr.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.nhzps.cn.gov.cn.nhzps.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn