gridview-base.tsx 6.2 KB
Newer Older
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
import { IPSAppDEGridView, IPSDEGrid, IPSDEToolbarItem } from '@ibiz/dynamic-model-api';
import { GirdViewInterface, GridViewEngine, ModelTool, Util } from 'ibiz-core';
import { MDViewBase } from './mdview-base';

/**
 * 表格视图基类
 *
 * @export
 * @class GridViewBase
 * @extends {MDViewBase}
 * @implements {GirdViewInterface}
 */
export class GridViewBase extends MDViewBase implements GirdViewInterface {

    /**
     * 视图实例
     * 
     * @memberof GridViewBase
     */
    public viewInstance!: IPSAppDEGridView;

    /**
     * 表格实例
     * 
     * @memberof GridViewBase
     */
    public gridInstance!: IPSDEGrid;

    /**
     * 表格行数据默认激活模式
     * 0 不激活
     * 1 单击激活
     * 2 双击激活
     *
     * @protected
     * @type {(0 | 1 | 2)}
     * @memberof GridViewBase
     */
    protected gridRowActiveMode: number = 0;

    /**
     * 视图引擎
     *
     * @public
     * @type {Engine}
     * @memberof GridViewBase
     */
    public engine: GridViewEngine = new GridViewEngine();

    /**
     * 引擎初始化
     *
     * @param {*} [opts={}] 引擎参数
     * @memberof GridViewBase
     */
    public engineInit(opts: any = {}): void {
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        if (this.engine && this.gridInstance) {
            let engineOpts = Object.assign({
                view: this,
                parentContainer: this.$parent,
                p2k: '0',
                isLoadDefault: this.viewInstance?.loadDefault,
                keyPSDEField: this.appDeCodeName.toLowerCase(),
                majorPSDEField: this.appDeMajorFieldName.toLowerCase(),
                opendata: (args: any[], fullargs?: any[], params?: any, $event?: any, xData?: any) => {
                    this.opendata(args, fullargs, params, $event, xData);
                },
                newdata: (args: any[], fullargs?: any[], params?: any, $event?: any, xData?: any) => {
                    this.newdata(args, fullargs, params, $event, xData);
                },
                grid: (this.$refs[this.gridInstance?.name] as any).ctrl,
            }, opts)
            if (this.searchFormInstance?.name && this.$refs[this.searchFormInstance.name]) {
                engineOpts.searchform = ((this.$refs[this.searchFormInstance.name] as any).ctrl);
            } else if(this.quickSearchFormInstance?.name && this.$refs[this.quickSearchFormInstance.name] ){
                engineOpts.searchform = ((this.$refs[this.quickSearchFormInstance.name] as any).ctrl);
            }
            if(this.searchBarInstance?.name && this.$refs[this.searchBarInstance.name]) {
                engineOpts.searchbar = ((this.$refs[this.searchBarInstance.name] as any).ctrl);
            }
            this.engine.init(engineOpts);
        }
    }

    /**
     * 初始化表格视图实例
     * 
     * @memberof GridViewBase
     */
    public async viewModelInit() {
        this.viewInstance = (this.staticProps?.modeldata) as IPSAppDEGridView;
        await super.viewModelInit();
        this.gridInstance = ModelTool.findPSControlByType("GRID",this.viewInstance.getPSControls());
        this.gridRowActiveMode = this.viewInstance?.gridRowActiveMode;        
    }
    
    /**
     * 渲染视图主体内容区
     * 
     * @memberof GridViewBase
     */
    public renderMainContent() {
        let { targetCtrlName, targetCtrlParam, targetCtrlEvent }: { targetCtrlName: string, targetCtrlParam: any, targetCtrlEvent: any } = this.computeTargetCtrlData(this.gridInstance);
        return this.$createElement(targetCtrlName, { props: targetCtrlParam, ref: this.gridInstance?.name, on: targetCtrlEvent },);
    }

    /**
     * 快速搜索
     *
     * @param {*} $event 事件源对象
     * @memberof GridViewBase
     */
    public onSearch($event: any): void {
        if (this.Environment && this.Environment.isPreviewMode) {
            return;
        }
        const refs: any = this.$refs;
        if (refs[this.gridInstance?.name] && refs[this.gridInstance.name].ctrl) {
            if (Object.is(Util.typeOf($event), 'object')) {
              refs[this.gridInstance?.name].ctrl.load(this.context, true, false);
            } else {
              refs[this.gridInstance?.name].ctrl.load(this.context, true, true);
            }
        }
    }

    /**
     * 计算目标部件所需参数
     *
     * @param {any} [controlInstance] 部件模型实例
     * @returns
     * @memberof GridViewBase
     */
    public computeTargetCtrlData(controlInstance: any) {
        const { targetCtrlName, targetCtrlParam, targetCtrlEvent } = super.computeTargetCtrlData(controlInstance);
        Object.assign(targetCtrlParam.staticProps, {
            isOpenEdit: this.viewInstance?.rowEditDefault,
            gridRowActiveMode: this.viewInstance?.gridRowActiveMode,
        })
        return { targetCtrlName: targetCtrlName, targetCtrlParam: targetCtrlParam, targetCtrlEvent: targetCtrlEvent };
    }

    /**
     * 部件事件
     * @param ctrl 部件 
     * @param action  行为
     * @param data 数据
     * 
     * @memberof GridViewBase
     */
    public onCtrlEvent(controlname: string, action: string, data: any) {
        if(action) {
            switch (action) {
                case "save":
                    this.$emit("view-event", { action: "drdatasaved", data: data });
                    break;
                case "search":
                    this.onSearch(data);
                    break;
                default:
                    super.onCtrlEvent(controlname, action, data);
                    break;
            }
        }
    }

    /**
     * 初始化工具栏项
     *
     * @param {IPSDEToolbarItem} item
     * 
     * @@memberof ViewBase
     */
    initToolBarItems(item: IPSDEToolbarItem): void {
        const tempModel: any = super.initToolBarItems(item);
        // 导出最大行数
        if(tempModel?.uiaction?.uIActionTag == "ExportExcel"){
            const gridInstance = ModelTool.findPSControlByType("GRID",this.viewInstance.getPSControls());
            const maxRowCount = gridInstance.getPSDEDataExport()?.maxRowCount;
            if(maxRowCount){
                tempModel.MaxRowCount = maxRowCount;
            }
        }
        return tempModel;
    }

}