vue封装搜索组件,自定义elment搜索组件

组件案例
<template>
    <div class="dialog-search">
        <el-form :inline="true" ref="ruleForm" :model="formInline" class="demo-form-inline top-screen">
            <div class="top-screen-left">
                <el-form-item v-for="(item, index) in formItemList" :key="index">
                    <!-- 下拉选择框 -->
                    <el-select
                        v-if="item.type == 'select'"
                        v-model="formInline[item.param]"
                        :multiple="item.multiple"
                        :placeholder="'请选择' + item.label"
                        size="mini"
                        clearable
                        @clear="()=>clear(formInline[item.param],item.param)"
                    >
                        <el-option
                            v-for="(item2, index2) in item.selectOptions"
                            :key="index2"
                            :label="item2.text"
                            :value="item2.value"
                        ></el-option>
                    </el-select>
                    <!-- 下拉选择框end -->
                    <!-- 输入框 -->
                    <el-input
                        v-if="item.type == 'input'"
                        v-model="formInline[item.param]"
                        size="mini"
                        :placeholder="'请输入' + item.label"
                        @clear="()=>clear(formInline[item.param],item.param)"
                        clearable
                    ></el-input>
                    <!-- 输入框 -->
                    <!-- 日期范围选择框 -->
                    <el-date-picker
                        v-if="item.type == 'daterange' || item.type == 'datetimerange' || item.type == 'date' || item.type == 'datetime'"
                        v-model="formInline[item.param]"
                        :value-format="item.valueFormat || 'yyyy-MM-dd'"
                        :format="item.format || 'yyyy-MM-dd'"
                        clearable
                        :type="item.type || ''"
                        :range-separator="item.rangeSeparator || '至'"
                        :start-placeholder="item.startPlaceholder"
                        :end-placeholder="item.endPlaceholder"
                        :placeholder="'请选择' + item.label"
                        :startPlaceholder="item.label+'开始日期'"
                        :endPlaceholder="item.label+'结束日期'"
                        size="mini"
                         @clear="()=>clear(formInline[item.param],item.param)"
                    >
                    </el-date-picker>
                    <!-- 日期范围选择框end -->
                    <!-- 级联选择器 -->
                    <el-cascader
                        v-if="item.type == 'cascader'"
                        v-model="formInline[item.param]"
                        size="mini"
                        :options="item.options"
                        :props="item.props"
                        :placeholder="'请选择' + item.label"
                        clearable
                         @clear="()=>clear(formInline[item.param],item.param)"
                    ></el-cascader>
                    <!-- 级联选择器end -->
                </el-form-item>
                <slot name="formItem"></slot>
            </div>
            <div class="top-screen-right">
                <el-form-item v-if="isShowDefault">
                    <el-button
                        type=""
                        size="mini"
                        @click="resetForm('ruleForm')"
                        class="top-right-bottom top-right-Reset-bottom top-right-bottom-I"
                        >重置</el-button
                    >
                    <el-button type="primary" size="mini" @click="onSubmit" class="top-right-bottom top-right-bottom-I">查询</el-button>
                </el-form-item>

                <!-- 可用于显示其他按钮 -->
                <slot name="formButton" :item="formInline"></slot>
            </div>
        </el-form>
    </div>
</template>

<script>
export default {
    name: 'BaseSearch',
    props: {
        emitSearch: {
            // 是否立即执行搜索
            type: Boolean,
            default: false
        },
        isShowDefault:{
            //是否展示默认按钮
             default: true
        },
        formItemList: {
            type: Array,
            default() {
                return [
                    {
                        label: '下拉框',
                        type: 'select',
                        selectOptions: [{ text: 111, value: 111 }],
                        param: 'company',
                        defaultSelect: '222', // 下拉框默认选中项
                        multiple: false
                    },
                    {
                        label: '输入框',
                        type: 'input',
                        param: 'name'
                    },
                    {
                        label: '日期范围',
                        type: 'daterange',
                        param: 'createtime'
                    },
                    {
                        label: '级联选择器',
                        type: 'cascader',
                        param: 'cascader',
                        options: [],
                        props: { multiple: false }
                    }
                ];
            }
        }
    },
    data() {
        let formInline = {};
        for (const obj of this.formItemList) {
            formInline[obj.param] = obj.defaultSelect || null;
        }
        return {
            formInline
        };
    },
    watch: {
        emitSearch(newVal, oldVal) {
            // 是否立即触发搜索  用在弹窗中异步请求下拉框后  或者给下拉框赋值默认值后  需要用到这个方法
            if (newVal) {
                console.log('此时触发--立即执行搜索');
                this.$emit('search', this.formInline);
            }
        },
        formItemList: {
            handler(newVal, oldVal) {
                for (const obj of this.formItemList) {
                    if (obj.defaultSelect) {
                        formInline[obj.param] = obj.defaultSelect;
                    }
                }
            },
            deep: true
        }
    },
    methods: {
        clear(e,param){
            if(e===''){
                this.formInline[param]=null
            }
        },
        onSubmit() {
            // console.log('submit!',this.formInline);
            this.$emit('search', this.formInline);
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            let formInline = {};
            for (const obj of this.formItemList) {
                // formInline[obj.param] = obj.defaultSelect || "";  // 重置时下拉框的默认值如果要保留就选用这个
                formInline[obj.param] = null; // 所有筛选条件清空
            }
            this.formInline = formInline;

            this.$emit('search', this.formInline);
        }
    }
};
</script>

<style lang="scss" scoped>

.dialog-search {
    // margin: 0 1rem;
    // margin: 20px;
    // padding: 20px;
    // background-color: white;
    // text-align: left;
    // /deep/ .el-form-item__content {
    //     // width: 16rem;
    //     .el-input {
    //         width: 16rem;
    //     }
    //     .el-select {
    //         .el-input__inner {
    //             // height: 3.2rem;
    //             width: 16rem;
    //         }
    //     }
    // }
}
</style>

引用方法

<!--
 * @Author: 张大碗 zhangjunhui@mangocosmos.com
 * @Date: 2023-09-06 16:49:57
 * @LastEditors: 张大碗 zhangjunhui@mangocosmos.com
 * @LastEditTime: 2023-09-08 14:59:20
 * @FilePath: 引用案例说明
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div class="divBox">
        <BaseSearch :formItemList="formItemList" @search="search">
            <div slot="formButton">
                <el-button type="primary" size="mini" @click="add">新增</el-button>
                <el-button type="primary" size="mini">操作按钮</el-button>
            </div>
        </BaseSearch>
    </div>
</template>

<script>
import BaseSearch from '@/components/searchCustom/index.vue';

export default {
    name: 'ceshi',
    components: { BaseSearch },
    data() {
        return {
            formItemList: [
                {
                    label: '下拉框单选:',
                    type: 'select',
                    selectOptions: [{ text: 111, value: 111 }],
                    param: 'company'
                },
                {
                    label: '化学品名称:',
                    type: 'input',
                    param: 'name'
                },
                {
                    label: '下拉框多选:',
                    type: 'select',
                    selectOptions: [],
                    multiple: true,
                    param: 'parm222'
                },
                {
                    label: '日期范围',
                    type: 'daterange',
                    param: 'createtime',
                    startPlaceholder: '开始日期',
                    endPlaceholder: '结束日期'
                },
                {
                    label: '级联选择器',
                    type: 'cascader',
                    param: 'cascader',
                    options: [
                        {
                            value: 'zhinan',
                            label: '指南',
                            children: [
                                {
                                    value: 'shejiyuanze',
                                    label: '设计原则',
                                    children: [
                                        {
                                            value: 'yizhi',
                                            label: '一致'
                                        },
                                        {
                                            value: 'fankui',
                                            label: '反馈'
                                        },
                                        {
                                            value: 'xiaolv',
                                            label: '效率'
                                        },
                                        {
                                            value: 'kekong',
                                            label: '可控'
                                        }
                                    ]
                                },
                                {
                                    value: 'daohang',
                                    label: '导航',
                                    children: [
                                        {
                                            value: 'cexiangdaohang',
                                            label: '侧向导航'
                                        },
                                        {
                                            value: 'dingbudaohang',
                                            label: '顶部导航'
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            value: 'zujian',
                            label: '组件',
                            children: [
                                {
                                    value: 'basic',
                                    label: 'Basic',
                                    children: [
                                        {
                                            value: 'layout',
                                            label: 'Layout 布局'
                                        },
                                        {
                                            value: 'color',
                                            label: 'Color 色彩'
                                        },
                                        {
                                            value: 'typography',
                                            label: 'Typography 字体'
                                        },
                                        {
                                            value: 'icon',
                                            label: 'Icon 图标'
                                        },
                                        {
                                            value: 'button',
                                            label: 'Button 按钮'
                                        }
                                    ]
                                },
                                {
                                    value: 'form',
                                    label: 'Form',
                                    children: [
                                        {
                                            value: 'radio',
                                            label: 'Radio 单选框'
                                        },
                                        {
                                            value: 'checkbox',
                                            label: 'Checkbox 多选框'
                                        },
                                        {
                                            value: 'input',
                                            label: 'Input 输入框'
                                        },
                                        {
                                            value: 'input-number',
                                            label: 'InputNumber 计数器'
                                        },
                                        {
                                            value: 'select',
                                            label: 'Select 选择器'
                                        },
                                        {
                                            value: 'cascader',
                                            label: 'Cascader 级联选择器'
                                        },
                                        {
                                            value: 'switch',
                                            label: 'Switch 开关'
                                        },
                                        {
                                            value: 'slider',
                                            label: 'Slider 滑块'
                                        },
                                        {
                                            value: 'time-picker',
                                            label: 'TimePicker 时间选择器'
                                        },
                                        {
                                            value: 'date-picker',
                                            label: 'DatePicker 日期选择器'
                                        },
                                        {
                                            value: 'datetime-picker',
                                            label: 'DateTimePicker 日期时间选择器'
                                        },
                                        {
                                            value: 'upload',
                                            label: 'Upload 上传'
                                        },
                                        {
                                            value: 'rate',
                                            label: 'Rate 评分'
                                        },
                                        {
                                            value: 'form',
                                            label: 'Form 表单'
                                        }
                                    ]
                                },
                                {
                                    value: 'data',
                                    label: 'Data',
                                    children: [
                                        {
                                            value: 'table',
                                            label: 'Table 表格'
                                        },
                                        {
                                            value: 'tag',
                                            label: 'Tag 标签'
                                        },
                                        {
                                            value: 'progress',
                                            label: 'Progress 进度条'
                                        },
                                        {
                                            value: 'tree',
                                            label: 'Tree 树形控件'
                                        },
                                        {
                                            value: 'pagination',
                                            label: 'Pagination 分页'
                                        },
                                        {
                                            value: 'badge',
                                            label: 'Badge 标记'
                                        }
                                    ]
                                },
                                {
                                    value: 'notice',
                                    label: 'Notice',
                                    children: [
                                        {
                                            value: 'alert',
                                            label: 'Alert 警告'
                                        },
                                        {
                                            value: 'loading',
                                            label: 'Loading 加载'
                                        },
                                        {
                                            value: 'message',
                                            label: 'Message 消息提示'
                                        },
                                        {
                                            value: 'message-box',
                                            label: 'MessageBox 弹框'
                                        },
                                        {
                                            value: 'notification',
                                            label: 'Notification 通知'
                                        }
                                    ]
                                },
                                {
                                    value: 'navigation',
                                    label: 'Navigation',
                                    children: [
                                        {
                                            value: 'menu',
                                            label: 'NavMenu 导航菜单'
                                        },
                                        {
                                            value: 'tabs',
                                            label: 'Tabs 标签页'
                                        },
                                        {
                                            value: 'breadcrumb',
                                            label: 'Breadcrumb 面包屑'
                                        },
                                        {
                                            value: 'dropdown',
                                            label: 'Dropdown 下拉菜单'
                                        },
                                        {
                                            value: 'steps',
                                            label: 'Steps 步骤条'
                                        }
                                    ]
                                },
                                {
                                    value: 'others',
                                    label: 'Others',
                                    children: [
                                        {
                                            value: 'dialog',
                                            label: 'Dialog 对话框'
                                        },
                                        {
                                            value: 'tooltip',
                                            label: 'Tooltip 文字提示'
                                        },
                                        {
                                            value: 'popover',
                                            label: 'Popover 弹出框'
                                        },
                                        {
                                            value: 'card',
                                            label: 'Card 卡片'
                                        },
                                        {
                                            value: 'carousel',
                                            label: 'Carousel 走马灯'
                                        },
                                        {
                                            value: 'collapse',
                                            label: 'Collapse 折叠面板'
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            value: 'ziyuan',
                            label: '资源',
                            children: [
                                {
                                    value: 'axure',
                                    label: 'Axure Components'
                                },
                                {
                                    value: 'sketch',
                                    label: 'Sketch Templates'
                                },
                                {
                                    value: 'jiaohu',
                                    label: '组件交互文档'
                                }
                            ]
                        }
                    ]
                }
            ]
        };
    },
    mounted() {
        // 此时请求下拉框的数据接口
        // 请求字典,把每个下拉option赋值
        this.formItemList.forEach(element => {
            switch (element.param) {
                case 'parm222':
                    element.selectOptions = [
                        { text: 111, value: 111 },
                        { text: 222, value: 222 }
                    ];
                    break;

                default:
                    break;
            }
        });

        this.getList();
    },

    methods: {
        getList(num) {},
        search(params) {
            console.log(params);
            // 搜索条件改变时
        },
        deleteHandle() {},
        add() {
            console.log('新增');
        },
        close() {
            console.log('关闭');
        }
    }
};
</script>

<style lang="scss" scoped></style>

 

posted @ 2023-10-08 18:29  前端张大碗  阅读(608)  评论(0编辑  收藏  举报