SelectMember.vue 7.75 KB
<template>
  <div>
    <div>
      <el-form ref="form"
               :model="company"
               label-width="154px"></el-form>
    </div>
    <div class="member-wrapper">
      <div class="member-left">
        <div class="member-content">
          <div class="member-content-head">
            <el-checkbox :indeterminate="isIndeterminate"
                         v-model="checkAll"
                         @change="handleCheckAllChange">全选</el-checkbox>
          </div>

          <div class="table-content">
            <el-input size="medium"
                      v-model="filterText"
                      suffix-icon="el-icon-search"
                      clearable
                      placeholder="请输入搜索关键字"></el-input>
            <el-tree :data="departmentList"
                     :default-expanded-keys="expandedKeys"
                     show-checkbox
                     ref="tree"
                     node-key="id"
                     :props="defaultProps"
                     @check="handleNodeClick"
                     :filter-node-method="filterNode"></el-tree>
          </div>
        </div>
      </div>
      <div class="member-right">
        <div class="member-name member-content-head">
          <div>已选名称</div>
          <div class="blue"
               @click="clear">清空</div>
        </div>
        <div class="input-tag-wrapper">
          <div class="tag-box">
            <el-tag v-for="(label, idx) in userList"
                    :key="idx"
                    type="info"
                    closable
                    :disable-transitions="false"
                    @close="remove(idx)">
              {{ label.name }}
            </el-tag>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      newArr: [],
      filterText: '',
      userList: [],
      isIndeterminate: false,
      checkAll: false,
      ids: [],
      selectIds: [],
      expandedKeys: [],
      defaultIds: [],
      departmentList: [],

      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  props: {
    merchantId: {
      type: String,
      default: ''
    },
    selectlist: {
      type: Array,
      default: () => []
    }
  },
  created() {
    this.getDepartmentTree()
  },
  watch: {
    filterText(val) {
      console.log(val)
      this.$refs.tree.filter(val)
    },
    selectlist(val) {
      this.defaultData()
    }
  },
  methods: {
    clear() {
      this.userList = []
      this.$refs.tree.setCheckedKeys([])
      this.handleNodeClick()
    },
    handleCheckAllChange(val) {
      // console.log(this.ids)
      val ? this.$refs.tree.setCheckedKeys(this.ids) : this.$refs.tree.setCheckedKeys([])
      this.handleNodeClick()
      this.isIndeterminate = false
    },
    defaultData() {
      this.defaultIds = this.selectlist.map(item => item.staffId)
      this.$refs.tree.setCheckedKeys(this.defaultIds)
      this.handleNodeClick()
    },
    getMerchantList() {
      this.$apis.battle.getMerchantList({}).then(res => {
        this.merchantList = res
        this.merchantId = this.merchantList[0].merchantId
        this.merchantName = this.merchantList[0].merchantName
      })
    },
    async getDepartmentTree() {
      let params = { addChildrensFlag: true, queryMerchantId: this.merchantId }
      //  let params = { addChildrensFlag: true, queryMerchantId: '44210895' }
      let data = await this.$apis.battle.getlistDepartmentTree(params)
      this.ids = []
      this.departmentList = data
      await this.initData(this.departmentList)
      this.expandedKeys = [this.departmentList[0].id]
      this.$nextTick(() => {
        if (this.selectlist.length > 0) {
          this.defaultData()
        }
      })
    },
    initData(data) {
      data.forEach((item, index) => {
        this.$set(item, 'name', item.staffName ? item.staffName : item.departmentName)
        let entmicroStaffResponses = this.$common.utils.deepClone(item.entmicroStaffResponses) || []
        entmicroStaffResponses = entmicroStaffResponses.filter((staff, idx) => {
          if (staff.staffName) {
            this.$set(staff, 'name', staff.staffName)
          } else {
            this.$set(staff, 'name', staff.departmentName)
          }
          if (staff.staffId && !this.ids.includes(staff.staffId)) {
            this.ids.push(staff.staffId)
            return true
          } else if (staff.staffId && this.ids.includes(staff.staffId)) {
            return false
          }
        })
        let child = this.$common.utils.deepClone(item.children) || []
        child = child.filter((staff, idx) => {
          if (staff.staffName) {
            this.$set(staff, 'name', staff.staffName)
          } else {
            this.$set(staff, 'name', staff.departmentName)
          }
          if (staff.staffId && !this.ids.includes(staff.staffId)) {
            this.ids.push(staff.staffId)
            return true
          } else if (staff.staffId && this.ids.includes(staff.staffId)) {
            return false
          } else if (staff.entmicroStaffResponses) {
            staff.staffId = staff.id
            return true
          }
        })
        const children = [...child, ...entmicroStaffResponses]
        item.children = children
        //清除没有员工的部门
        if (!item.staffName && item.entmicroStaffResponses === null) {
          data.splice(index, 1)
        }
        children.length && this.initData(children)
      })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    search() { },
    handleNodeClick() {
      this.userList = this.$refs.tree.getCheckedNodes().filter(item => item.children && item.children.length == 0)
      this.isIndeterminate = this.userList.length > 0 && this.userList.length < this.ids.length
      this.checkAll = this.userList.length === this.ids.length
    },
    remove(index) {
      this.userList.splice(index, 1)
      let ids = this.userList.map(item => item.staffId)
      this.$refs.tree.setCheckedKeys(ids)
      this.isIndeterminate = ids.length < this.ids.length
    }
  }
}
</script>
<style scoped lang="scss">
.member-wrapper {
  display: flex;

  .member-left {
    width: 400px;
    .member-content {
      border: 1px solid #eee;
      .member-content-head {
        height: 44px;
        line-height: 44px;
        padding: 0 16px;
      }
      .member-form {
        padding: 10px 20px;
      }
      .table-content {
        max-height: 456px;
        overflow: auto;
        border: 1px solid #eee;
        .el-input {
          margin: 20px 20px 6px 20px;
          width: 360px;
          height: 36px;
          box-sizing: border-box;
        }
      }
    }
    .member-checked {
      text-align: right;
      margin-top: 10px;
    }
  }
  .member-right {
    width: 264px;
    margin-left: 14px;
    .member-name {
      font-size: 14px;
      color: #333;
      line-height: 44px;
      height: 44px;
      border: 1px solid #eee;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .blue {
        color: #5850ff;
        cursor: pointer;
      }
    }
  }
}
.table-content {
  max-height: 456px;
  overflow: auto;
  border: 1px solid #eee;
}
.table-content .el-tree {
  margin: 0 20px 20px;
}
.member-right {
  width: 300px;
  margin-left: 14px;
}
.member-name {
  font-size: 14px;
  color: #333;
  line-height: 60px;
  height: 60px;
  padding: 0 20px;
  border: 1px solid #eee;
}
.input-tag-wrapper {
  max-height: 456px;
  overflow: scroll;
}
.tag-box {
  margin: 12px 8px;
  .el-tag {
    margin-right: 4px;
    margin-bottom: 4px;
    background-color: #fff !important;
    font-weight: 400;
    font-size: 14px;
    color: #666666;
    line-height: 20px;
    padding: 3px 8px;
    height: auto;
  }
}
</style>