app-upicker.vue 8.6 KB
Newer Older
1
<template>
2 3 4 5 6 7 8 9 10 11 12 13 14
    <div class='app-upicker'>
        <el-select ref="appUpicker" :value="refvalue" size='small' filterable
            @change="onSelect" :disabled="disabled" style='width:100%;' clearable
            @clear="onClear" @visible-change="onSelectOpen">
            <template v-if="items">
                <el-option v-for="(_item,index) in items" :key="index" :value="_item.value" :label="_item.label"></el-option>
            </template>
        </el-select>
		<span style='position: absolute;right: 5px;color: #c0c4cc;top:0;font-size: 13px;'>
            <i v-show="open" class='el-icon-arrow-up' @click="closeDropdown"></i> 
			<i v-show="!open" class='el-icon-arrow-down' @click="openDropdown"></i> 
        </span>
    </div>
15 16
</template>

17 18 19 20
<script lang = 'ts'>
import { Component, Vue, Prop, Model, Watch } from 'vue-property-decorator';
import { Subject } from 'rxjs';
import { AppModal } from '@/utils';
21

22 23 24
@Component({
})
export default class AppUpicker extends Vue {
25 26

	/**
27 28 29 30 31
	 * 请求到的数据
	 * @type {any[]}
	 * @memberof AppUpicker
	 */
	public itemList: any[] = []
32 33

	/**
34
	 * 列表项请求路径
35
	 *
36
	 * @type {string}
37 38
	 * @memberof AppUpicker
	 */
39
	public url: string = '';
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

	/**
	 * 请求参数和请求数据的映射关系
	 *
	 * @type {*}
	 * @memberof AppUpicker
	 */
	public interaction:any = {};

	/**
	 * 编辑器参数
	 *
	 * @type {*}
	 * @memberof AppUpicker
	 */
	@Prop() public itemParams?: any;

57 58 59 60 61 62 63
    /**
     * 视图上下文
     *
     * @type {*}
     * @memberof AppUpicker
     */
    @Prop() public context!: any;
64

65 66 67 68 69 70 71
    /**
     * 视图参数
     *
     * @type {*}
     * @memberof AppUpicker
     */
    @Prop() public viewparams!: any;
72

73 74 75 76 77 78 79
    /**
     * 表单数据
     *
     * @type {*}
     * @memberof AppUpicker
     */
    @Prop() public data!: any;
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 {string}
     * @memberof AppUpicker
     */
    @Prop() public name!: string;

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

    /**
     * 是否显示按钮
     *
     * @type {boolean}
     * @memberof AppUpicker
     */
    @Prop({default:true}) public showButton?: boolean;

    /**
     * 局部上下文导航参数
     * 
     * @type {any}
     * @memberof AppUpicker
     */
    @Prop() public localContext!:any;

    /**
     * 局部导航参数
     * 
     * @type {any}
     * @memberof AppUpicker
     */
    @Prop() public localParam!:any;

    /**
     * 值项名称
     *
     * @type {string}
     * @memberof AppUpicker
     */
    @Prop() public valueitem!: string;

    /**
     * 排序
     *
     * @type {string}
     * @memberof AppUpicker
     */
    @Prop() public sort?: string;

    /**
     * 值
     *
     * @type {*}
     * @memberof AppUpicker
     */
    @Model('change') public value?: any;

    /**
     * 当前值
     *
     * @type {string}
     * @memberof AppUpicker
     */
    public curvalue: string = '';

    /**
     * 下拉数组
     * @type {any[]}
     * @memberof AppUpicker
     */
    public items: any[] = [];

    /** 
     * 下拉图标指向状态管理
     * @type {boolean}
     * @memberof AppUpicker 
     */
    public open: boolean = false;

    /**
     * 输入状态
     *
     * @type {boolean}
     * @memberof AppUpicker
     */
    public inputState: boolean = false;
174 175 176 177

    /**
     * vue 生命周期
     *
178
     * @memberof AppUpicker
179
     */
180
    public created() {
181
		this.analysis(this.itemParams);
182 183 184 185 186 187 188 189 190 191 192 193 194
    }
    /**
     * 获取关联数据项值
     *
     * @readonly
     * @memberof AppUpicker
     */
    get refvalue() {
        if (this.valueitem && this.data) {
            return this.data[this.valueitem];
        }
        return this.curvalue;
    }
195

196 197
    /**
     * 展开下拉
198
     *
199
     * @memberof AppUpicker
200
     */
201 202 203 204 205 206
    public openDropdown() {
        const appUpicker: any = this.$refs.appUpicker;
        if(appUpicker) {
            appUpicker.focus();
        }
    }
207

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    /**
     * 收起下拉
     *
     * @memberof AppUpicker
     */
    public closeDropdown() {
        const appUpicker: any = this.$refs.appUpicker;
        if(appUpicker) {
            appUpicker.blur();
        }
    }

    /**
     * 下拉切换回调
     * @param flag 
     * 
     * @memberof AppUpicker
     */
    public onSelectOpen(flag: boolean): void {
        this.open = flag;
        if (this.open) {
			this.fectchItemList(this.url);
        }
    }

    /**
     * 下拉选中
     *
     * @param {string} val
     * @memberof AppUpicker
     */
    public onSelect(val: string) {
        let index = this.items.findIndex((item) => Object.is(item.value, val));
        if (index >= 0) {
            let item:any = this.items[index];
            if (this.valueitem) {
                this.$emit('formitemvaluechange', { name: this.valueitem, value: item.value });
            }
            if (this.name) {
                this.$emit('formitemvaluechange', { name: this.name, value: item.label });
            }
        }
    }

    /**
     * 清除
     * 
     * @memberof AppUpicker
     */
    public onClear($event: any): void {
        if (this.valueitem) {
            this.$emit('formitemvaluechange', { name: this.valueitem, value: '' });
        }
        if (this.name) {
            this.$emit('formitemvaluechange', { name: this.name, value: '' });
        }
        this.$forceUpdate();
    }

    /**
     * 值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppUpicker
     */
    @Watch('value',{immediate:true})
    public onValueChange(newVal: any, oldVal: any) {
        this.analysis(this.itemParams);
        if(newVal){
            this.curvalue = newVal;
            const value = this.data[this.valueitem];
            const index = this.items.findIndex((item: any) => Object.is(item.value, value));
            if (index !== -1) {
                return;
            }
            this.items = [];
            if (value) {
                this.items.push({text: newVal, value: value});
            }
        }
    }
	
291 292 293
	/**
	 * 解析编辑器参数
	 * @param {*} itemparams
294 295
     * 
	 * @memberof AppUpicker
296 297 298 299 300 301 302 303
	 */
    public analysis(itemparams:any) {
		Object.keys(itemparams).forEach((param)=>{
			if(param==='path'){
				this.url = itemparams[param]
			}else{
				this.interaction[param] = itemparams[param]
			}
304
        })
305 306 307 308 309
    }

	/**
	 * 请求下拉列表数据
	 * @param {string} url
310 311
     * 
	 * @memberof AppUpicker
312 313
	 */
	public fectchItemList(url:string) {
314 315
        let arg:any = {};
        this.handlePublicParams(arg);
316
		this.$http
317
			.get(url,arg.param)
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
			.then((response: any) => {
				if (response && response.status==200 && response.data) {
					this.itemList = response.data;
					// 提取需要的值(value,label)
					this.extractItem(this.itemList,this.items);
				}
			})
			.catch((response: any) => {
				if (!response || !response.status || !response.data) {
					this.$Notice.error({
						title: this.$t("app.commonWords.error") as string,
						desc: this.$t("app.commonWords.sysException") as string,
					});
					return;
				}
			});
	}

	/**
	 * 解析下拉列表数据
	 * @param {any[]} itemList 请求到的数据
	 * @param {any[]} items 下拉数组
340 341
     * 
     * @memberof AppUpicker
342 343
	 */
	public extractItem(itemList:any[],items:any[]) {
344
		this.items = [];
345
		itemList.forEach((item) => {
346
			this.items.push({
347 348 349
				label: item[this.interaction.label],
				value: item[this.interaction.value],
			});
350
        });
351 352
	}

353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    /**
     * 公共参数处理
     *
     * @param {*} arg
     * @returns
     * @memberof AppUpicker
     */
    public handlePublicParams(arg: any): boolean {
        if (!this.data) {
            this.$Notice.error({ title: (this.$t('components.appPicker.error') as any), desc: (this.$t('components.appPicker.formdataException') as any) });
            return false;
        }
        // 合并表单参数
        arg.param = this.viewparams ? JSON.parse(JSON.stringify(this.viewparams)) : {};
        arg.context = this.context ? JSON.parse(JSON.stringify(this.context)) : {};
        // 附加参数处理
        if (this.localContext && Object.keys(this.localContext).length >0) {
            let _context = this.$util.computedNavData(this.data,arg.context,arg.param,this.localContext);
            Object.assign(arg.context,_context);
        }
        if (this.localParam && Object.keys(this.localParam).length >0) {
            let _param = this.$util.computedNavData(this.data,arg.param,arg.param,this.localParam);
            Object.assign(arg.param,_param);
        }
        return true;
378 379 380 381 382 383
    }
}
</script>
<style lang="less">
@import './app-upicker.less';
</style>