brandTree.vue 5.41 KB
<template>
  <d2-dialog title="选择品牌" :is-visible="dialogVisible" class="brand-dialog" width="50%" @closeDialog="handleClose"
    @confirmDialog="handleConfirm">
    <el-form :inline="true" size="mini" class="demo-form-inline">
      <el-form-item label="品牌名称:">
        <el-input v-model="filterText" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="content">
      <div class="tree-box">
        <div class="title">预选品牌</div>
        <el-tree ref="tree" node-key="id" :filter-node-method="filterNode" :data="brandData" :props="defaultProps"
          show-checkbox @check-change="handleNodeClick"></el-tree>
      </div>
      <!-- <div class="check-box">
        <div class="title">已选品牌
          <el-button type="text" @click="resetChecked">清空</el-button>
        </div>
        <template v-if="checkList.length == 0">
          <div class="no-data">暂无数据</div>
        </template>
<template v-else>
          <div class="check-list">
            <div class="check-item" v-for="item in checkList" :key="item.id">{{ item.label }}<i class="el-icon-close"
                @click="closeChecked(item)"></i></div>
          </div>
        </template>
</div> -->
    </div>
  </d2-dialog>

</template>
<script>
import { deepClone } from "@/common/util"
import { getCarbrandTree } from "@api/contentManager"
export default {
  name: "brandTree",
  props: {
    dialogVisible: Boolean,
    defaultKeys: Array
  },
  data() {
    return {
      filterText: '',
      checked: false,
      checkList: [],
      brandData: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      selectList: []
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val);
    },
    defaultKeys(val) {
      console.log(val)
      this.$nextTick(() => {
        // val.forEach(i => {
        //   this.$refs.tree.setChecked(i, true)
        // })
        this.$refs.tree.setCheckedKeys(val)
      })
    }
  },
  mounted() {
    this.getCarbrandTree()
  },
  methods: {
    // 获取品牌树
    async getCarbrandTree() {
      const res = await getCarbrandTree()
      this.brandData = res
    },
    handleClose() {
      this.resetChecked()
      this.$emit('close')
    },
    handleConfirm() {
      this.resetChecked()
      console.log(this.checkList, 'checkList')
      const newList = this.filterCheckList(deepClone(this.checkList))
      console.log(newList, 'newList')
      this.$emit('submit', {
        checkList: this.checkList,
        newList: newList
      })
    },
    onSearch() {
      this.$refs.tree.filter(this.filterText)
    },
    handleNodeClick(data) {
      console.log(data, this.$refs.tree.getCheckedNodes());
      this.checkList = this.$refs.tree.getCheckedNodes()
    },
    filterCheckList(checkList) {
      // 收集所有 children 中的 id
      const childrenIds = new Set();
      checkList.forEach(item => {
        if (item.children && item.children.length > 0) {
          item.children.forEach(child => {
            childrenIds.add(child.id);
          });
        }
      });

      // 过滤掉一级项中 id 存在于 childrenIds 中的项
      return checkList.filter(item => !childrenIds.has(item.id));
    },
    // 清空
    resetChecked() {
      this.$refs.tree.setCheckedKeys([]);
    },
    closeChecked(row) {
      console.log(row)
      this.$refs.tree.setChecked(row.id, false)
      console.log(this.$refs.tree.getCheckedNodes())
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    handleChange(e) {
      console.log(e)
      if (e) {
        const nodes = this.getTreeArray()
        this.$refs.tree.setCheckedKeys(nodes)
      } else {
        this.$refs.tree.setCheckedKeys([]);
      }
    },
    //获取选择项
    getTreeArray() {
      let tree_arr = []
      this.brandData.forEach(k => {
        let child = k.children
        if (child && child.length > 0) {
          child.forEach(j => {
            tree_arr.push(j.id)
          })
        } else {
          tree_arr.push(k.id)
        }
      })
      return tree_arr
    },
  }
}
</script>
<style scoped lang="scss">
.content {
  display: flex;

  .title {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 40px;
    opacity: 1;
    border-bottom: 1px solid #e5e5e5;
    background: #f2f4f7;
    padding: 0 16px;
  }

  .tree-box {
    border: 1px solid #e5e5e5;
    background: #ffffff;
    height: 324px;
    overflow-y: auto;
    width: 100%;

    // margin-right: 20px;
    .tree-title {
      display: flex;
      align-items: center;
      height: 40px;
      opacity: 1;
      border-bottom: 1px solid #e5e5e5;
      background: #f2f4f7;
      padding: 0 16px;
    }
  }

  .check-box {
    flex: 1;
    height: 324px;
    opacity: 1;
    border: 1px solid #e5e5e5;
  }

  .no-data {
    width: 100%;
    height: calc(100% - 40px);
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .check-list {
    margin-top: 12px;
    height: calc(100% - 64px);
    overflow-y: auto;
  }

  .check-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 16px;
  }
}
</style>
<style lang="scss">
.brand-dialog {
  .el-dialog__body {
    padding: 20px !important;
  }
}
</style>