CONTROL-BASE.vue.ftl 16.6 KB
Newer Older
1 2
<template>  
    <div :class="['app-list', this.items.length > 0 ? '' : 'app-list-empty' ]">
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
        <div v-if="items.length > 0" style="height:100%;">
<#if ctrl.render??>
            ${ctrl.render.code}
<#else>                 
            <div v-for = "item in items" :key="item.srfmajortext"  :class="['app-list-item', {'isSelect': item.isselected === true ? true : false}]" @click="handleClick(item)"  @dblclick="handleDblClick(item)">
  <#if ctrl.getItemPSLayoutPanel()??>
    <#assign panel = ctrl.getItemPSLayoutPanel()>
                <@ibizindent blank=16>
                ${P.getCtrlCode(panel, 'CONTROL.html').code}
                </@ibizindent>
  <#elseif ctrl.itemRender??>
                ${ctrl.itemRender.code}
  <#else>
                {{item.srfmajortext}}
  </#if>
18
            </div>
19 20 21 22 23
</#if>
        </div>
        <div v-else>
            暂无数据
        </div>
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
    </div>
</template>
<#ibizinclude>
../@MACRO/CONTROL/CONTROL_HEADER-BASE.vue.ftl
</#ibizinclude>

    /**
     * 获取多项数据
     *
     * @returns {any[]}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public getDatas(): any[] {
        return this.selections;
    }

    /**
     * 获取单项树
     *
     * @returns {*}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public getData(): any {
        return null;
    }

    /**
     * 是否默认选中第一条数据
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
56
    @Prop({ default: false }) public isSelectFirstDefault!: boolean;
57 58 59 60 61 62 63

    /**
     * 显示处理提示
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
64
    @Prop({ default: true }) public showBusyIndicator?: boolean;
65 66 67 68 69 70 71

    /**
     * 部件行为--create
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
72
    @Prop() public createAction!: string;
73 74 75 76 77 78 79

    /**
     * 部件行为--remove
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
80
    @Prop() public removeAction!: string;
81 82 83 84 85 86 87

    /**
     * 部件行为--update
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
88
    @Prop() public updateAction!: string;
89 90 91 92 93 94 95

    /**
     * 部件行为--fetch
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
96
    @Prop() public fetchAction!: string;
97

tony001's avatar
tony001 committed
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
<#if view.getPSAppViewLogics?? && view.getPSAppViewLogics()??>
  <#list view.getPSAppViewLogics() as logic>
    <#if logic.getPFLogicCodeType() == 'APP_NEWDATA'>
    /**
     * 打开新建数据视图
     *
     * @type {any}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    @Prop() public newdata: any;
    </#if>
    <#if logic.getPFLogicCodeType() == 'APP_OPENDATA'>
    /**
     * 打开编辑数据视图
     *
     * @type {any}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    @Prop() public opendata: any;
    </#if>
  </#list>
</#if>

    /**
     * this引用
     *
     * @type {number}
125
     * @memberof ${srfclassname('${ctrl.codeName}')}
tony001's avatar
tony001 committed
126 127 128
     */
    public thisRef: any = this;

129 130 131 132
    /**
     * 当前页
     *
     * @type {number}
133
     * @memberof ${srfclassname('${ctrl.codeName}')}
134
     */
135
    public curPage: number = 1;
136 137 138 139 140 141 142

    /**
     * 数据
     *
     * @type {any[]}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
143
    public items: any[] = [];
144 145 146 147 148 149 150

    /**
     * 是否支持分页
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
151
    public isEnablePagingBar: boolean = true;
152 153 154 155 156 157 158

    /**
     * 分页条数
     *
     * @type {number}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
tony001's avatar
tony001 committed
159
    public limit: number = ${ctrl.getPagingSize()?c};
160 161 162 163 164 165 166

    /**
     * 总条数
     *
     * @type {number}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
167
    public totalRecord: number = 0;
168

tony001's avatar
tony001 committed
169 170 171 172 173 174 175 176
    /**
     * 加载的数据是否附加在items之后
     *
     * @type {boolean}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public isAddBehind:boolean = false;

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    /**
     * 排序方向
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */    
    public sortDir:string = '<#if ctrl.getMinorSortDir()??>${ctrl.getMinorSortDir()?lower_case}</#if>';

    /**
     * 排序字段
     *
     * @type {string}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */    
    public sortField: string = '<#if ctrl.getMinorSortPSDEF()??>${ctrl.getMinorSortPSDEF().getCodeName()?lower_case}</#if>';

193 194 195 196 197
    /**
     * 选中数组
     * @type {Array<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
198
    public selections: Array<any> = [];
199 200 201 202 203 204

     /**
     * Vue声明周期,组件挂载完毕
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
205
    public mounted () {
206 207 208 209 210 211 212 213
        this.afterMounted();
    }

    /**
     * 执行mounted后的逻辑
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
214
    public afterMounted () {
215 216 217 218 219 220
        this.$el.addEventListener('scroll', ()=> {
            if( this.$el.scrollTop +  this.$el.clientHeight  >=  this.$el.scrollHeight) {
                this.loadMore();
            }
        })
    }
tony001's avatar
tony001 committed
221
    
222 223 224 225 226 227

    /**
     * Vue声明周期,组件创建完毕
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
228
    public created() {
229 230 231 232 233 234 235 236
        this.afterCreated()
    }

    /**
     * 执行created后的逻辑
     *
     *  @memberof ${srfclassname('${ctrl.codeName}')}
     */    
237
    public afterCreated(){
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        if (this.viewState) {
            this.viewStateEvent = this.viewState.subscribe(({ tag, action, data }) => {
                if (!Object.is(this.name, tag)) {
                    return;
                }
                if (Object.is(action,'load')) {
                    this.refresh(data)
                }
            });
        }
    }    

    /**
     * vue 生命周期
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
255
    public destroyed() {
256 257 258 259 260 261 262 263
        this.afterDestroy();
    }

    /**
     * 执行destroyed后的逻辑
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
264
    public afterDestroy() {
265 266 267 268 269 270 271 272 273 274 275 276 277
        if (this.viewStateEvent) {
            this.viewStateEvent.unsubscribe();
        }
        <#if destroyed_block??>
        ${destroyed_block}
        </#if>
    }    

    /**
    * 加载更多
    *
    * @memberof Mob
    */
278
    public loadMore(){
279 280 281
        if(this.totalRecord>this.items.length)
        {
            this.curPage = ++this.curPage;
tony001's avatar
tony001 committed
282
            this.isAddBehind = true;
283 284 285 286 287 288 289 290 291 292
            this.load({});
        }
    }

    /**
     * 刷新
     *
     * @param {*} [opt={}]
     * @memberof Main
     */
293
    public refresh(opt: any = {}) {
294 295 296 297 298 299 300 301
        this.curPage = 1;
        this.items = [];
        this.load(opt);
    }

    /**
     * 列表数据加载
     *
302
     * @public
303 304 305
     * @param {*} [arg={}]
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
306
    public load(opt: any = {}): void {
307 308 309 310 311 312 313 314 315
        if(!this.fetchAction){
            this.$Notice.error({ title: '错误', desc: '${view.getName()}视图列表fetchAction参数未配置' });
            return;
        }        
        const arg: any = {...opt};
        const page: any = {};
        if (this.isEnablePagingBar) {
            Object.assign(page, { page: this.curPage-1, size: this.limit });
        }
316 317 318 319 320
        // 设置排序
        if (!Object.is(this.sortDir, '') && !Object.is(this.sortField, '')) {
          const sort: string = this.sortField+","+this.sortDir;
            Object.assign(page, { sort: sort });
        }
321 322 323 324
        Object.assign(arg, page);
        const parentdata: any = {};
        this.$emit('beforeload', parentdata);
        Object.assign(arg, parentdata);
325
        let tempViewParams:any = parentdata.viewparams?parentdata.viewparams:{};
tony001's avatar
tony001 committed
326 327 328
        if(this.viewparams){
            Object.assign(tempViewParams,JSON.parse(JSON.stringify(this.viewparams)));
        }
329
        Object.assign(arg,{viewparams:tempViewParams});
330 331 332 333 334 335 336 337 338
        const post: Promise<any> = this.service.search(this.fetchAction, this.context?JSON.parse(JSON.stringify(this.context)):{}, arg, this.showBusyIndicator);
        post.then((response: any) => {
            if (!response || response.status !== 200) {
                if (response.errorMessage) {
                    this.$Notice.error({ title: '错误', desc: response.errorMessage });
                }
                return;
            }
            const data: any = response.data;
tony001's avatar
tony001 committed
339 340 341
            if(!this.isAddBehind){
                this.items = [];
            }
342 343 344 345 346 347 348 349
            if (Object.keys(data).length > 0) {
                let datas = JSON.parse(JSON.stringify(data));
                datas.map((item: any) => {
                    Object.assign(item, { isselected: false });
                });
                this.totalRecord = response.total;
                this.items.push(...datas);
            }
tony001's avatar
tony001 committed
350
            this.isAddBehind = false;
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
            this.$emit('load', this.items);
            if(this.isSelectFirstDefault){
                this.handleClick(this.items[0]);
            }
        }, (response: any) => {
            if (response && response.status === 401) {
                return;
            }
            this.$Notice.error({ title: '错误', desc: response.errorMessage });
        });
    }

    /**
     * 删除
     *
     * @param {any[]} datas
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
370
    public async remove(datas: any[]): Promise<any> {
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        if(!this.removeAction){
            this.$Notice.error({ title: '错误', desc: '${view.getName()}视图表格removeAction参数未配置' });
            return;
        }
        if (datas.length === 0) {
            return;
        }
        let dataInfo = '';
        datas.forEach((record: any, index: number) => {
            let srfmajortext = record.srfmajortext;
            if (index < 5) {
                if (!Object.is(dataInfo, '')) {
                    dataInfo += '、';
                }
                dataInfo += srfmajortext;
            } else {
                return false;
            }
        });

        if (datas.length < 5) {
            dataInfo = dataInfo + ' 共' + datas.length + '条数据';
        } else {
            dataInfo = dataInfo + '...' + ' 共' + datas.length + '条数据';
        }

        const removeData = () => {
            let keys: any[] = [];
            datas.forEach((data: any) => {
400
                keys.push(data.srfkey); 
401
            });
WodahsOrez's avatar
WodahsOrez committed
402
            let _removeAction = keys.length > 1 ? 'removeBatch' : this.removeAction ;
403
            const context:any = JSON.parse(JSON.stringify(this.context));
404
            const post: Promise<any> = this.service.delete(_removeAction,Object.assign(context,{ ${ctrl.getPSAppDataEntity().codeName?lower_case}: keys.join(';') }),Object.assign({ ${ctrl.getPSAppDataEntity().codeName?lower_case}: keys.join(';') },{viewparams:this.viewparams}), this.showBusyIndicator);
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
            return new Promise((resolve: any, reject: any) => {
                post.then((response: any) => {
                    if (!response || response.status !== 200) {
                        this.$Notice.error({ title: '', desc: '删除数据失败,' + response.info });
                        return;
                    } else {
                        this.$Notice.success({ title: '', desc: '删除成功!' });
                    }
                    //删除items中已删除的项
                    datas.forEach((data: any) => {
                      this.items.some((item:any,index:number)=>{
                        if(Object.is(item.srfkey,data.srfkey)){
                          this.items.splice(index,1);
                                return true;
                            }
                        });
                    });
                    this.$emit('remove', null);
                    this.selections = [];
                    resolve(response);
                }).catch((response: any) => {
                    if (response && response.status === 401) {
                        return;
                    }
                    if (!response || !response.status || !response.data) {
                        this.$Notice.error({ title: '错误', desc: '系统异常' });
                        reject(response);
                        return;
                    }
                    reject(response);
                });
            });
        }

        dataInfo = dataInfo.replace(/[null]/g, '').replace(/[undefined]/g, '').replace(/[ ]/g, '');
        this.$Modal.confirm({
            title: '警告',
            content: '确认要删除 ' + dataInfo + ',删除操作将不可恢复?',
            onOk: () => {
                removeData();
            },
            onCancel: () => { }
        });
        return removeData;
    }

451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
    /**
     * 保存
     *
     * @param {*} $event
     * @returns {Promise<any>}
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public async save(args: any[], params?: any, $event?: any, xData?: any){
        let _this = this;
        let successItems:any = [];
        let errorItems:any = [];
        let errorMessage:any = [];
        for (const item of _this.items) {
            try {
                if(Object.is(item.rowDataState, 'create')){
                    if(!this.createAction){
                        this.$Notice.error({ title: '错误', desc: '${view.getName()}视图列表createAction参数未配置' });
                    }else{
                      Object.assign(item,{viewparams:this.viewparams});
                      let response = await this.service.add(this.createAction, JSON.parse(JSON.stringify(this.context)),item, this.showBusyIndicator);
                      successItems.push(JSON.parse(JSON.stringify(response.data)));
                    }
                }else if(Object.is(item.rowDataState, 'update')){
                    if(!this.updateAction){
                        this.$Notice.error({ title: '错误', desc: '${view.getName()}视图列表updateAction参数未配置' });
                    }else{
                        Object.assign(item,{viewparams:this.viewparams});
<#if de??>
                        if(item.${appde.getCodeName()?lower_case}){
                            Object.assign(this.context,{${appde.getCodeName()?lower_case}:item.${appde.getCodeName()?lower_case}});
                        }
</#if>
                        let response = await this.service.add(this.updateAction,JSON.parse(JSON.stringify(this.context)),item, this.showBusyIndicator);
                        successItems.push(JSON.parse(JSON.stringify(response.data)));
                    }
                }
            } catch (error) {
                errorItems.push(JSON.parse(JSON.stringify(item)));
                errorMessage.push(error);
            }
        }
        this.$emit('save', successItems);
        this.refresh();
        if(errorItems.length === 0){
            this.$Notice.success({ title: '', desc: '保存成功!' });
        }else{
          errorItems.forEach((item:any,index:number)=>{
            this.$Notice.error({ title: '保存失败', desc: item.majorentityname+'保存失败!' });
            console.error(errorMessage[index]);
          });
        }
        return successItems;
    }

505 506 507 508 509 510 511 512 513 514 515
    /**
     * 面板数据变化处理事件
     * @param {any} item 当前列数据
     * @param {any} $event 面板事件数据
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
    public onPanelDataChange(item:any,$event:any) {
        Object.assign(item, $event, {rowDataState:'update'});
    }

516 517 518 519 520
    /**
     * 选择数据
     * @memberof ${srfclassname('${ctrl.codeName}')}
     *
     */
521
    public handleClick(args: any) {
522 523 524 525 526 527 528 529 530 531
        this.clearSelection();
        args.isselected = !args.isselected;
        this.selectchange();
    }

    /**
     * 双击数据
     * @memberof ${srfclassname('${ctrl.codeName}')}
     *
     */
532
    public handleDblClick(args: any) {
533 534 535 536 537 538 539 540
        this.$emit('rowdblclick', args);
    }

    /**
     * 触发事件
     * @memberof ${srfclassname('${ctrl.codeName}')}
     *
     */
541
    public selectchange() {
542 543 544 545 546 547 548 549 550 551 552 553 554 555
        this.selections = [];
        this.items.map((item: any) => {
            if (item.isselected) {
                this.selections.push(item);
            }
        });
        this.$emit('selectionchange', this.selections);
    }

    /**
     * 清除当前所有选中状态
     *
     * @memberof ${srfclassname('${ctrl.codeName}')}
     */
556
    public clearSelection(){
557 558 559 560 561 562 563 564 565 566 567 568
        this.items.map((item: any) => {
            Object.assign(item, { isselected: false });
        }); 
    }

<#ibizinclude>
../@MACRO/CONTROL/CONTROL_BOTTOM-BASE.vue.ftl
</#ibizinclude>

<#ibizinclude>
../@MACRO/CONTROL/CONTROL-BASE.style.ftl
</#ibizinclude>