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

<script>
// import _ from 'lodash'
import { deepClone } from "@/common/util";

export default {
	name: "TableHeadSearch",

	props: {
		// 原来的model值
		searchForm: {
			type: Object,
			default: () => { },
		},
		optionConfig: {
			type: Object,
			default: () => { },
		},
		// 多选列表
		// optionList: {
		//   type: Array,
		//   default: () => []
		// },
		// 多选列表Obj
		baseData: {
			type: Object,
			default: () => { },
		},
	},
	data() {
		return {
			isPopoverVisible: false,
			checkAll: false,
			checkedList: [],
			isIndeterminate: false,
		};
	},

	methods: {
		handleOpen() {
			this.checkedList = deepClone(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>