ibiz-data-grid2.ts 19.5 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 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 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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 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 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 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
/**
 * agGrid控件
 *
 * @class IBizDataGrid2
 * @extends {IBizDataGrid}
 */
class IBizDataGrid2 extends IBizDataGrid {
    /**
     * agGrid控件api对象
     *
     * @type {*}
     * @memberof IBizDataGrid2
     */
    public agGridApi: any = null;

    /**
     * agGrid列api对象
     *
     * @type {*}
     * @memberof IBizDataGrid2
     */
    public agGridColumnApi: any = null;

    /**
     * agGrid列数组
     *
     * @type {Array<any>}
     * @memberof IBizDataGrid2
     */
    public columnDefs: Array<any> = [];

    /**
     * 树表格列模型
     *
     * @type {*}
     * @memberof IBizDataGrid2
     */
    public treeColMode: any = {};

    /**
     * 自定义分组列
     *
     * @type {*}
     * @memberof IBizDataGrid2
     */
    public autoGroupColumn: any = null;

    /**
     * 列过滤参数
     *
     * @type {*}
     * @memberof IBizDataGrid2
     */
    public colFilterParams: any = {};

    /**
     * 是否开启列过滤
     *
     * @type {boolean}
     * @memberof IBizDataGrid2
     */
    public isOpenFilter: boolean = false;

    /**
     *Creates an instance of IBizDataGrid2.
     * @param {*} [opts={}]
     * @memberof IBizDataGrid2
     */
    constructor(opts: any = {}) {
        super(opts);
        if(Object.is(this.getGridStyle(), 'TREEGRID')) {
            this.setTreeColMode();
        }
        this.setIsOpenFilter();
        this.regColumnDefs();
        if(!this.autoGroupColumn) {
            this.setCheckboxColumn();
        }
    }

    /**
     * 加载数据
     *
     * @param {*} [arg={}]
     * @returns {void}
     * @memberof IBizGrid
     */
    public load(arg: any = {}): void {
        // tslint:disable-next-line:prefer-const
        let opt: any = {};
        Object.assign(opt, arg);
        Object.assign(opt, { srfctrlid: this.getName(), srfaction: 'fetch' });
        if (!opt.start) {
            Object.assign(opt, { start: (this.curPage - 1) * this.limit });
        }
        if (!opt.limit) {
            Object.assign(opt, { limit: this.limit });
        }

        Object.assign(opt, { sort: JSON.stringify(this.gridSortField) });

        // 发送加载数据前事件
        this.fire(IBizMDControl.BEFORELOAD, opt);

        if(this.setIsOpenFilter) {
            Object.assign(opt, this.colFilterParams);
        }

        this.allChecked = false;
        this.indeterminate = false;
        this.selection = [];
        this.fire(IBizMDControl.SELECTIONCHANGE, this.selection);


        this.beginLoading();
        this.iBizHttp.post(this.getBackendUrl(), opt).subscribe(response => {
            this.endLoading();
            if (!response.items || response.ret !== 0) {
                if (response.errorMessage) {
                    this.iBizNotification.error('', response.errorMessage);
                }
                return;
            }
            this.items = this.rendererDatas(response.items);
            this.totalrow = response.totalrow;
            if(response.summaryitem) {
                this.setPinnedBottomRow(response.summaryitem);
            }
            this.fire(IBizMDControl.LOADED, response.items);
        }, error => {
            this.endLoading();
            console.log(error.info);
        });
    }

    /**
     * 表格准备完成
     *
     * @param {*} obj
     * @memberof IBizDataGrid2
     */
    public onGridReady() {
        return (params) => {
            this.agGridApi = params.api;
            this.agGridColumnApi = params.columnApi;
        };
    }

    /**
     * 排序
     *
     * @param {*} argu
     * @memberof IBizDataGrid2
     */
    public elementSortChange() {
        return () => {
            var sorts:Array<any> = this.agGridApi.getSortModel();
            if(sorts.length > 0) {
                let fieldName = '';
                if(Object.is(sorts[0].colId, 'ag-Grid-AutoColumn')){
                    fieldName = this.autoGroupColumn.field;
                } else {
                    fieldName = sorts[0].colId
                }
                const col = this.columns.find((item: any) => Object.is(fieldName, item.field));
                if(col) {
                    this.sort(col.name, sorts[0].sort);
                }
            }
        }
    }

    /**
     * 选中值变化
     *
     * @memberof IBizDataGrid2
     */
    public selectionChanged() {
        return () => {
            if(this.openRowEdit) {
                this.agGridApi.deselectAll();
                return;
            }
            this.selection = this.agGridApi.getSelectedRows();
            this.fire(IBizMDControl.SELECTIONCHANGE, this.selection);
        }
    }

    public isRowSelectable() {
        return () => {
            return !this.openRowEdit;
        }
    }

    /**
     * 行双击
     *
     * @returns
     * @memberof IBizDataGrid2
     */
    public rowDbClicked() {
        return (param) => {
            if(!this.openRowEdit && !Object.is(param.rowPinned, 'bottom') && !Object.is(param.rowPinned, 'top')) {
                this.fire(IBizDataGrid.ROWDBLCLICK, this.selection);
            } 
        }
    }

    /**
     * 设置是否支持多选
     * 
     * @param {boolean} state 是否支持多选
     * @memberof IBizGrid
     */
    public setMultiSelect(state: boolean): void {
        super.setMultiSelect(state);
        this.autoGroupColumn.headerCheckboxSelection = state;
        if(this.agGridApi) {
            this.agGridApi.refreshHeader();
        }
    }

    /**
     * 注册多数据列
     *
     * @memberof IBizDataGrid2
     */
    public regColumnDefs(): void {

    }

    /**
     * 注册多数据列
     *
     * @param {*} column
     * @returns
     * @memberof IBizDataGrid2
     */
    public regColumnDef(column: any) {
        if (Object.keys(column).length === 0) {
            return;
        }
        this.columnDefs.push(column);
        this.regColumn(column);
    }

    /**
     * 设置多数据列头
     *
     * @param {*} [column={}]
     * @returns {void}
     * @memberof IBizDataGrid2
     */
    public regColumn(column: any = {}): void {
        if (Object.keys(column).length === 0) {
            return;
        }
        if (!this.columns) {
            this.columns = [];
        }
        if(Object.is(column.colType, 'GROUPGRIDCOLUMN') && column.children) {
            column.children.forEach(col => {
                this.regColumn(col);
            });
        } else {
            if(Object.is(this.getGridStyle(), 'TREEGRID') && this.treeColMode.parentText && Object.is(this.treeColMode.text, column.field)) {
                this.setAutoTreeColumn(column);                
                column.hide = true;
            } else if(column.rowGroupIndex === 1) {
                this.setAutoGroupColumn(column);
                column.hide = true;
            }
            if(column.filter) {
                this.colFilterParams[column.searchName] = '';
            }
            this.columns.push(column);
        }
    }

    /**
     * 默认展开分组层级
     * {-1: 全部展开, 0: 不展开, 1: 展开一级}
     * @returns {number}
     * @memberof IBizDataGrid2
     */
    public getGroupDefExpand(): number {
        return -1
    }

    /**
     * 分组列
     *
     * @returns
     * @memberof IBizDataGrid2
     */
    public getAutoGroupCol() {
        return this.autoGroupColumn;
    }

    /**
     * 获取树数据层级结构
     *
     * @returns
     * @memberof IBizDataGrid2
     */
    public getTreePath() {
        return (data) => {
            return data.paths;
        }
    }

    /**
     * 获取表格样式类型
     *
     * @returns {string}
     * @memberof IBizDataGrid2
     */
    public getGridStyle(): string {
        return '';
    }

    /**
     * 设置树模型
     *
     * @param {*} [column={}]
     * @memberof IBizDataGrid2
     */
    public setTreeColMode(): void {
        
    }

    /**
     * 设置自定义树列
     *
     * @param {*} [column={}]
     * @memberof IBizDataGrid2
     */
    public setAutoTreeColumn(column: any = {}): void {
        if(!this.autoGroupColumn) {
            this.autoGroupColumn = {};
        }
        Object.assign(this.autoGroupColumn, column);
        delete this.autoGroupColumn.hide;
        this.autoGroupColumn.cellRendererParams = {
            checkbox: true,
            suppressCount: true,
        }
    }

    /**
     * 设置自定义分组列
     *
     * @param {*} [column={}]
     * @memberof IBizDataGrid2
     */
    public setAutoGroupColumn(column: any = {}): void {
        if(!this.autoGroupColumn) {
            this.autoGroupColumn = {};
        }
        Object.assign(this.autoGroupColumn, column);
        delete this.autoGroupColumn.hide;
        this.autoGroupColumn.cellRendererParams = {
            checkbox: true,
            suppressCount: true,
        }
    }

    /**
     * 创建复选框列
     *
     * @memberof IBizDataGrid2
     */
    public setCheckboxColumn(): void {
        if(!this.autoGroupColumn) {
            this.autoGroupColumn = {};
        }
        Object.assign(this.autoGroupColumn, {
            width: 42,
            maxWidth: 42,
            pinned: 'left',
            field: 'srf_grid_checkbox',
            suppressMovable: true,
            suppressResize: true,
            suppressMenu: true,
            suppressSorting: true,
            suppressFilter: true,
            checkboxSelection: true,
            suppressSizeToFit: true,
            headerCheckboxSelection: true
        });
        if(this.columnDefs) {
            this.columnDefs.push(this.autoGroupColumn);
        }
    }

    /**
     * 渲染绘制多项数据
     *
     * @param {Array<any>} items
     * @returns {Array<any>}
     * @memberof IBizGrid
     */
    public rendererDatas(items: Array<any>): Array<any> {
        if(Object.is(this.getGridStyle(), 'TREEGRID') && this.treeColMode.parentId && this.treeColMode.id) {
            let arr: Array<any> = [...items];
            arr.forEach((item) => {
                item.paths = [];
                this.setTreePath(items, item, item.paths);
            });
        }
        return items;
    }

    /**
     * 设置树路径
     *
     * @param {Array<any>} items
     * @param {*} [item={}]
     * @param {Array<string>} [paths=[]]
     * @memberof IBizDataGrid2
     */
    public setTreePath(items: Array<any>, item: any = {}, paths: Array<string> = []) {
        let id = this.treeColMode.id;
        let text = this.treeColMode.text?this.treeColMode.text: 'srfmajortext';
        let parentId = this.treeColMode.parentId;
        let parentText = this.treeColMode.parentText;
        
        if(item[parentId] && !Object.is(item[parentId], '')) {
            let hesParent: boolean = true;
            items.forEach((data) => {
                if(Object.is(data[id], item[parentId])) {
                    this.setTreePath(items, data, paths);
                    hesParent = false;
                }
            })
            if(hesParent && parentText) {
                let index = items.indexOf(item);
                let data: any = {};
                data[id] = item[parentId];
                data[text] = item[parentText];
                data.paths = [item[parentText]];
                items.splice(index, 0 ,data);
                paths.push(item[parentText]);
            }
        }
        paths.push(item[text]);
    }

    /**
     * 是否开启行编辑
     *
     * @returns {boolean}
     * @memberof IBizDataGrid2
     */
    public getIsOpenEdit() {
        return (param) => {
            return (this.openRowEdit && param.node && !param.node.rowPinned);
        }
    }

    /**
     * 启用行编辑
     *
     * @param {*} params
     * @returns {void}
     * @memberof IBizDataGrid2
     */
    public openEdit(params: any): void {
        if (!this.isEnableRowEdit) {
            this.iBizNotification.info('提示', '未启用行编辑');
            return;
        }
        this.agGridApi.deselectAll();
        if (!this.openRowEdit) {
            this.openRowEdit = true;
            this.items.forEach((item: any) => { item.openeditrow = true; });
            this.backupDatas = [...JSON.parse(JSON.stringify(this.items))];
            this.items.forEach((item: any, index: number) => {
                this.setEditItemState(item.srfkey);
                this.editRow(item, index, false);
            });
        }
    }

    /**
     * 行编辑单元格值变化
     *
     * @param {string} name
     * @param {*} data
     * @memberof IBizGrid
     */
    public colValueChange(name: string, value: string, data: any): void {
        const srfkey = data.srfkey;
        const _data = this.items.find(back => Object.is(back.srfkey, srfkey));
        if (this.state[srfkey] && this.state[srfkey][name]) {
            data[name] = value;
            this.fire(IBizDataGrid.UPDATEGRIDITEMCHANGE, { name: name, data: data });
        }
    }

    /**
     * 获取语言资源
     *
     * @returns {*}
     * @memberof IBizDataGrid2
     */
    public getLocaleText(): any {
        return IBizDataGrid2.LOCALETEXT;
    }

    /**
     * 设置是否开启列过滤
     *
     * @memberof IBizDataGrid2
     */
    public setIsOpenFilter(): void {

    }

    /**
     * 获取列过滤字段值
     *
     * @param {*} param
     * @returns {*}
     * @memberof IBizDataGrid2
     */
    public getColFilterValue(param): any {
        if(!param || !this.agGridApi) {
            return;
        }
        let filterModel: any = this.agGridApi.getFilterModel();
        let defCol: any = param.colDef;
        if(defCol && filterModel[defCol.field]) {
            return filterModel[defCol.field].filter;
        }
        return '';

    }

    /**
     * 过滤值改变
     *
     * @returns
     * @memberof IBizDataGrid2
     */
    public filterChanged() {
        return () => {
            let filterModel: any = this.agGridApi.getFilterModel();
            let keys: Array<string>  = Object.keys(this.colFilterParams);
            let isChange: boolean = false;
            keys.forEach((key) => {
                if(filterModel) {
                    let index = this.columns.findIndex((column) => Object.is(column.searchName, key));
                    let column: any = this.columns[index];
                    if(filterModel.hasOwnProperty(column.field)) {
                        let filter = Object.is(filterModel[column.field].filter, 'undefined') ? '' : filterModel[column.field].filter;
                        if(Object.is(this.colFilterParams[key], filter)) {
                            return;
                        } else {
                            this.colFilterParams[key] = filter;
                            isChange = true;
                        }
                    } else if (Object.is(this.colFilterParams[key], '')) {
                        return;
                    }
                } else if (Object.is(this.colFilterParams[key], '')){
                    return;
                } else {
                    this.colFilterParams[key] = '';
                    isChange = true;
                }
            });
            if(isChange) {
                this.refresh();
            }
        }
    }

    /**
     * 获取预置组件
     *
     * @returns
     * @memberof IBizDataGrid2
     */
    public getFrameworkComponents() {
        return {
            customTextColumnFilter: Vue.options.components['costom-text-column-filter'],
            customSelectColumnFilter: Vue.options.components['costom-select-column-filter'],
        }
    }

    /**
     * 获取列默认设置
     *
     * @returns
     * @memberof IBizDataGrid2
     */
    public getDefColOption() {
        return {
            menuTabs: ['generalMenuTab', 'columnsMenuTab'],
            pinnedRowCellRenderer: (data) => {
            	return this.getPinnedRowCellRender(data);
            }
        }
    }

    /**
     * 设置底部固定行
     *
     * @param {*} item
     * @memberof IBizDataGrid2
     */
    public setPinnedBottomRow(item: any): void {
        if(item) {
            if(Array.isArray(item)) {
                this.agGridApi.setPinnedBottomRowData(item);
            } else {
                this.agGridApi.setPinnedBottomRowData([item]);
            }
        }
    }

    /**
     * 获取固定行列绘制
     *
     * @param {*} data
     * @returns {string}
     * @memberof IBizDataGrid2
     */
    public getPinnedRowCellRender(data: any): string {
        return data.value;
    }

    /**
     * 表格模型变化
     *
     * @memberof IBizDataGrid2
     */
    public onGridModelChange() {
        let subject: any = new rxjs.Subject();
        subject.pipe(rxjs.operators.debounceTime(2000)).subscribe(() => {
            this.setModel(this.agGridColumnApi.getColumnState());
        });
        return () => {
            subject.next();
        }
    }

    /**
     * 导出数据
     * 
     * @param {any} params 
     * @memberof IBizGrid
     */
    public exportData(arg: any = {}): void {
        // tslint:disable-next-line:prefer-const
        let params: any = {};
        this.fire(IBizMDControl.BEFORELOAD, params);
        if (params.search) {
            Object.assign(params, { query: params.search });
        }
        if(this.setIsOpenFilter) {
            Object.assign(params, this.colFilterParams);
        }
        Object.assign(params, { srfaction: 'exportdata', srfctrlid: this.getName() });

        if (Object.is(arg.itemTag, 'all')) {
            Object.assign(params, { start: 0, limit: this.maxExportRow });
        } else if (Object.is(arg.itemTag, 'custom')) {
            const nStart = arg.exportPageStart;
            const nEnd = arg.exportPageEnd;
            if (nStart < 1 || nEnd < 1 || nStart > nEnd) {
                this.iBizNotification.warning('警告', '请输入有效的起始页');
                return;
            }
            Object.assign(params, { start: (nStart - 1) * this.limit, limit: (nEnd - nStart + 1) * this.limit });
        } else {
            Object.assign(params, { start: (this.curPage * this.limit) - this.limit, limit: this.curPage * this.limit });
        }

        if (this.gridSortField.length > 0) {
            Object.assign(params, { sort: this.gridSortField[0].property, sortdir: this.gridSortField[0].direction });
        }
        this.beginLoading();
        this.iBizHttp.post(this.getBackendUrl(), params).subscribe((res: any) => {
            this.endLoading();
            if (res.ret !== 0) {
                this.iBizNotification.warning('警告', res.info);
                return;
            }
            // if (res.downloadurl) {
            //     let downloadurl = `/${IBizEnvironment.AppName.toLowerCase()}${res.downloadurl} `;
            //     downloadurl = (IBizEnvironment.LocalDeve ? '' : `/${IBizEnvironment.BaseUrl}`) + downloadurl;
            //     IBizUtil.download(downloadurl);
            // }
        }, (error: any) => {
            this.endLoading();
            this.iBizNotification.error('', error.errorMessage);
        });
    }

    /**
     * 路由跳转
     *
     * @memberof IBizDataGrid2
     */
    public onRouteChange() {
        var ele: any = document.getElementsByClassName('ag-header-container');
        if(ele.length > 0) {
            ele[0].style.left = 0;
        }
    }

    /**
     * 多语言设置
     *
     * @static
     * @memberof IBizDataGrid2
     */
    public static LOCALETEXT = {
        pinColumn: '锁定列',
        autosizeThiscolumn: '自动调整此列的大小',
        autosizeAllColumns: '自动调整所有列的大小',
        resetColumns: '重置所有列',
        pinLeft: '锁定左侧',
        pinRight: '锁定右侧',
        noPin: '不锁定',
        noRowsToShow: '暂无数据'
    }
}