app-searchbar.vue 4.3 KB
Newer Older
1 2
<template>
    <div class="app-searchbar">
3
        <div class="app-searchbar-group" v-for="(item,index) in items">
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
            <el-select size="small" class="app-searchbar-field app-searchbar-item" v-model="item.field" clearable :placeholder="$t('components.filterTree.placeholder')">
                <el-option
                    v-for="field in fieldItems"
                    :key="field.value"
                    :label="field.label"
                    :value="field.value">
                </el-option>
            </el-select>
            <AppSearchMode class="app-searchbar-item" v-model="item.mode" :modes="getModes(item.field)" @on-change="onModeChange($event, item)"></AppSearchMode>
            <div class="app-searchbar-value app-searchbar-item">
                <i-input v-if="!item.editor"></i-input>
                <div v-else :key="item.editor">
                    <slot :data="item"></slot>
                </div>
                <div class="app-searchbar-action">
19
                    <img v-if="index === items.length - 1" src="assets/img/ic_add_circle.svg" @click="onAddItem()">
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
                    <img v-else src="assets/img/remove_circle.svg" @click="onRemoveItem(index)">
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts"> 
import {Vue, Component, Prop, Watch} from 'vue-property-decorator';
import AppSearchMode from './app-searchbar-mode.vue';

@Component({
    components: {
        AppSearchMode
    }
})
export default class AppSearchbar extends Vue {

    /**
     * 数据集
     *
     * @type {*}
     * @memberof AppSearchbar
     */
    @Prop() items: any;

    /**
     * 过滤项集合
     *
     * @type {*}
     * @memberof AppSearchbar
     */
    @Prop() fields: any;

54 55 56 57 58 59 60 61 62 63 64
    @Watch('items',{immediate: true})
    onItemsChange(newItems: any[]) {
        if (newItems.length == 0) {
            this.items.push({
                field: null,
                mode: null,
                editor: null
            })
        }
    }

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    /**
     * 属性项集合
     *
     * @type {*}
     * @memberof AppSearchbar
     */
    protected fieldItems: any[] = [];

    /**
     * 生命周期
     *
     * @return {void}
     * @memberof AppSearchbar
     */
    public created() {
        if(!this.fields) {
            return;
        }
        this.fields.forEach((field: any) => {
            let index: number = this.fieldItems.findIndex((item: any) => Object.is(item.value, field.prop));
            if(index < 0) {
                this.fieldItems.push({
                    label: field.label,
                    value: field.prop,
                    modes: this.getFieldModes(field.prop)
                })
            } 
        });
        
    }

    /**
     * 获取逻辑模式集合
     *
     * @return {void}
     * @memberof AppSearchbar
     */
    public getModes(field: string) {
        if(this.fieldItems.length > 0) {
            let item: any = this.fieldItems.find((item: any) => Object.is(item.value, field));
            if(item) {
                return item.modes;
            }
        }
        return [];
    }

    /**
     * 获取属性逻辑模式集合
     *
     * @return {void}
     * @memberof AppSearchbar
     */
    public getFieldModes(name: string) {
        let modes: any[] = [];
        for(let i = 0; i < this.fields.length; i++) {
            let field: any = this.fields[i];
            if(!Object.is(field.prop, name)) {
                continue;
            }
            modes.push({
                name: field.name,
                mode: field.mode ? field.mode : 'all'
            })
        }
        return modes;
    }

    /**
     * 添加条件
     *
     * @return {*}
     * @memberof AppSearchbar
     */
    public onAddItem(data: any) {
        this.items.push({
            field: null,
            mode: null,
            editor: null
        });
    }

    /**
     * 删除条件/组
     *
     * @return {*}
     * @memberof AppSearchbar
     */
    public onRemoveItem(index: number) {
        this.items.splice(index, 1);
    }

    /**
     * 条件逻辑变化
     *
     * @return {*}
     * @memberof AppSearchbar
     */
    public onModeChange(mode: any, data: any) {
        if(mode && data) {
            data.editor = mode.name;
        } 
    }
}
</script>

<style lang="scss">
@import './app-searchbar.scss';
</style>