• eltree 和 elswitch 组合使用


    1、效果

    2、组件代码

    <template>
      <div style=" 370px">
        <div>
          <el-input placeholder="输入关键字进行过滤" v-model="filterText"
            >></el-input
          >
        </div>
        <el-tree
          :data="treeData"
          :props="defaultProps"
          :width="treeWidth"
          @node-click="handleNodeClick"
          node-key="id"
          :expand-on-click-node="false"
          :filter-node-method="filterNode"
          default-expand-all
          ref="mapTree"
        >
          <span class="custom-tree-node" slot-scope="{ data }">
            <span>{{ data.label }}</span>
            <span>
              <el-switch
                v-model="data.isChecked"
                active-color="#13ce66"
                inactive-color="#608060"
                :width="35"
                :active-value="true"
                :inactive-value="false"
                @change="switchOpenOrClose(data)"
              >
              </el-switch>
            </span>
          </span>
        </el-tree>
      </div>
    </template>
    
    <script>
    export default {
      name: "SwitchTree",
      props: {
        treeData: {
          type: Array,
        },
        treeWidth: {
          type: Number,
          default: 200,
        },
      },
      data() {
        return {
          filterText: "",
          defaultProps: {
            children: "children",
            label: "label",
          },
        };
      },
      methods: {
        handleNodeClick() {},
        switchOpenOrClose(data) {
          this.$emit("openswitch", data);
          this.setSwitchStatus(data.id, this.treeData, data.isChecked);
        },
        filterNode(value, data) {
          if (!value) return true;
          return data.label.indexOf(value) !== -1;
        },
        setSwitchStatus(id, data, status) {
          let that = this;
          data.forEach((x) => {
            if (x.id == id) {
              x.isChecked = status;
              if (x.children) {
                x.children.forEach((o) => {
                  that.setSwitchStatus(o.id, x.children, status);
                });
              }
            } else {
              if (x.children) {
                that.setSwitchStatus(id, x.children, status);
              }
            }
          });
          this.setParentSwitcStatus(this.treeData[0]);
        },
        setParentSwitcStatus(data) {
          let that = this;
          let isChecked = true;
          if (data.children) {
            data.children.forEach((x) => {
              if (!x.isChecked) {
                isChecked = false;
              }
              if (x.children) {
                that.setParentSwitcStatus(x);
              }
            });
          }
          data.isChecked = isChecked;
        },
      },
      watch: {
        filterText(val) {
          this.$refs.mapTree.filter(val);
        },
      },
    };
    </script>
    
    <style scoped>
    .custom-tree-node /deep/ .el-switch__core {
       35px;
      height: 15px;
    }
    .custom-tree-node /deep/ .el-switch__core::after {
      height: 12px;
       12px;
    }
    
    .custom-tree-node {
      flex: 1;
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 14px;
      padding-right: 8px;
    }
    </style>
    

      

    3、使用示例

    <template>
        <div>
          <switch-tree
            :treeData="data"
            @openswitch="openOrCloseSwitch"
          ></switch-tree>
        </div>
    </template>
    
    <script>
    import SwitchTree from "./SwitchTree.vue";
    export default {
      name: "HelloWorld",
      components: {
        SwitchTree,
      },
      props: {
        treeWith: {
          type: Number,
          default: 200,
        },
      },
      data() {
        return {
          filterText: "",
          data: [
            {
              id: 1,
              label: "一级",
              isChecked: false,
              children: [
                {
                  id: 2,
                  label: "二级 1-1",
                  isChecked: false,
                  children: [
                    {
                      id: 3,
                      label: "三级 1-1-1",
                      isChecked: false,
                    },
                    {
                      id: 4,
                      label: "三级 1-1-2",
                    },
                  ],
                },
              ],
            },
            {
              id: 5,
              label: "一级",
              isChecked: true,
            },
          ],
        };
      },
      methods: {
       
        openOrCloseSwitch(data) {
          if (data.isChecked) {
            console.log("open");       
          } else {
            console.log("close");
          }
        },
    
      },
    
    };
    </script>
    
    <!-- Add "scoped" attribute to limit CSS to this component only -->
    <style scoped>
    
    </style>
     
  • 相关阅读:
    C# Socket 入门2(转)
    C# Socket 入门1(转)
    StructLayout特性(转)
    [转载]U3d常规性能优化技巧
    Python语言系统学习(七)
    Python语言系统学习(五)
    python语言系统学习(六)
    python语言系统学习(四)
    白话经典算法-常见排序算法的实现与性能比较
    最快的内容查找算法-----暴雪的Hash算法
  • 原文地址:https://www.cnblogs.com/xiaoqiyaozou/p/15958466.html
Copyright © 2020-2023  润新知