app-group-select.vue 7.6 KB
Newer Older
JunZai's avatar
JunZai committed
1 2 3 4 5 6 7 8 9
<template>
    <div class="ibiz-group-select">
        <div class="ibiz-group-content">
            <span v-if="!multiple">
                {{ selectName }}
            </span>
            <template v-else v-for="(select, index) of selects">
                <div :key="index" class="ibiz-group-item">
                    {{ select.label }}
JunZai's avatar
JunZai committed
10
                    <i v-if="!disabled" class="el-icon-close" @click="remove(select)"></i>
JunZai's avatar
JunZai committed
11 12 13
                </div>
            </template>
        </div>
JunZai's avatar
JunZai committed
14 15
        <div v-if="!disabled" class="ibiz-group-open">
            <i v-if="!disabled && !multiple && selects.length > 0" class="el-icon-close" @click="remove(selects[0])"></i>
JunZai's avatar
JunZai committed
16 17 18 19 20 21 22 23 24 25
            <i class="el-icon-search" @click="openView"></i>
        </div>
    </div>
</template>

<script lang="ts">
import { Component, Vue, Prop, Watch } from 'vue-property-decorator';
import { Subject } from 'rxjs';

@Component({})
tony001's avatar
tony001 committed
26
export default class AppGroupSelect extends Vue {
JunZai's avatar
JunZai committed
27 28 29 30
    /**
     * 名称标识
     *
     * @type {*}
tony001's avatar
tony001 committed
31
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
32 33 34
     */  
    @Prop() name!: string;

35 36 37 38
    /**
     * 树加载地址
     *
     * @type {*}
tony001's avatar
tony001 committed
39
     * @memberof AppGroupSelect
40 41 42
     */  
    @Prop() treeurl?:boolean;

JunZai's avatar
JunZai committed
43 44 45 46
    /**
     * 数据接口地址
     *
     * @type {*}
tony001's avatar
tony001 committed
47
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
48
     */  
49
    @Prop() url!: string;
JunZai's avatar
JunZai committed
50 51 52 53 54

    /**
     * 多选
     *
     * @type {*}
tony001's avatar
tony001 committed
55
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
56 57 58 59 60 61 62
     */  
    @Prop({default: false}) multiple?: boolean;

    /**
     * 数据对象
     *
     * @type {*}
tony001's avatar
tony001 committed
63
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
64 65 66 67 68 69 70
     */  
    @Prop() data: any;

    /**
     * 过滤属性标识
     *
     * @type {*}
tony001's avatar
tony001 committed
71
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
72
     */  
JunZai's avatar
JunZai committed
73
    @Prop() filter?: string;
JunZai's avatar
JunZai committed
74 75 76 77 78

    /**
     * 是否启用
     *
     * @type {*}
tony001's avatar
tony001 committed
79
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
80 81 82 83 84 85 86
     */  
    @Prop() disabled?: boolean;

    /**
     * 值
     *
     * @type {*}
tony001's avatar
tony001 committed
87
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
88 89 90 91 92 93 94
     */  
    @Prop() value: any;

    /**
     * 上下文参数
     *
     * @type {*}
tony001's avatar
tony001 committed
95
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
96 97 98 99 100 101 102
     */  
    @Prop() context: any;

    /**
     * 关联属性
     *
     * @type {*}
tony001's avatar
tony001 committed
103
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
104 105 106
     */  
    @Prop() valueitem: any;

JunZai's avatar
JunZai committed
107 108 109 110
    /**
     * 填充属性
     *
     * @type {*}
tony001's avatar
tony001 committed
111
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
112 113 114
     */  
    @Prop() fillmap: any;

JunZai's avatar
JunZai committed
115 116 117 118
    /**
     * 选中项集合
     *
     * @type {*}
tony001's avatar
tony001 committed
119
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
120 121 122 123 124 125 126
     */  
    protected selects: any[] = [];

    /**
     * 值变化
     *
     * @type {*}
tony001's avatar
tony001 committed
127
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
128 129 130 131 132
     */  
    @Watch('value')
    onValueChange(newVal: any) {
        this.selects = [];
        if (newVal) {
JunZai's avatar
JunZai committed
133 134
            let item: any = {};
            item.label = newVal.split(',');
JunZai's avatar
JunZai committed
135
            if(this.valueitem) {
JunZai's avatar
JunZai committed
136 137 138 139 140 141
                item.id = this.data[this.valueitem] ? this.data[this.valueitem].split(',') : [];
            }
            if(this.fillmap) {
                for(let key in this.fillmap) {
                    item[this.fillmap[key]] = this.data[key] ? this.data[key].split(',') : [];
                }
JunZai's avatar
JunZai committed
142
            }
JunZai's avatar
JunZai committed
143 144 145 146 147 148
            item.label.forEach((val: string, index: number) => {
                let _item: any = {};
                for(let key in item) {
                    _item[key] = item[key][index] ? item[key][index] : null;
                }
                this.selects.push(_item)
JunZai's avatar
JunZai committed
149 150 151 152 153 154 155 156
            })
        }
    }

    /**
     * 单选时选中名称
     *
     * @type {*}
tony001's avatar
tony001 committed
157
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
158 159 160 161 162 163 164 165 166 167 168
     */  
    get selectName() {
        if(this.selects.length > 0) {
            return this.selects[0].label;
        }
    }

    /**
     * 打开选择视图
     *
     * @type {*}
tony001's avatar
tony001 committed
169
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
170 171 172
     */  
    public openView() {
        const view: any = {
173
            viewname: 'app-group-picker',
JunZai's avatar
JunZai committed
174 175 176
            title: '分组选择'
        };
        const context: any = JSON.parse(JSON.stringify(this.context));
tony001's avatar
tony001 committed
177 178 179 180 181 182 183 184 185
        let filtervalue:string = "";
        if(this.filter){
            if(this.data[this.filter]){
                filtervalue = this.data[this.filter];
            }else if(context[this.filter]){
                filtervalue = context[this.filter];
            }else{
                filtervalue = context.srforgid;
            }
186 187
        }else{
            filtervalue = context.srforgid;
tony001's avatar
tony001 committed
188
        }
JunZai's avatar
JunZai committed
189 190
        const param: any = {};
        Object.assign(param, {
191 192 193
            showtree: this.treeurl?true:false,
            url:this.url,
            treeurl:this.treeurl,
tony001's avatar
tony001 committed
194
            filtervalue: filtervalue,
JunZai's avatar
JunZai committed
195
            multiple: this.multiple,
JunZai's avatar
JunZai committed
196
            selects: this.selects
JunZai's avatar
JunZai committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210
        });
        let container: Subject<any> = this.$appmodal.openModal(view, context, param);
        container.subscribe((result: any) => {
            if (!result || !Object.is(result.ret, 'OK')) {
                return;
            }
            this.openViewClose(result);
        });
    }

    /**
     * 选择视图关闭
     *
     * @type {*}
tony001's avatar
tony001 committed
211
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
212 213 214 215
     */  
    public openViewClose(result: any) {
        this.selects = [];
        if (result.datas && result.datas.length > 0) {
JunZai's avatar
JunZai committed
216
            this.selects = result.datas
JunZai's avatar
JunZai committed
217 218 219 220 221 222 223 224
        }
        this.setValue()
    }

    /**
     * 数据删除
     *
     * @type {*}
tony001's avatar
tony001 committed
225
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
226 227 228 229 230 231 232 233 234 235
     */  
    public remove(item: any) {
        this.selects.splice(this.selects.indexOf(item), 1);
        this.setValue()
    }

    /**
     * 设置值
     *
     * @type {*}
tony001's avatar
tony001 committed
236
     * @memberof AppGroupSelect
JunZai's avatar
JunZai committed
237 238 239
     */  
    public setValue() {
        let item: any = {};
JunZai's avatar
JunZai committed
240 241 242 243 244 245 246 247 248
        item[this.name] = null;
        if(this.valueitem) {
            item[this.valueitem] = null;
        }
        if(this.fillmap) {
            for(let key in this.fillmap) {
                item[key] = null;
            }
        }
JunZai's avatar
JunZai committed
249 250
        if(this.multiple) {
            this.selects.forEach((select: any) => {
JunZai's avatar
JunZai committed
251 252 253 254 255 256 257 258 259
                item[this.name] = item[this.name] ? `${item[this.name]},${select.label}` : select.label;
                if(this.valueitem) {
                    item[this.valueitem] = item[this.valueitem] ? `${item[this.valueitem]},${select.id}` : select.id;
                }
                if(this.fillmap) {
                    for(let key in this.fillmap) {
                        item[key] = item[key] ? `${item[key]},${select[this.fillmap[key]]}` : select[this.fillmap[key]];
                    }
                }
JunZai's avatar
JunZai committed
260 261 262
            });
        } else {
            item = this.selects.length > 0 ? this.selects[0] : {};
JunZai's avatar
JunZai committed
263 264 265 266 267 268 269 270 271
            item[this.name] = this.selects.length > 0 ? this.selects[0].label : null;
            if(this.valueitem) {
                item[this.valueitem] = this.selects.length > 0 ? this.selects[0].id : null;
            }
            if(this.fillmap) {
                for(let key in this.fillmap) {
                    item[key] = this.selects.length > 0 ? this.selects[0][this.fillmap[key]] : null;
                }
            }
JunZai's avatar
JunZai committed
272
        }
JunZai's avatar
JunZai committed
273 274
        for(let key in item) {
            this.$emit('formitemvaluechange', { name: key, value: item[key] });
JunZai's avatar
JunZai committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
        }
    } 
}
</script>

<style lang="less">
.ibiz-group-select {
    width: 100%;
    display: flex;
    border: 1px solid #DCDFE6;
    min-height: 32px;
    border-radius: 4px;
    .ibiz-group-content {
        flex-grow: 1;
        padding: 0 16px;
        .ibiz-group-item {
            display: inline-block;
            border: 1px solid #bbb;
            line-height: 24px;
            border-radius: 5px;
            margin-right: 5px;
            padding: 0 5px;
        }
    }
    .ibiz-group-open {
        display: flex;
        text-align: center;
        align-items: center;
JunZai's avatar
JunZai committed
303
        padding: 0 5px;
JunZai's avatar
JunZai committed
304 305 306 307 308 309
    }
}
.ibiz-group-select:hover {
    border-color: #108cee;
}
</style>