TableHeadSearch.vue 2.7 KB
<!-- 表格头部搜索 -->
<template>
  <el-popover
    placement="bottom"
    title=""
    width="150"
    trigger="click"
    v-model="isPopoverVisible"
  >
    <block>
      <p class="flex">
        <el-checkbox
          :indeterminate="isIndeterminate"
          v-model="checkAll"
          @change="handleCheckAllChange"
        >全选</el-checkbox>
        <el-button
          @click="handleConfirm"
          type="text"
        >确定</el-button>
      </p>
      <el-checkbox-group
        v-model="checkedList"
        @change="handleCheckedChange"
      >
        <el-checkbox
          v-for="item in baseData[optionConfig.searchItemName]"
          :label="item[optionConfig.optionId]"
          :key="item[optionConfig.optionId]"
          @change="handleCheckedChange"
        >{{item[optionConfig.optionLabel]}}</el-checkbox>
      </el-checkbox-group>
    </block>
    <img
      slot="reference"
      class="head-search"
      src="./image/search.png"
      @click="handleOpen"
    />
  </el-popover>
</template>

<script>
import _ from 'lodash'
export default {
  name: 'TableHeadSearch',
  data() {
    return {
      isPopoverVisible: false,
      checkAll: false,
      checkedList: [],
      isIndeterminate: false
    }
  },

  props: {
    // 原来的model值
    searchForm: {
      type: Array,
      default: () => { }
    },
    optionConfig: {
      type: Object,
      default: () => { }
    },
    // 多选列表
    // optionList: {
    //   type: Array,
    //   default: () => []
    // },
    // 多选列表Obj
    baseData: {
      type: Object,
      default: () => { }
    }
  },

  methods: {
    handleOpen() {
      this.checkedList = _.cloneDeep(this.searchForm[this.optionConfig.searchItemName])
      this.handleCheckedChange(this.checkedList)
      this.$nextTick()
    },

    handleCheckedChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.baseData[this.optionConfig.searchItemName].length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.baseData[this.optionConfig.searchItemName].length
    },

    handleCheckAllChange(val) {
      this.checkedList = val ? this.baseData[this.optionConfig.searchItemName].map(item => item[this.optionConfig.optionId]) : []
      this.isIndeterminate = false
    },

    handleConfirm() {
      this.isPopoverVisible = false
      this.$emit('handleTableHeadChange', { searchItemName: this.optionConfig.searchItemName, checkedList: this.checkedList })
    }
  }
}

</script>
<style scoped>
.flex {
  justify-content: space-between;
}
.head-search {
  width: 12px;
  height: 12px;
}
</style>