ibiz-mpicker.component.ts 5.3 KB
Newer Older
ibiz's avatar
ibiz committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 54 55 56 57 58 59 60 61 62 63 64 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
Vue.component("ibiz-mpicker", {
    template: `
    <div style="position: relative;width: 100%;">
        <el-select :value="value" multiple filterable remote :remote-method="onSearch" size="small" style="width:100%;" @change="onSelect" @remove-tag="onRemove" :disabled="field.disabled">
            <el-option v-for="item in items" :label="item.text" :value="item.value"></el-option>
        </el-select>
        <span style="position: absolute;right: 5px;color: #c0c4cc;top: 0;font-size: 13px;">
            <i class="el-icon-search"  @click="openView"></i>
        </span>
    </div>
    `,
    props: ['field', 'name'],
    data: function () {
        let data: any = {
            http: IBizHttp.getInstance(),
            items: [],
            value: [],
            selectItems: []
        };
        Object.assign(data, this.field.editorParams);
        Object.assign(data, { form: this.field.getForm() })
        return data;
    },
    mounted: function () {
        // this.onSearch('');
    },
    watch: {
        'field.value': function (newVal, oldVal) {
            this.value = [];
            this.selectItems = [];
            if (newVal) {
                this.selectItems = JSON.parse(newVal);
                this.selectItems.forEach(item => {
                    this.value.push(item.srfkey);
                    let index = this.items.findIndex((i) => Object.is(i.value, item.srfkey));
                    if (index < 0) {
                        this.items.push({ text: item.srfmajortext, value: item.srfkey })
                    }
                });
            }
        }
    },
    methods: {
        'onSelect': function (selects) {
            let val: Array<any> = [];
            selects.forEach(select => {
                let index = this.items.findIndex((item) => Object.is(item.value, select));
                if (index >= 0) {
                    let item = this.items[index];
                    val.push({ srfkey: item.value, srfmajortext: item.text });
                } else {
                    index = this.selectItems.findIndex((item) => Object.is(item.srfkey, select));
                    if (index >= 0) {
                        let item = this.selectItems[index];
                        val.push(item);
                    }
                }
            });
            if (this.field) {
                let value = val.length > 0 ? JSON.stringify(val) : '';
                this.field.setValue(value);
            }
        },
        'onRemove': function (tag) {
            let index = this.selectItems.findIndex((item) => Object.is(item.value, tag));
            if (index >= 0) {
                this.selectItems.splice(index, 1);
                let value = this.selectItems.length > 0 ? JSON.stringify(this.selectItems) : '';
                if (this.field) {
                    this.field.setValue(value);
                }
            }
        },
        'onSearch': function (query) {
            if (this.url) {
                let param: any = {
                    srfaction: 'itemfetch',
                    query: query
                };
                if (this.form) {
                    Object.assign(param, { srfreferdata: JSON.stringify(this.form.getActiveData()) });
                }
                this.http.post(this.url, param).subscribe((data) => {
                    this.items = data.items;
                })
            }
        },
        'openView': function () {
            if (this.field && this.field.disabled) {
                return;
            }
            let view = { viewparam: {} };
            let viewController: any;
            if (this.form) {
                viewController = this.form.getViewController();
                Object.assign(view.viewparam, {
                    srfreferdata: JSON.stringify(this.form.getActiveData()),
                    selectedData: [...this.selectItems]
                });
            }

            if (viewController) {
                Object.assign(view.viewparam, viewController.getViewParam());
            }

            if (this.pickupView && Object.keys(this.pickupView).length > 0) {
                const subject: Subject<any> = new rxjs.Subject();
                Object.assign(view, this.pickupView, { subject: subject });
                this.$root.addModal(view);
                subject.subscribe((result: any) => {
                    if (!result || !Object.is(result.ret, 'OK')) {
                        return;
                    }
                    let selects: Array<any> = [];
                    if (result.selections && Array.isArray(result.selections)) {
                        result.selections.forEach(select => {
                            selects.push({ srfkey: select.srfkey, srfmajortext: select.srfmajortext });
                            let index = this.items.findIndex((item) => Object.is(item.value, select.srfkey));
                            if (index < 0) {
                                this.items.push({ text: select.srfmajortext, value: select.srfkey });
                            }
                        });
                    }
                    if (this.field) {
                        let value = selects.length > 0 ? JSON.stringify(selects) : '';
                        this.field.setValue(value);
                    }
                })
            }
        }
    }
})