ibiz-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 26 27 28 29 30 31 32 33 34
            <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({})
export default class IBizGroupSelect extends Vue {
    /**
     * 名称标识
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    @Prop() name!: string;

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

JunZai's avatar
JunZai committed
43 44 45 46 47 48
    /**
     * 数据接口地址
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
49
    @Prop() url!: string;
JunZai's avatar
JunZai committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    /**
     * 多选
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    @Prop({default: false}) multiple?: boolean;

    /**
     * 数据对象
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    @Prop() data: any;

    /**
     * 过滤属性标识
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
JunZai's avatar
JunZai committed
73
    @Prop() filter?: string;
JunZai's avatar
JunZai committed
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

    /**
     * 是否启用
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    @Prop() disabled?: boolean;

    /**
     * 值
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    @Prop() value: any;

    /**
     * 上下文参数
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    @Prop() context: any;

    /**
     * 关联属性
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    @Prop() valueitem: any;

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

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

    /**
     * 值变化
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    @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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            })
        }
    }

    /**
     * 单选时选中名称
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    get selectName() {
        if(this.selects.length > 0) {
            return this.selects[0].label;
        }
    }

    /**
     * 打开选择视图
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    public openView() {
        const view: any = {
            viewname: 'ibiz-group-picker',
            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 211 212 213 214 215
        });
        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 {*}
     * @memberof IBizGroupSelect
     */  
    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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        }
        this.setValue()
    }

    /**
     * 数据删除
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    public remove(item: any) {
        this.selects.splice(this.selects.indexOf(item), 1);
        this.setValue()
    }

    /**
     * 设置值
     *
     * @type {*}
     * @memberof IBizGroupSelect
     */  
    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>