网站运营怎么样,做网站该读啥学校,简述站点推广有哪些方式,做论坛网站的cms背景#xff1a;现在有一个新需求#xff0c;需要借助树结构来实现词库的分类管理#xff0c;树的节点是不同的分类#xff0c;不同的分类可以有自己的词库#xff0c;所以父子节点是互不影响的#xff1b;同样为了选择的方便性#xff0c;提出了新需求#xff0c;选择…
背景现在有一个新需求需要借助树结构来实现词库的分类管理树的节点是不同的分类不同的分类可以有自己的词库所以父子节点是互不影响的同样为了选择的方便性提出了新需求选择了父级子级需要全选父级取消勾选子级需要全部取消勾选分类支持修改名称、增加子节点、删除子节点多选子节点时需要获取当前所选分类下的所有词库。
一、开发前需要知道的
1、树结构需要借助 element 的 tree 组件
2、树结构需要设置父子级不关联即 :check-strictlytrue
3、节点选择时如果是非叶子结点最后一层的子节点没有 children需要递归获取子节点如果进行节点的选择与取消选择
4、自定义节点需要通过 slot 方式活着 render-content 方式实现
二、实现流程
1、引入 tree传入源数据 data、设置 key设置默认配置属性 defaultProps
1. 定义 tree
el-tree:check-strictlytrue:datadatacheck-changecheckChangereftreeRefshow-checkboxdefault-expand-allnode-keyidreftreehighlight-current:propsdefaultProps/el-tree
2. 设置 data
data [{id: xx, label: xx, children: {id: number, label: string, children: []}[]}]
3. 设置父子不关联
:check-strictlytrue
4. 设置默认配置
const defaultProps {children: children,label: label,
}
2、实现父子勾选子级联动
// 1. 给 el-tree 绑定 check-change 事情
check-changecheckChange// 2. 实现 checkChange 函数
// data当前节点 checked节点是否选中 indeterminate是否有选中的子项
function checkChange(data, checked, indeterminate) {console.log(data, checked, indeterminate, data, checked, indeterminate);this.$nextTick(() {// 2.1 获取节点详细信息const nodeInfo this.$refs.treeRef.getNode(data.id);const { isLeaf, checked } nodeInfo;this.nodeInfo nodeInfo;function getAllIds(arr, res []) {for (let i 0; i arr.length; i) {res.push(arr[i].id);if (arr[i].children arr[i].children.length) {getAllIds(arr[i].children, res);}}return res;}// 2.2 获取当前已选中的节点console.log(this.$refs.treeRef.getCheckedKeys());// 2.3 如果是父级子级选中跟取消选择if (!isLeaf) {const checkedIds this.$refs.treeRef.getCheckedKeys();const ids getAllIds(nodeInfo.data.children);console.log(ids);// 2.3.1 父级选中子级全选if (checked) {this.$refs.treeRef.setCheckedKeys(Array.from(new Set([...checkedIds, ...ids])));} else {// 2.3.2 父级取消选中子级全不选this.$refs.treeRef.setCheckedKeys(Array.from(new Set(checkedIds.filter((item) !ids.includes(item)))));}}});
},
3、通过 slot 实现编辑、新增、删除
1. slot 内容
span classcustom-tree-node slot-scope{ node, data }span{{ node.label }}/spanspanel-button typetext sizemini click() edit(node, data)edit/el-buttonel-button typetext sizemini click() append(data)Append/el-buttonel-buttontypetextsizeminiclick() remove(node, data)Delete/el-button/span
/span2. 设置函数
{// 2.1 编辑子级可以弹窗回显名称然后修改名称edit(node, data) {console.log(node, data);},// 2.2 新增新增内容自己定也可以弹窗自定义名称通过接口返回 id 再拼接append(data) {const newChild { id: this.id, label: testtest, children: [] };if (!data.children) {this.$set(data, children, []);}data.children.push(newChild);},// 2.3 删除可以先进行提示提示确定后再删remove(node, data) {const parent node.parent;const children parent.data.children || parent.data;const index children.findIndex((d) d.id data.id);children.splice(index, 1);}
}
三、整体代码
!DOCTYPE html
html langenheadmeta charsetUTF-8 /meta nameviewport contentwidthdevice-width, initial-scale1.0 /titleDocument/titlelinkrelstylesheethrefhttps://unpkg.com/element-ui2.15.14/lib/theme-chalk/index.css/style.custom-tree-node {flex: 1;display: flex;align-items: center;justify-content: space-between;font-size: 14px;padding-right: 8px;}/style/headbodyscript srchttps://unpkg.com/vue2.7.16/dist/vue.js/scriptscript srchttps://unpkg.com/element-ui2.15.14/lib/index.js/scriptdiv idappel-tree:check-strictlytrue:datadatacheck-changecheckChangereftreeRefshow-checkboxdefault-expand-allnode-keyidreftreehighlight-current:propsdefaultPropsspan classcustom-tree-node slot-scope{ node, data }span{{ node.label }}/spanspanel-button typetext sizemini click() edit(node, data)edit/el-buttonel-button typetext sizemini click() append(data)Append/el-buttonel-buttontypetextsizeminiclick() remove(node, data)Delete/el-button/span/span/el-treediv classbuttonsel-button clickgetCheckedNodes通过 node 获取/el-buttonel-button clickgetCheckedKeys通过 key 获取/el-buttonel-button clicksetCheckedNodes通过 node 设置/el-buttonel-button clicksetCheckedKeys通过 key 设置/el-buttonel-button clickresetChecked清空/el-button/div/divscriptvar Main {methods: {edit(node, data) {console.log(node, data);},append(data) {const newChild { id: this.id, label: testtest, children: [] };if (!data.children) {this.$set(data, children, []);}data.children.push(newChild);},remove(node, data) {const parent node.parent;const children parent.data.children || parent.data;const index children.findIndex((d) d.id data.id);children.splice(index, 1);},checkChange(data, checked, indeterminate) {this.node data;this.checked checked;this.indeterminate indeterminate;// console.log(data, checked, indeterminate, data, checked, indeterminate);this.$nextTick(() {console.log(data.id);const nodeInfo this.$refs.treeRef.getNode(data.id);const { isLeaf, checked } nodeInfo;this.nodeInfo nodeInfo;console.log(nodeInfo);// console.log(// nodeInfo, isLeaf, childNodes, checked,// nodeInfo,// isLeaf,// childNodes,// checked// );function getAllIds(arr, res []) {for (let i 0; i arr.length; i) {res.push(arr[i].id);if (arr[i].children arr[i].children.length) {getAllIds(arr[i].children, res);}}return res;}console.log(this.$refs.treeRef.getCheckedKeys());if (!isLeaf) {const checkedIds this.$refs.treeRef.getCheckedKeys();const ids getAllIds(nodeInfo.data.children);console.log(ids);if (checked) {this.$refs.treeRef.setCheckedKeys(Array.from(new Set([...checkedIds, ...ids])));} else {this.$refs.treeRef.setCheckedKeys(Array.from(new Set(checkedIds.filter((item) !ids.includes(item)))));}}});},getCheckedNodes() {console.log(this.$refs.tree.getCheckedNodes());},getCheckedKeys() {console.log(this.$refs.tree.getCheckedKeys());},setCheckedNodes() {this.$refs.tree.setCheckedNodes([{id: 5,label: 二级 2-1,},{id: 9,label: 三级 1-1-1,},]);},setCheckedKeys() {this.$refs.tree.setCheckedKeys([3]);},resetChecked() {this.$refs.tree.setCheckedKeys([]);},},data() {return {id: 10,data: [{id: 1,label: 一级 1,children: [{id: 4,label: 二级 1-1,children: [{id: 9,label: 三级 1-1-1,},{id: 10,label: 三级 1-1-2,},],},],},{id: 2,label: 一级 2,children: [{id: 5,label: 二级 2-1,},{id: 6,label: 二级 2-2,},],},{id: 3,label: 一级 3,children: [{id: 7,label: 二级 3-1,},{id: 8,label: 二级 3-2,},],},],defaultProps: {children: children,label: label,},};},};var Ctor Vue.extend(Main);new Ctor().$mount(#app);/script/body
/html四、codepen 在线编辑
https://codepen.io/CAILeiz/pen/MYgpYOW 文章转载自: http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.wjmb.cn.gov.cn.wjmb.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.spnky.cn.gov.cn.spnky.cn http://www.morning.wdrxh.cn.gov.cn.wdrxh.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.zxxys.cn.gov.cn.zxxys.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.snkry.cn.gov.cn.snkry.cn http://www.morning.hcrxn.cn.gov.cn.hcrxn.cn http://www.morning.npmcf.cn.gov.cn.npmcf.cn http://www.morning.qbxdt.cn.gov.cn.qbxdt.cn http://www.morning.lmknf.cn.gov.cn.lmknf.cn http://www.morning.snnkt.cn.gov.cn.snnkt.cn http://www.morning.nccyc.cn.gov.cn.nccyc.cn http://www.morning.kbntl.cn.gov.cn.kbntl.cn http://www.morning.yrrnx.cn.gov.cn.yrrnx.cn http://www.morning.wdhlc.cn.gov.cn.wdhlc.cn http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.rnnts.cn.gov.cn.rnnts.cn http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn http://www.morning.mqxrx.cn.gov.cn.mqxrx.cn http://www.morning.lynkz.cn.gov.cn.lynkz.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.plhhd.cn.gov.cn.plhhd.cn http://www.morning.nqbcj.cn.gov.cn.nqbcj.cn http://www.morning.mxlmn.cn.gov.cn.mxlmn.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.rhlhk.cn.gov.cn.rhlhk.cn http://www.morning.rlfr.cn.gov.cn.rlfr.cn http://www.morning.ckhyj.cn.gov.cn.ckhyj.cn http://www.morning.ssjee.cn.gov.cn.ssjee.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.fflnw.cn.gov.cn.fflnw.cn http://www.morning.smqjl.cn.gov.cn.smqjl.cn http://www.morning.wslr.cn.gov.cn.wslr.cn http://www.morning.qghjc.cn.gov.cn.qghjc.cn http://www.morning.rqgbd.cn.gov.cn.rqgbd.cn http://www.morning.pqjlp.cn.gov.cn.pqjlp.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.hkysq.cn.gov.cn.hkysq.cn http://www.morning.qytpt.cn.gov.cn.qytpt.cn http://www.morning.yhpl.cn.gov.cn.yhpl.cn http://www.morning.zlmbc.cn.gov.cn.zlmbc.cn http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.krjrb.cn.gov.cn.krjrb.cn http://www.morning.hhrpy.cn.gov.cn.hhrpy.cn http://www.morning.qbfs.cn.gov.cn.qbfs.cn http://www.morning.fmswb.cn.gov.cn.fmswb.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.bfbl.cn.gov.cn.bfbl.cn http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.zhmgcreativeeducation.cn.gov.cn.zhmgcreativeeducation.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.nktxr.cn.gov.cn.nktxr.cn http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.kgsws.cn.gov.cn.kgsws.cn http://www.morning.npxht.cn.gov.cn.npxht.cn http://www.morning.lphtm.cn.gov.cn.lphtm.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.rwfp.cn.gov.cn.rwfp.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.kqpq.cn.gov.cn.kqpq.cn http://www.morning.qhkdt.cn.gov.cn.qhkdt.cn http://www.morning.fnpyk.cn.gov.cn.fnpyk.cn http://www.morning.ljdd.cn.gov.cn.ljdd.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.gjxr.cn.gov.cn.gjxr.cn http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.zydr.cn.gov.cn.zydr.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.bwmq.cn.gov.cn.bwmq.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.zztmk.cn.gov.cn.zztmk.cn