import { Component, Vue, Prop, Watch } from 'vue-property-decorator'; import './app-mpicker.less'; import { Subject } from 'rxjs'; import { AppModal } from '@/utils'; @Component({}) export default class AppMpicker extends Vue { /** * 传入url */ @Prop() url?: any; /** * 表单数据 */ @Prop() activeData?: any; /** * 是否禁用 */ @Prop() disabled?: boolean; /** * 编辑器参数 */ @Prop() editorParams?: any; /** * 表单项值 */ @Prop() curvalue?: any; /** * 表单项名称 */ @Prop() name: any; /** * 打开对应的选择视图 */ @Prop() pickupView?: any; /** * 当前表单项绑定值key的集合 */ public value: any; /** * 所有操作过的下拉选选项 */ public items: Array = []; /** * 选中项key-value键值对 * */ public selectItems: Array = []; /** * 监听curvalue值 * @param newVal * @param val */ @Watch('curvalue', { deep: true }) oncurvalueChange(newVal: any, val: any) { this.value = []; this.selectItems = []; if (newVal) { this.selectItems = JSON.parse(newVal); this.selectItems.forEach((item: any) => { 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 }); } }); } this.$forceUpdate(); } /** * 远程执行搜索 * * @param {*} query * @memberof AppMpicker */ public onSearch(query: any) { if (this.url) { let param: any = { srfaction: 'itemfetch', query: query }; if (this.activeData) { Object.assign(param, { srfreferdata: this.activeData }); } this.$http.post(`${this.url}${this.name}/ac`, param).then((data: any) => { if(data.items){ this.items = data.items; } }) } } /** * 下拉选中回调 * * @param {*} selects * @memberof AppMpicker */ public onSelect(selects: any) { let val: Array = []; if (selects.length > 0) { selects.forEach((select: any) => { 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: any) => Object.is(item.srfkey, select)); if (index >= 0) { let item = this.selectItems[index]; val.push(item); } } }); let value = val.length > 0 ? JSON.stringify(val) : ''; this.$emit('formitemvaluechange', { name: this.name, value: value }); } } /** * 移除标签回调 * * @param {*} tag * @memberof AppMpicker */ public onRemove(tag: any) { let index = this.selectItems.findIndex((item: any) => Object.is(item.srfkey, tag)); if (index >= 0) { this.selectItems.splice(index, 1); let value = this.selectItems.length > 0 ? JSON.stringify(this.selectItems) : ''; this.$emit('formitemvaluechange', { name: this.name, value: value }); } } /** * 打开视图 * * @returns * @memberof AppMpicker */ public openView() { if (this.disabled) { return; } let data = { srfparentdata: { srfparentkey: this.activeData.srfkey }, selectedData: [...this.selectItems], }; if (this.pickupView && Object.keys(this.pickupView).length > 0) { const view = { ...this.pickupView }; const modal: Subject = AppModal.getInstance().openModal(view, data); modal.subscribe((result: any) => { if (!result || !Object.is(result.ret, 'OK')) { return; } let selects: Array = []; if (result.datas && Array.isArray(result.datas)) { result.datas.forEach((select: any) => { 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.name && this.activeData) { let value = selects.length > 0 ? JSON.stringify(selects) : ''; this.$emit('formitemvaluechange', { name: this.name, value: value }); } }) } } /** * 渲染组件 * * @returns * @memberof AppMpicker */ public render() { return (
{this.items.map((item: any) => { return })}
); } }