app-autocomplete.vue 7.2 KB
Newer Older
ibizdev's avatar
ibizdev 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
<template>
    <el-autocomplete 
      class='text-value' 
      :value-key="deMajorField" 
      :disabled="disabled" 
      v-model="curvalue" 
      size='small'
      :trigger-on-focus="true" 
      :fetch-suggestions="onSearch" 
      @select="onACSelect"
      @input="onInput" 
      @blur="onBlur" 
      style='width:100%;'>
        <template v-slot:suffix>
            <i v-if="curvalue && !disabled" class='el-icon-circle-close' @click="onClear"></i>
            <i class="el-icon-arrow-down"></i>
        </template>
    </el-autocomplete>
</template>

<script lang='ts'>
import { Component, Vue, Prop, Model, Watch } from 'vue-property-decorator';
import { Subject } from 'rxjs';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';

ibizdev's avatar
ibizdev committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
@Component({
    i18n: {
        messages: {
            'zh-CN': {
                errorInfo: {
                    error: '错误',
                    miss: '缺少参数',
                    requestException: '请求异常!',
                }
            },
            'en-US': {
                errorInfo: {
                    error: 'Error',
                    miss: 'Missing parameter ',
                    requestException: 'Request Exception!',
                }
            }
        }
    }
})
ibizdev's avatar
ibizdev committed
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 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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
export default class AppAutocomplete extends Vue {

    /**
     * 表单数据
     *
     * @type {*}
     * @memberof AppAutocomplete
     */
    @Prop() public data: any;

    /**
     * 视图上下文
     *
     * @type {*}
     * @memberof AppAutocomplete
     */
    @Prop() public context!: any;

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof AppFormDRUIPart
     */
    @Prop() public viewparams!: any;

    /**
     * AC参数
     *
     * @type {*}
     * @memberof AppFormDRUIPart
     */
    @Prop({default: {}}) public acParams?: any;

    /**
     * 表单服务
     *
     * @type {*}
     * @memberof AppFormDRUIPart
     */
    @Prop() public service?: any;

    /**
     * 应用实体主信息属性名称
     *
     * @type {string}
     * @memberof AppAutocomplete
     */
    @Prop({default: 'srfmajortext'}) public deMajorField!: string;

    /**
     * 应用实体主键属性名称
     *
     * @type {string}
     * @memberof AppAutocomplete
     */
    @Prop({default: 'srfkey'}) public deKeyField!: string;

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

    /**
     * 属性项名称
     *
     * @type {string}
     * @memberof AppAutocomplete
     */
    @Prop() public name!: string;

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

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

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

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

    /**
     * 远程请求url 地址
     *
     * @type {string}
     * @memberof AppAutocomplete
     */
    @Prop() public url?: string;

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

    /**
     * 输入状态
     *
     * @type {boolean}
     * @memberof AppAutocomplete
     */
    public inputState: boolean = false;

    /**
     * 值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
     * @memberof AppPicker
     */
    @Watch('value')
    public onValueChange(newVal: any, oldVal: any) {
        this.curvalue = newVal;
    }

    /**
     * 执行搜索数据
     * @param query 
     * @param callback 
     */
    public onSearch(query: any, callback: any): void {
        // 处理搜索参数
        query = !query ? '' : query;
        if (!this.inputState && Object.is(query, this.value)) {
            query = '';
        }
        this.inputState = false;
        // 合并视图上下文参数和视图参数
        let param: any = JSON.parse(JSON.stringify(this.viewparams));
        let context: any = JSON.parse(JSON.stringify(this.context));
        Object.assign(param, { query: query });
        // 附加参数处理
        if (this.itemParam.context) {
            let _context = this.$util.formatData(this.data,this.itemParam.context);
            Object.assign(context,_context);
        }
        if (this.itemParam.param) {
            let _param = this.$util.formatData(this.data,this.itemParam.param);
            Object.assign(param,_param);
        }
        if (this.itemParam.parentdata) {
            let _parentdata = this.$util.formatData(this.data,this.itemParam.parentdata);
            Object.assign(param,_parentdata);
        }
ibizdev's avatar
ibizdev committed
217 218 219 220 221
        // 错误信息国际化
        let error: string = (this.$t('errorInfo.error') as any);
        let miss: string = (this.$t('errorInfo.miss') as any);
        let requestException: string = (this.$t('errorInfo.requestException') as any);

ibizdev's avatar
ibizdev committed
222
        if(!this.service){
ibizdev's avatar
ibizdev committed
223
            this.$Notice.error({ title: error, desc: miss+'service' });
ibizdev's avatar
ibizdev committed
224
        } else if(!this.acParams.serviceName) {
ibizdev's avatar
ibizdev committed
225
            this.$Notice.error({ title: error, desc: miss+'serviceName' });
ibizdev's avatar
ibizdev committed
226
        } else if(!this.acParams.interfaceName) {
ibizdev's avatar
ibizdev committed
227
            this.$Notice.error({ title: error, desc: miss+'interfaceName' });
ibizdev's avatar
ibizdev committed
228 229 230
        } else {
          this.service.getItems(this.acParams.serviceName,this.acParams.interfaceName, context, param).then((response: any) => {
              if (!response) {
ibizdev's avatar
ibizdev committed
231
                  this.$Notice.error({ title: error, desc: requestException });
ibizdev's avatar
ibizdev committed
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 291 292 293 294 295 296 297 298 299 300
              } else {
                  this.items = [...response];
              }
              if (callback) {
                  callback(this.items);
              }
          }).catch((error: any) => {
              if (callback) {
                  callback([]);
              }
          });
        } 
    }

    /**
     * 选中数据回调
     * @param item 
     */
    public onACSelect(item: any): void {
      if (this.name) {
          this.$emit('formitemvaluechange', { name: this.name, value: item[this.deMajorField] });
      }
      if (this.valueitem) {
          this.$emit('formitemvaluechange', { name: this.valueitem, value: item[this.deKeyField] });
      }
  }

    /**
     * 输入过程中
     *
     * @memberof AppAutocomplete
     */
    public onInput($event: any) {
        if (Object.is($event, this.value)) {
            this.inputState = true;
        }
    }

    /**
     * 失去焦点事件
     * @param e 
     */
    public onBlur(e: any): void {
        let val: string = e.target.value;
        if (!Object.is(val, this.value)) {
            this.onACSelect({ [this.deMajorField]: val, [this.deKeyField]: '' });
        }
        this.$forceUpdate();
    }

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

}
</script>

<style lang='less'>
@import './app-autocomplete.less';
</style>