antd-vue Checkbox Group 封装实现多选、全选、搜索过滤组件

实现效果:

 说明:

基于antd实现搜索,全选,多选功能,并且支持保存,重置,初始默认选中项。

注意事项:

我的列表格式是这样的

[
    { code: 1, name: '停车场1' },
    { code: 2, name: '停车场2' },
    { code: 3, name: '停车场3' },
    { code: 4, name: '停车场4' },
    { code: 5, name: '停车场5' },
    { code: 6, name: '停车场6' },
    { code: 7, name: '停车场7' },
    { code: 8, name: '停车场8' },
    { code: 9, name: '停车场9' },
]

所以需要注意的是checkbox上绑定的value要是code,不然group是找不到对应的值。

checkbox-group v-model对应的是选中的arr,格式是[1, 2, 3, 4]这样的,所以保存时候要取交集。

具体代码:

html部分:

<template>
    <a-modal
      v-model="visible"
      :title="title"
      :width="width"
      @ok="save"
      okText="确定"
      cancelText="关闭"
    >
      <a-form-model
        ref="ruleForm"
      >
        <a-form-model-item v-if="isShowSearch">
            <a-input v-model="parkingName" style="width: 280px" placeholder="搜索停车场" />
        </a-form-model-item>
        <a-form-model-item>
            <!-- 全选 -->
            <a-checkbox
                v-if="parkingList && parkingList.length"
                v-model="checkAll"
                :indeterminate="indeterminate"
                @change="onCheckAllChange"
            >
                全选
            </a-checkbox>
            <!-- 列表 -->
            <a-checkbox-group v-model="checkedList" style="width: 100%;max-height:300px;overflow-y:scroll;">
                <a-row>
                    <a-col :span="checkboxSpan" :key="item.code" v-for="(item) in parkingList" style="padding: 10px 0;">
                        <a-checkbox :value="item.code">{{item.name}}</a-checkbox>
                    </a-col>
                </a-row>
            </a-checkbox-group>
        </a-form-model-item>
      </a-form-model>
    </a-modal>
</template>

js部分:

<script>
export default {
    name: 'CheckboxList', //复选框列表
    props: {
        title: {
            type: String
        },
        list: {
            type: Array
        },
        width: {
            type: Number,
            default: 1000
        },
        // 是否开启搜索功能
        isShowSearch: {
            type: Boolean,
            default: true
        },
        checkboxSpan: {
            type: Number,
            default: 8
        },
        // 初始默认选中项
        initCheckedList: {
            type: Array
        },
    },
    data() {
        return {
            parkingName: '',
            visible: false,
            parkingList: [],
            indeterminate: false,
            checkAll: false,
            checkedList: []
        }
    },
    watch: {
        list: {
            handler(newD) {
                this.parkingList = newD;
            },
        },
        initCheckedList: {
            handler(newD) {
                this.checkedList = newD;
            },
        },
        // 根据搜索名称过滤列表
        parkingName: {
            handler(newD) {
                if (this.list) {
                    this.checkedList = [];
                    this.indeterminate = false;
                    this.checkAll = false;
                    if (newD === "") {
                        this.parkingList = this.list;
                    } else {
                        this.parkingList = this.list.filter((item) => {
                            return item.name.includes(newD);
                        });
                    }
                }
            },
        },
        checkedList: {
            handler(val) {
                this.indeterminate = !!val.length && val.length < this.parkingList.length;
                this.checkAll = val.length > 0 && val.length === this.parkingList.length;
            },
        },
    },
    methods: {
        onCheckAllChange(e) {
            this.checkedList = e.target.checked ? this.parkingList.map(item => item.code) : [];
            this.indeterminate = false;
        },
        showModal() {
            this.visible = true;
        },
        save() {
            let checkedList = this.checkedList;
            let list = this.list;
            let intersection = [];
            // 取交集
            for (let i = 0; i < list.length; i++) {
                const item = list[i];
                for (let j = 0; j < checkedList.length; j++) {
                    const items = checkedList[j];
                    if(item.code == items){
                        intersection.push(item);
                    }
                }
            }
            this.$emit('checkedListChange', intersection);
            this.visible = false;
        },
        reset() {
            this.checkedList = [];
            this.indeterminate = false;
            this.checkAll = false;
        }
    },
}

</script>

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值