SAP UI5:使用生命周期钩子函数动态填充xml下拉列表

SAP UI5: Fill XML-Dropdown dynamically using Lifecycle Hook Function

本文关键字:动态 子函数 填充 xml 下拉列表 周期 UI5 生命 SAP      更新时间:2023-09-26

我已经完成了使用SAPUI5构建fiori类ui的教程,并尝试添加排序函数。

下拉框中填充了JSON-Model-Data的名称:

{
"SalesOrderCollection": [
    {
        "SoId": "300000097",
        "ApprovalStatus": "",
        "Status": "Initial",
        "ConfirmationStatus": "",
        "BillingStatus": "Initial",
        ...
        } ...

我在我的视图控制器中实现了这个函数来填充下拉菜单:

    fillDropdown : function(evt) {
    // Get current view and dropdownbox
    var oView = this.getView();
    var oDropdown = oView.byId("ddbox");
    // Get names of nodes in model
    var metaArray = Object.getOwnPropertyNames(oView.getModel()
            .getProperty("/SalesOrderCollection/0"));
    // Add every name to dropdownbox
    var arrayLength = metaArray.length;
    for ( var i = 0; i < arrayLength; i++) {
        oDropdown.addItem(new sap.ui.core.ListItem("item" + i)
                .setText(metaArray[i]));
    }
}

最后,我的问题来了:

如何在渲染视图时自动运行这个函数?我知道生命周期钩子函数onInit;onBeforeRendering但是我不能在XML-View中使用它们:

我可以像这样为UI-Elements注册eventandler:

<uicom:DropdownBox id="ddbox" editable="true" change="handleListSort">

但不像我在这里尝试的View或Page:

<Page title="myPage" onBeforeRendering="fillDropdown">
<core:View controllerName="sap.ui.demo.myFiori.view.Master" (...) onBeforeRendering="fillDropdown">

可能的解决方法:在点击图标栏中的排序按钮时调用该函数

<IconTabBar select="fillDropdown">

谢谢你的帮助!


如果我使用JavaScript-View而不是XML-View,我可以简单地在视图的onAfterRendering函数中实现我的fillDropdown函数。但是为什么XML视图不抛出生命周期钩子事件?


更新2

我也不能使用我的视图控制器的onAfterRendering;oView.getModel().getProperty("/SalesOrderCollection/0");不返回任何对象,因为我的模型的内容(无论如何)只在钩子函数之后可用。

onInit, onBeforeRendering, onAfterRendering, onExit是生命周期事件。您不需要从视图注册。函数应该在实现独立于视图类型时调用。请看这里http://jsbin.com/hiyanuqu/1/edit

顺便说一句,在sap.m库中有一个类似的内置控件,名为"带建议的输入"。https://openui5.hana.ondemand.com/test-resources/sap/m/demokit/explored/index.html/样本/sap.m.sample.InputSuggestionsCustomFilter