【问题标题】:Binding JSON data in UI5 table在 UI5 表中绑定 JSON 数据
【发布时间】:2014-08-05 02:43:18
【问题描述】:

我正在尝试将一些 JSON 数据显示到 UI5 中。我能够加载数据并且还能够访问它以在独立字段中显示。例如,如果我试图在文本视图我可以显示它。但它似乎不适用于表格。

以下是我的尝试(在 ajax 成功处理程序方法中完成):

var oModel1 = new sap.ui.model.json.JSONModel();  
var aData = jsonModel.getProperty("{/list}");
oModel1.setData({modelData : aData}); 
weatherTable.setModel(oModel1);  
weatherTable.bindRows("/modelData");  
weatherTable.placeAt('content'); 

更新 index.html 的 script 标签中的完整代码,显示什么是 jsonModel 并且它不为空。

<script>


            var url = "http://api.openweathermap.org/data/2.5/group?id=524901,703448,2643743&units=metric";

            jQuery.ajax({
                url : url,
                dataType : "json",
                success : function(data, textStatus, jqXHR){
                    var jsonModel = new sap.ui.model.json.JSONModel();
                    alert("status: " +textStatus);
                    alert(data);
                    jsonModel.setData(data);
                    sap.ui.getCore().setModel(jsonModel);


                    var weatherTable = new sap.ui.table.Table({
                        title : "Current Weather Details",
                        visibleRowCount : 4             
                    });



                    weatherTable.addColumn(new sap.ui.table.Column({
                            label : new sap.ui.commons.Label({text : "Country"}),               
                            template : new sap.ui.commons.TextView().bindText("country")                
                    }));

                    weatherTable.addColumn(new sap.ui.table.Column({
                        label : new sap.ui.commons.Label({text : "City"}),              
                        template : new sap.ui.commons.TextView().bindText("sunrise")
                    }));

                    weatherTable.addColumn(new sap.ui.table.Column({
                        label : new sap.ui.commons.Label({text : "Weather"}),               
                        template : new sap.ui.commons.TextView().bindText("sunset")
                    }));


                    //Create a model and bind the table rows to this model    
                    var oModel1 = new sap.ui.model.json.JSONModel();  
                    //Get the forecastday array table from txt_forecast object    
                    //var aData = jsonModel.getProperty("{/sys}");
                    alert(jsonModel);
                    var aData = jsonModel.getProperty("{/list}");                        
                    //alert(aData);
                    oModel1.setData({modelData : aData});            
                    //oModel1.setData(data);
                    alert(oModel1.getJSON());
                    weatherTable.setModel(oModel1);  
                    weatherTable.bindAggregation("rows","/modelData");  
                    weatherTable.placeAt('content'); 


                    var dataLayout = new sap.ui.layout.HorizontalLayout({
                        id : "dataLayout", // sap.ui.core.ID                
                    });

                    var country = new sap.ui.commons.TextView({
                        id : "country",
                        //text : '{/sys/country}'
                        text : '{/list/0/sys/country}'
                    })

                    var city = new sap.ui.commons.TextView({
                        id : "city", // sap.ui.core.ID
                        //text : '{/name}', // string
                        text : '{/list/0/name}', // string
                    });

                    var weatherDesc = new sap.ui.commons.TextView({
                        id : "weather", // sap.ui.core.ID
                        //text : '{/weather/0/description}', // string
                        text : '{/list/0/weather/0/description}', // string
                    });

                    dataLayout.addContent(country);
                    dataLayout.addContent(city);
                    dataLayout.addContent(weatherDesc);
                    dataLayout.placeAt("content");


                }
            });

</script>

当我提醒 aData 时,我将其设为 null,因为模型上的 getProperty 没有返回任何内容。这就是问题所在。可能是我尝试访问列表的方式错误。

以下是返回的 JSON 数据:

{
  "cnt": 3,
  "list": [
    {
      "coord": {
        "lon": 37.62,
        "lat": 55.75
      },
      "sys": {
        "type": 1,
        "id": 7323,
        "message": 0.0208,
        "country": "RU",
        "sunrise": 1407202987,
        "sunset": 1407259673
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "Sky is Clear",
          "icon": "01n"
        }
      ],
      "main": {
        "temp": 18.76,
        "pressure": 1019,
        "humidity": 72,
        "temp_min": 17.5,
        "temp_max": 20
      },
      "wind": {
        "speed": 3,
        "deg": 30
      },
      "clouds": {
        "all": 0
      },
      "dt": 1407200467,
      "id": 524901,
      "name": "Moscow"
    },
    {
      "coord": {
        "lon": 30.52,
        "lat": 50.43
      },
      "sys": {
        "type": 1,
        "id": 7348,
        "message": 0.0346,
        "country": "UA",
        "sunrise": 1407205931,
        "sunset": 1407260136
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "Sky is Clear",
          "icon": "01n"
        }
      ],
      "main": {
        "temp": 19.55,
        "pressure": 1013,
        "humidity": 88,
        "temp_min": 17,
        "temp_max": 21
      },
      "wind": {
        "speed": 4,
        "deg": 10
      },
      "clouds": {
        "all": 0
      },
      "dt": 1407200377,
      "id": 703448,
      "name": "Kiev"
    },
    {
      "coord": {
        "lon": -0.13,
        "lat": 51.51
      },
      "sys": {
        "type": 1,
        "id": 5091,
        "message": 0.1124,
        "country": "GB",
        "sunrise": 1407213071,
        "sunset": 1407267704
      },
      "weather": [
        {
          "id": 800,
          "main": "Clear",
          "description": "Sky is Clear",
          "icon": "01n"
        }
      ],
      "main": {
        "temp": 14.51,
        "pressure": 1018,
        "humidity": 72,
        "temp_min": 13,
        "temp_max": 17
      },
      "wind": {
        "speed": 4.1,
        "deg": 240
      },
      "clouds": {
        "all": 0
      },
      "dt": 1407200474,
      "id": 2643743,
      "name": "London"
    }
  ]
}

请提出建议。等待您的回复。

干杯, 哇

【问题讨论】:

    标签: json sapui5


    【解决方案1】:

    查看完整代码后更新答案:

    1. 调用 JSONModel 的 getProperty 方法时不应该有大括号。

      var aData = jsonModel.getProperty("/list");

    2. 您的表格列的绑定路径错误。添加sys路径

             var weatherTable = new sap.ui.table.Table({
                      title : "Current Weather Details",
                      visibleRowCount : 4             
             });
      
      
      
             weatherTable.addColumn(new sap.ui.table.Column({
                      label : new sap.ui.commons.Label({text : "Name"}),               
                  template : new sap.ui.commons.TextView().bindText("name")                
              }));
      
              weatherTable.addColumn(new sap.ui.table.Column({
                      label : new sap.ui.commons.Label({text : "Country"}),               
                  template : new sap.ui.commons.TextView().bindText("sys/country")                
              }));
      
              weatherTable.addColumn(new sap.ui.table.Column({
                  label : new sap.ui.commons.Label({text : "City"}),              
                  template : new sap.ui.commons.TextView().bindText("sys/sunrise")
              }));
      
              weatherTable.addColumn(new sap.ui.table.Column({
                  label : new sap.ui.commons.Label({text : "Weather"}),               
                  template : new sap.ui.commons.TextView().bindText("sys/sunset")
              }));
      
              weatherTable.addColumn(new sap.ui.table.Column({
             label : new sap.ui.commons.Label({text : "Description"}),               
                    template : new sap.ui.commons.TextView().bindText("weather/0/description")
      }));
      

    【讨论】:

    • 我想我在做和你一样的事情,除了在 set data 方法而不是 jsonmodel 的构造函数中设置数据。jsonModel 包含从 url 返回的整个数据集,并且创建了另一个模型这是 oModel1,它只包含表的数据集。请参阅我更新的完整代码,其中显示了这一点并提出了建议。感谢您的帮助。
    • 感谢艾伦,它有效。您能否告诉我我正在尝试访问天气数组下的描述属性。我怎样才能访问它?我正在尝试使用 /weather。感谢您的帮助。
    • 您好,很高兴为您提供帮助,请将我的回答标记为已回答。天气下的description属性应该是/list/0/weather/0/description。
    • 您好 Allen,一旦我清除所有查询,肯定会将其标记为已回答。对于您提到的天气,它将为我提供第 0 个索引处数组元素的描述。正如您将看到的我的代码,我试图将它们放在表中并希望它循环遍历数组,以便我可以获得每个数组对象的描述。请建议。谢谢,AW
    • 嗨,请查看我的更新答案。只需添加其他列模板。谢谢。
    【解决方案2】:

    在您提到的代码中,aData 正在获取null。我认为这是因为var aData = jsonModel.getProperty("{/list}"); 这里jsonModel 是什么问题。正在尝试获取您在某处设置的模型

    示例:

    sap.ui.getCore().setModel('jsonModel' , jsonData);  // Setting the model in core with name jsonModel
    

    并在变量中获取此模型。

    var jsonModel = sap.ui.getCore().getModel('jsonModel');
    var aData = jsonModel.getProperty('/list'); //please check the path
    

    【讨论】:

    • jsonModel 不是我检查过的空值。我已经在脚本标签中发布了完整的代码。我还发布了 JSON 响应,以便您可以建议我是否使用了错误的路径。请看一下并提出建议。
    • @user3905218 : 什么是 jsonModel 在这个 var aData = jsonModel.getProperty("{/list}");
    • 请查看更新后的代码,其中 jsonModel 被定义为从公共 Web 服务加载的数据的基本模型。
    • 感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2020-11-20
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多