天天看點

如何使用 controllerExtensions 給 SAP Fiori Elements List Report 的表格注冊事件響應函數

步驟1:在 manifest.json 的 extends 區域裡,注冊 controllerExtensions:

如何使用 controllerExtensions 給 SAP Fiori Elements List Report 的表格注冊事件響應函數
源代碼:

"extends": {
            "extensions": {
                "sap.ui.controllerExtensions": {
                    "sap.suite.ui.generic.template.ListReport.view.ListReport": {
                      "controllerName": "com.sap.jerry.jerryfioriapp.ext.controller.ListReportExtension",
                        "sap.ui.generic.app": {
                          "SEPMRA_C_PD_Product": {
                            "EntitySet": "SEPMRA_C_PD_Product",
                            "Actions": {
                              "ActionName1": {
                                "id" : "ActionName1",
                                "text" : "Jerry的按鈕",
                                "press" : "onCustomAction1",
                                "global": true
                              }
                            }
                        }
                    }
                }        
            }
            }
        },      

步驟2:

實作 controller extension:

sap.ui.define("com.sap.jerry.jerryfioriapp.ext.controller.ListReportExtension", [], function() {
    return {
        onCustomAction1 : function(oEvent) {
            alert('Hello');
        },
        onAfterRendering: function (oEvent) {
            debugger;
            var oContentTable = this.byId("com.sap.jerry.jerryfioriapp::sap.suite.ui.generic.template.ListReport.view.ListReport::SEPMRA_C_PD_Product--responsiveTable");
            oContentTable.attachSelect(this._onSelectChanged);
    
        },
        _onSelectChanged: function (oEvent) {
            debugger;
        }
    }
  });      

步驟3:測試。

運作時,首先觸發 onAfterRendering 鈎子函數,通過byId API,根據 Smart Table 控件 ID,拿到其 table 執行個體:

如何使用 controllerExtensions 給 SAP Fiori Elements List Report 的表格注冊事件響應函數

SAP UI5 裡所有的運作時建立執行個體,都存儲在全局對象 mInstances 裡,鍵為 控件 id,值為控件執行個體。

如何使用 controllerExtensions 給 SAP Fiori Elements List Report 的表格注冊事件響應函數

拿到 table 執行個體後,調用其 attach 方法,挂接對應的事件處理函數。

如何使用 controllerExtensions 給 SAP Fiori Elements List Report 的表格注冊事件響應函數

一切完成後,點選 Smart Table 某行項目,我們使用 attachSelect 注冊的事件處理函數就會觸發:

如何使用 controllerExtensions 給 SAP Fiori Elements List Report 的表格注冊事件響應函數