首页 > 其他分享 >element树形复选框实现一级菜单单选

element树形复选框实现一级菜单单选

时间:2023-04-03 12:03:56浏览次数:37  
标签:initArr name element length 单选 选中 复选框 type id

  

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <script src="https://unpkg.com/vue@2/dist/vue.js"></script>
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <style>
    .part2 {
      margin: 0 auto;
    }
  </style>
</head>

<body>
  <div id="app">
    <div style="width: 100%;display: flex;">
      <el-select ref="selectTree" clearable v-model="value" placeholder="总行(默认)" class="part2">
        <el-option :value="selectValue" style="height: auto !important;overFlow: auto">
          <el-tree :data="centerOptions" :props="defaultProps" node-key="id" show-checkbox ref="Tree"
            style="height:500px" @check="checkClick">
            </e1-tree>
        </el-option>
      </el-select>
    </div>
  </div>
  <script>
    new Vue(
      {
        el: '#app',
        data: {
          value: [],
          selectValue: [],
          defaultProps: {
            children: "children",
            value: "id",
            label: "name",
          },
          centerOptions: [
            {
              "name": "陕西省分行",
              "checked": false,
              "type": 'one',
              "id": "11005293",
              "children": [
                {
                  id: 1,
                  name: "A支行",
                  type: "one",
                },
                {
                  id: 2,
                  name: "B支行",
                  type: 'one'
                },
                {
                  id: 3,
                  name: "c 支行",
                  type: 'one'
                }
              ]
            },
            {
              "name": "厦门分行",
              "checked": false,
              "type": 'two',
              "id": "110052931",
              children: [
                {
                  "id": 4,
                  name: "D支行",
                  type: 'two'
                },
                {
                  id: 5,
                  name: "E支行",
                  type: 'two'
                }
              ]
            }
          ],
          initArr: [] // 存放选中的值
        },
        methods: {
          checkClick(checkedNodes, checkedKeys, halfCheckedNodes, halfCheckedKeys) {
            this.initArr.push(checkedNodes);
            if (this.initArr.length > 1) {
              if (this.initArr[0].type != this.initArr[1].type) { // 一级选中和跨级选中进行判断
                this.initArr = [this.initArr[1]];
                this.$nextTick(() => {
                  this.$refs.Tree.setCheckedKeys([this.initArr[0].id]);
                })
              } else if (this.initArr[this.initArr.length - 2].type != this.initArr[this.initArr.length - 1].type) { // 选中多个的时候进行判断
                this.initArr = [this.initArr[this.initArr.length - 1]];
                this.$nextTick(() => {
                  this.$refs.Tree.setCheckedKeys([this.initArr[this.initArr.length - 1].id]);
                })
              }
            }
          },
        }
      }
    )
  </script>
</body>

</html>

  代码思路:

           1. 通过点击第二次复选框与前一次点击复选框进行唯一值判断,相同类型的不做处理,不同类型的值取后面一次点击的,前面的数据都清空。

           2. this.initArr[0].type != this.initArr[1].type   // 一级选中和跨级选中进行判断

           3. 除了2情况外,就是点击全选中的时候数据是多个,需要判断最后一个与最后倒数二个   this.initArr[this.initArr.length - 2].type != this.initArr[this.initArr.length - 1].type

标签:initArr,name,element,length,单选,选中,复选框,type,id
From: https://www.cnblogs.com/PengZhao-Mr/p/17282673.html

相关文章

  • Unknown custom element: <el-empty> - did you register the component correctly? For
     报错原因:“el-empty”未注册解决:element版本太低了,当前版本里面查找不到el-enpty这个组件,需要重新安装一下element的版本。[email protected]@2.15.6-S重新运行,上面的问题就解决了。......
  • .net6 制作elementplus离线文档
    1、新建net6项目设置配置信息<ProjectSdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>net6.0</TargetFramework><Nullable>enable</Nullable><ImplicitUsings>enable</ImplicitUsings>......
  • 【element UI】修改 el-select 下拉框样式
    前言项目中经常使用到el-select组件,默认的el-select组件样式不符合项目实际需要,需要进行样式修改,这里记录下样式的修改步骤。借鉴文章:https://blog.csdn.net/qq_26695613/article/details/127870263实现过程官方文档里有该属性popper-append-to-body1、在使用到el-se......
  • vue3+elementPlus 深色主题切换
    首先安装需要的两个依赖npmi@vueuse/corenpminstallelement-plus--save在main.js中引入css文件,自定义深色背景颜色可以看ElementPlus官方网站//引入elementUIimportElementPlusfrom'element-plus'importzhCnfrom'element-plus/dist/locale/zh-cn.mjs'//引入......
  • element-ui plus中如何单独出发el-upload提交
    因为单独提交才好触发el-upload中的on-success函数在Vue3中,可以通过ref引用指向upload组件,然后使用该引用调用upload的submit方法来触发上传操作。具体实现如下:<template><el-uploadref="uploadRef"action="https://www.mocky.io/v2/5cc8019d300000980a05......
  • 覆盖ElementPlus样式theme
    import{defineConfig}from'vite'importvuefrom'@vitejs/plugin-vue'importpathfrom'path'importAutoImportfrom'unplugin-auto-import/vite'importComponentsfrom'unplugin-vue-components/vite'imp......
  • vue elementui中使表的滚动条 滚到顶部或底部
    1.设置table的ref为tableList2.设置滚动至顶部this.$refs.tableList.bodyWrapper.scrollTop=0;3.设置滚动至底部this.$refs.tableList.bodyWrapper.scrollTop=this.$refs.tableList.bodyWrapper.scrollHeight;//如果请求完更新数据,需要使用$nextTickthis.$next......
  • elementUI 表单input输入框限制整数和小数长度
    <el-inputv-model="input1"placeholder="请输入内容"@keyup.native="input1=limitControlLine(input1,5,2)"></el-input>limitControlLine(val,zs,xs){letvalue=val;if(isNaN(val)){value=String(val).replace(/[......
  • element+vue2的查询form表单封装成组件复用
    <template><el-form:inline="true"style="display:flex;flex-direction:row;flex-wrap:wrap;flex:1;"class="formClass"label-width="90px......
  • element ui Table 自定义头部样式
    <el-table-column><templateslot="header"><divclass="Stakedrow-center"><imgsrc="../image/caret-down.png"alt=""><span>Date&T......