javascript 在 SAPUI5 中从控制器动态绑定表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27135788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:00:28  来源:igfitidea点击:

Dynamically bind table from controller in SAPUI5

javascriptodatasapsapui5

提问by rustycode

I have created a table in the xml view, I would like to bind the table in the controller so it can be modified dynamically depending on who is loading the app.

我在 xml 视图中创建了一个表,我想在控制器中绑定该表,以便可以根据加载应用程序的人员动态修改它。

XML VIEW

XML视图

<Table inset="false"
            id="pTable">    
            <columns>
                <Column id="year" width="auto">
                    <Text text="Year" />
                </Column>
                <Column id="rating" width="auto">
                    <Text text="Performance Rating"/>
                </Column>
                <Column id="respect" width="auto">
                    <Text text="Managing with Respect" />
                </Column>
            </columns>
            <items>
                <ColumnListItem>
                    <cells>
                        <Text id="tYear" text="{Begda}" />
                        <Text id="tRating" text="{Rating}" />
                        <Text id="tRespect" text="{MwrRating}" />                        
                    </cells>
                </ColumnListItem>
             </items>
            </Table>     

JS Controller

JS控制器

var pHistory = this.byId("pTable");
var phURL = "/PMRPerformanceSet/?$filter=IvGuid eq '5438A43913276540E1008000A7E414BA'"
pHistory.setModel(oModel);
pHistory.bindRows(phURL);

It seems like the controller should look something like this, however, this does not work

控制器看起来应该像这样,但是,这不起作用

Any suggestions?

有什么建议?

采纳答案by Abul

I guess you are using sap.m.Table:

我猜你正在使用 sap.m.Table:

<Table id="pTable" 
    inset="false" 
    items="{/PMRPerformanceSet}">  // add items Here for your oData  collection   
    <columns>
        <Column id="year" width="auto">
            <Text text="Year" />
         </Column>
         <Column id="rating" width="auto">
             <Text text="Performance Rating"/>
         </Column>
         <Column id="respect" width="auto">
             <Text text="Managing with Respect" />
         </Column>
     </columns>
     <items>
         <ColumnListItem>
             <cells>
                 <Text id="tYear" text="{Begda}" />
                 <Text id="tRating" text="{Rating}" />
                 <Text id="tRespect" text="{MwrRating}" />                        
             </cells>
         </ColumnListItem>
     </items>
</Table>

Then set Model in controller. check your odata service response in Network tab of developers tool then Bind items according to that in XML view.

然后在控制器中设置模型。在开发人员工具的网络选项卡中检查您的 odata 服务响应,然后根据 XML 视图中的绑定项目。

回答by cschuff

I guess it should more look like this:

我想它应该更像这样:

var oTable = this.getView().byId("pTable");
var phURL = "/PMRPerformanceSet/?$filter=IvGuid eq '5438A43913276540E1008000A7E414BA'"
var oModel = new sap.ui.model.odata.ODataModel(baseUrl + phURL);

oTable.setModel(oModel);
oTable.bindRows("/YourBindingRootPath");

See Example in the docs.

请参阅文档中的示例

回答by Rosemol Francis

Please try this in controller

请在控制器中试试这个

   var oModel = new sap.ui.model.odata.ODataModel("proxy/http/seasrv05.applexus.com:8000/sap/opu/odata/sap/ZGET_MATERIALS",
              true, 'appdevelop', 'develop01');

    oModel.read("/zget_materialsCollection",null,["$filter=i_search eq 'R1' and i_werks eq '1000'"],true,
            function(data, response) 
        {
        var value=[];
        value = data.results;
        console.log(value);
        console.log(oModel);
        //sap.ui.getCore().getModel("getMaterialModel").fireRequestCompleted();
        var oModel1 = new sap.ui.model.json.JSONModel();
        oModel1.setData({ materials: value});
        oModel1.setSizeLimit(300);
        var oTable = sap.ui.getCore().byId("table");
        oTable.setModel(oModel1);
        oTable.bindRows("/materials");      
        },
        function()
        {
            jQuery.sap.require("sap.m.MessageToast");
            sap.m.MessageToast.show("No record fetched",{my : "center center",at : "center center"});
        }); 

回答by rustycode

Here is the solution I ending up using, this takes the table with the id "pTable" from an xml view and binds it with my JSON model jModel.

这是我最终使用的解决方案,它从 xml 视图中获取 ID 为“pTable”的表,并将其与我的 JSON 模型 jModel 绑定。

var pHistory = this.byId("pTable");
    pHistory.setModel(jModel);
    pHistory.bindAggregation("items", "/ratings/results", new sap.m.ColumnListItem({
        cells:[
               new sap.m.Label({
                   text:"{Begda}"
               }),
               new sap.m.Text({
                   text:"{Rating}"
               }),
               new sap.m.Text({
                   text:"{MwrRating}"
               })
               ]
    }));