Quantcast
Channel: SCN: Message List - SAPUI5 Developer Center
Viewing all articles
Browse latest Browse all 8124

Re: JSON data in SAPUI5

$
0
0

Chandrashekhar Mahajan  hai....i tried to copy your same code in your blog ......when is run that table is coming yar.....But i cant get the Datas :/ Can you explain me why frnd


  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <!-- Added charset='utf-8' to handle data in various langauges --> 
  5. <meta http-equiv='X-UA-Compatible' content='IE=edge' charset='utf-8' /> 
  6. <title>Weather Forecast</title> 
  7. <!-- Load UI5, select gold reflection theme and the "commons" and "table" control libraries -> 
  8. <script id='sap-ui-bootstrap' type='text/javascript' 
  9.           src='resources/sap-ui-core.js' data-sap-ui-theme='sap_platinum' 
  10.           data-sap-ui-libs='sap.ui.commons,sap.ui.table'></script> 
  11. <script type="text/javascript"> 
  12.      // create a JSONModel, fill in the data and bind the Table to this model   
  13.           var oModel = new sap.ui.model.json.JSONModel(); 
  14.           // service url for Weather Underground JSON API    
  15.           var url = 'http://api.wunderground.com/api/Your_Key/conditions/forecast/q/autoip.json?callback=getJSON'; 
  16.           // var url = 'http://api.wunderground.com/api/Your_Key/conditions/forecast/lang:MR/q/autoip.json?callback=getJSON';  //Example URL 
  17.           // var url = 'http://api.wunderground.com/api/Your_Key/forecast/conditions/lang:MR/q/France/Paris.json?callback=getJSON';   //Example URL 
  18.           //Ajax Call with Callback function and JSONP data type    
  19.           $ 
  20.                               .ajax({ 
  21.                                         url : url, 
  22.                                         jsonpCallback : 'getJSON', 
  23.                                         contentType : "application/json", 
  24.                                         dataType : 'jsonp', 
  25.                                         success : function(data, textStatus, jqXHR) { 
  26.                                                   oModel.setData(data); 
  27.                                                   sap.ui.getCore().setModel(oModel); 
  28.                                                   // create matrix layout   
  29.                                                   var oMatrix = new sap.ui.commons.layout.MatrixLayout({ 
  30.                                                             id : 'matrix', 
  31.                                                             layoutFixed : false, 
  32.                                                             columns : 3, 
  33.                                                   }); 
  34.                                                   var oCell = new sap.ui.commons.layout.MatrixLayoutCell({ 
  35.                                                             colSpan : 3 
  36.                                                   }); 
  37.                                                   var oTV = new sap.ui.commons.TextView( 
  38.                                                                       { 
  39.                                                                                 id : 'TV-Head', 
  40.                                                                                 text : 'SAPUI5 Application based on Weather Underground JSON API', 
  41.                                                                                 design : sap.ui.commons.TextViewDesign.H1 
  42.                                                                       }); 
  43.                                                   oCell.addContent(oTV); 
  44.                                                   oMatrix.createRow(oCell); 
  45.                                                   //Create a standard divider   
  46.                                                   var oCell = new sap.ui.commons.layout.MatrixLayoutCell({ 
  47.                                                             colSpan : 3 
  48.                                                   }); 
  49.                                                   oCell.addContent(new sap.ui.commons.HorizontalDivider()); 
  50.                                                   oMatrix.createRow(oCell); 
  51.                                                   //Lable and TextField for City   
  52.                                                   oLabelCity = new sap.ui.commons.Label({ 
  53.                                                             id : 'L-City', 
  54.                                                             text : 'City' 
  55.                                                   }); 
  56.                                                   oTFCity = new sap.ui.commons.TextField({ 
  57.                                                             id : 'TF-City', 
  58.                                                             tooltip : 'City', 
  59.                                                             editable : false, 
  60.                                                             value : '{/current_observation/display_location/full}', 
  61.                                                             width : '200px' 
  62.                                                   }); 
  63.                                                   oLabelCity.setLabelFor(oTFCity); 
  64.                                                   oMatrix.createRow(oLabelCity, oTFCity); 
  65.                                                   //Lable and TextField for Latitute   
  66.                                                   oLabelLat = new sap.ui.commons.Label({ 
  67.                                                             id : 'L-Lat', 
  68.                                                             text : 'Latitude' 
  69.                                                   }); 
  70.                                                   oTFLat = new sap.ui.commons.TextField( 
  71.                                                                       { 
  72.                                                                                 id : 'TF-Lat', 
  73.                                                                                 tooltip : 'Latitude', 
  74.                                                                                 editable : false, 
  75.                                                                                 value : '{/current_observation/display_location/latitude}', 
  76.                                                                                 width : '200px' 
  77.                                                                       }); 
  78.                                                   oLabelLat.setLabelFor(oTFLat); 
  79.                                                   oMatrix.createRow(oLabelLat, oTFLat); 
  80.                                                   //Lable and TextField for longitude   
  81.                                                   oLabelLon = new sap.ui.commons.Label({ 
  82.                                                             id : 'L-Lon', 
  83.                                                             text : 'Longitude' 
  84.                                                   }); 
  85.                                                   oTFLon = new sap.ui.commons.TextField( 
  86.                                                                       { 
  87.                                                                                 id : 'TF-Lon', 
  88.                                                                                 tooltip : 'Longitude', 
  89.                                                                                 editable : false, 
  90.                                                                                 value : '{/current_observation/display_location/longitude}', 
  91.                                                                                 width : '200px' 
  92.                                                                       }); 
  93.                                                   oLabelLon.setLabelFor(oTFLon); 
  94.                                                   oMatrix.createRow(oLabelLon, oTFLon); 
  95.                                                   //Lable and TextField for Elevation   
  96.                                                   oLabelElev = new sap.ui.commons.Label({ 
  97.                                                             id : 'L-Elev', 
  98.                                                             text : 'Elevation' 
  99.                                                   }); 
  100.                                                   oTFElev = new sap.ui.commons.TextField( 
  101.                                                                       { 
  102.                                                                                 id : 'TF-Elev', 
  103.                                                                                 tooltip : 'Elevation', 
  104.                                                                                 editable : false, 
  105.                                                                                 value : '{/current_observation/observation_location/elevation}', 
  106.                                                                                 width : '200px' 
  107.                                                                       }); 
  108.                                                   oLabelElev.setLabelFor(oTFElev); 
  109.                                                   oMatrix.createRow(oLabelElev, oTFElev); 
  110.                                                   //Create a standard divider   
  111.                                                   var oCell = new sap.ui.commons.layout.MatrixLayoutCell({ 
  112.                                                             colSpan : 3 
  113.                                                   }); 
  114.                                                   oCell.addContent(new sap.ui.commons.HorizontalDivider()); 
  115.                                                   oMatrix.createRow(oCell); 
  116.                                                   //Weather image   
  117.                                                   var oImageWeather = new sap.ui.commons.Image({ 
  118.                                                             src : '{/current_observation/icon_url}', 
  119.                                                             alt : '{/current_observation/icon}' 
  120.                                                   }); 
  121.                                                   //Lable and TextField for weather   
  122.                                                   oLabelWeather = new sap.ui.commons.Label({ 
  123.                                                             id : 'L-Weather', 
  124.                                                             text : 'Weather' 
  125.                                                   }); 
  126.                                                   oTFWeather = new sap.ui.commons.TextField({ 
  127.                                                             id : 'TF-Weather', 
  128.                                                             tooltip : 'Weather', 
  129.                                                             editable : false, 
  130.                                                             value : '{/current_observation/weather}', 
  131.                                                             width : '200px' 
  132.                                                   }); 
  133.                                                   oLabelWeather.setLabelFor(oTFWeather); 
  134.                                                   oMatrix.createRow(oLabelWeather, oTFWeather, oImageWeather); 
  135.                                                   //Create a standard divider   
  136.                                                   var oCell = new sap.ui.commons.layout.MatrixLayoutCell({ 
  137.                                                             colSpan : 3 
  138.                                                   }); 
  139.                                                   oCell.addContent(new sap.ui.commons.HorizontalDivider()); 
  140.                                                   oMatrix.createRow(oCell); 
  141.                                                   //Lable and TextField for temp_c   
  142.                                                   oLabelTemp = new sap.ui.commons.Label({ 
  143.                                                             id : 'L-Temp', 
  144.                                                             text : 'Temperature' 
  145.                                                   }); 
  146.                                                   var tempstring = oModel 
  147.                                                                       .getProperty("/current_observation/temp_c"); 
  148.                                                   //Append Degree Celsius unit symbol to Temperature reading    
  149.                                                   var tempinC = tempstring + "\u2103"; 
  150.                                                   oTFTemp = new sap.ui.commons.TextField({ 
  151.                                                             id : 'TF-Temp', 
  152.                                                             tooltip : 'Temperature', 
  153.                                                             editable : false, 
  154.                                                             value : tempinC, 
  155.                                                             width : '220px' 
  156.                                                   }); 
  157.                                                   oLabelTemp.setLabelFor(oTFTemp); 
  158.                                                   oMatrix.createRow(oLabelTemp, oTFTemp); 
  159.                                                   //Lable and TextField for Obervation Time   
  160.                                                   oLabelObsTime = new sap.ui.commons.Label({ 
  161.                                                             id : 'L-ObsTime', 
  162.                                                             text : 'Observation Time' 
  163.                                                   }); 
  164.                                                   oTFObsTime = new sap.ui.commons.TextField({ 
  165.                                                             id : 'TF-ObsTime', 
  166.                                                             tooltip : 'Observation Time', 
  167.                                                             editable : false, 
  168.                                                             value : '{/current_observation/observation_time}', 
  169.                                                             width : '220px' 
  170.                                                   }); 
  171.                                                   oLabelObsTime.setLabelFor(oTFObsTime); 
  172.                                                   oMatrix.createRow(oLabelObsTime, oTFObsTime); 
  173.                                                   //Lable and TextField for Local Time   
  174.                                                   oLabelLclTime = new sap.ui.commons.Label({ 
  175.                                                             id : 'L-LclTime', 
  176.                                                             text : 'Local Time' 
  177.                                                   }); 
  178.                                                   oTFLclTime = new sap.ui.commons.TextField({ 
  179.                                                             id : 'TF-LclTime', 
  180.                                                             tooltip : 'Local Time', 
  181.                                                             editable : false, 
  182.                                                             value : '{/current_observation/local_time_rfc822}', 
  183.                                                             width : '220px' 
  184.                                                   }); 
  185.                                                   oLabelLclTime.setLabelFor(oTFLclTime); 
  186.                                                   oMatrix.createRow(oLabelLclTime, oTFLclTime); 
  187.                                                   //Lable and TextField for relative humidity   
  188.                                                   oLabelRelHum = new sap.ui.commons.Label({ 
  189.                                                             id : 'L-RelHum', 
  190.                                                             text : 'Relative Humidity' 
  191.                                                   }); 
  192.                                                   oTFRelHum = new sap.ui.commons.TextField({ 
  193.                                                             id : 'TF-RelHum', 
  194.                                                             tooltip : 'Relative Humidity', 
  195.                                                             editable : false, 
  196.                                                             value : '{/current_observation/relative_humidity}', 
  197.                                                             width : '220px' 
  198.                                                   }); 
  199.                                                   oLabelRelHum.setLabelFor(oTFRelHum); 
  200.                                                   oMatrix.createRow(oLabelRelHum, oTFRelHum); 
  201.                                                   //Lable and TextField for Wind   
  202.                                                   oLabelWind = new sap.ui.commons.Label({ 
  203.                                                             id : 'L-Wind', 
  204.                                                             text : 'Wind' 
  205.                                                   }); 
  206.                                                   oTFWind = new sap.ui.commons.TextField({ 
  207.                                                             id : 'TF-Wind', 
  208.                                                             tooltip : 'Wind', 
  209.                                                             editable : false, 
  210.                                                             value : '{/current_observation/wind_string}', 
  211.                                                             width : '220px' 
  212.                                                   }); 
  213.                                                   oLabelWind.setLabelFor(oTFWind); 
  214.                                                   oMatrix.createRow(oLabelWind, oTFWind); 
  215.                                                   //attach it to some element in the page   
  216.                                                   oMatrix.placeAt('content'); 
  217.                                                   //Create an instance of the table control   
  218.                                                   var oTable1 = new sap.ui.table.Table({ 
  219.                                                             title : "Simple Forecast Details", 
  220.                                                             visibleRowCount : 4, 
  221.                                                             selectionMode : sap.ui.table.SelectionMode.Single, 
  222.                                                             navigationMode : sap.ui.table.NavigationMode.Paginator, 
  223.                                                   }); 
  224.                                                   //Define the columns and the control templates to be used   
  225.                                                   oTable1.addColumn(new sap.ui.table.Column({ 
  226.                                                             label : new sap.ui.commons.Label({ 
  227.                                                                       text : "Period" 
  228.                                                             }), 
  229.                                                             template : new sap.ui.commons.TextView().bindProperty( 
  230.                                                                                 "text", "date/weekday"), 
  231.                                                             width : "10px" 
  232.                                                   })); 
  233.                                                   oTable1.addColumn(new sap.ui.table.Column({ 
  234.                                                             label : new sap.ui.commons.Label({ 
  235.                                                                       text : "Image" 
  236.                                                             }), 
  237.                                                             template : new sap.ui.commons.Image().bindProperty( 
  238.                                                                                 "src", "icon_url"), 
  239.                                                             width : "10px", 
  240.                                                             hAlign : "Center" 
  241.                                                   })); 
  242.                                                   oTable1.addColumn(new sap.ui.table.Column({ 
  243.                                                             label : new sap.ui.commons.Label({ 
  244.                                                                       text : "High" 
  245.                                                             }), 
  246.                                                             template : new sap.ui.commons.TextView().bindProperty( 
  247.                                                                                 "text", "high/celsius"), 
  248.                                                             width : "10px" 
  249.                                                   })); 
  250.                                                   oTable1.addColumn(new sap.ui.table.Column({ 
  251.                                                             label : new sap.ui.commons.Label({ 
  252.                                                                       text : "Low" 
  253.                                                             }), 
  254.                                                             template : new sap.ui.commons.TextView().bindProperty( 
  255.                                                                                 "text", "low/celsius"), 
  256.                                                             width : "10px" 
  257.                                                   })); 
  258.                                                   oTable1.addColumn(new sap.ui.table.Column({ 
  259.                                                             label : new sap.ui.commons.Label({ 
  260.                                                                       text : "Avarage Humidity" 
  261.                                                             }), 
  262.                                                             template : new sap.ui.commons.TextView().bindProperty( 
  263.                                                                                 "text", "avehumidity"), 
  264.                                                             width : "10px" 
  265.                                                   })); 
  266.                                                   oTable1.addColumn(new sap.ui.table.Column({ 
  267.                                                             label : new sap.ui.commons.Label({ 
  268.                                                                       text : "Max Humidity" 
  269.                                                             }), 
  270.                                                             template : new sap.ui.commons.TextView().bindProperty( 
  271.                                                                                 "text", "maxhumidity"), 
  272.                                                             width : "10px" 
  273.                                                   })); 
  274.                                                   oTable1.addColumn(new sap.ui.table.Column({ 
  275.                                                             label : new sap.ui.commons.Label({ 
  276.                                                                       text : "Min Humidity" 
  277.                                                             }), 
  278.                                                             template : new sap.ui.commons.TextView().bindProperty( 
  279.                                                                                 "text", "minhumidity"), 
  280.                                                             width : "10px" 
  281.                                                   })); 
  282.                                                   oTable1.addColumn(new sap.ui.table.Column({ 
  283.                                                             label : new sap.ui.commons.Label({ 
  284.                                                                       text : "Chance of Precipitation" 
  285.                                                             }), 
  286.                                                             template : new sap.ui.commons.TextView().bindProperty( 
  287.                                                                                 "text", "pop"), 
  288.                                                             width : "10px" 
  289.                                                   })); 
  290.                                                   //Create a model and bind the table rows to this model   
  291.                                                   var oModel2 = new sap.ui.model.json.JSONModel(); 
  292.                                                   //Get he forecastday array from simpleforecast object   
  293.                                                   var aSimpleForecast = oModel.getProperty("/forecast/simpleforecast/forecastday"); 
  294.                                                   oModel2.setData({ 
  295.                                                             modelData : aSimpleForecast 
  296.                                                   }); 
  297.                                                   oTable1.setModel(oModel2); 
  298.                                                   oTable1.bindRows("/modelData"); 
  299.                                                   oTable1.placeAt('content'); 
  300.                                                   //Create an instance of the table control   
  301.                                                   var oTable = new sap.ui.table.Table({ 
  302.                                                             title : "Textual Forecast Details", 
  303.                                                             visibleRowCount : 8, 
  304.                                                             selectionMode : sap.ui.table.SelectionMode.Single, 
  305.                                                             navigationMode : sap.ui.table.NavigationMode.Paginator, 
  306.                                                   }); 
  307.                                                   //Define the columns and the control templates to be used   
  308.                                                   oTable.addColumn(new sap.ui.table.Column({ 
  309.                                                             label : new sap.ui.commons.Label({ 
  310.                                                                       text : "Period" 
  311.                                                             }), 
  312.                                                             template : new sap.ui.commons.TextView().bindProperty( 
  313.                                                                                 "text", "title"), 
  314.                                                             width : "50px" 
  315.                                                   })); 
  316.                                                   oTable.addColumn(new sap.ui.table.Column({ 
  317.                                                             label : new sap.ui.commons.Label({ 
  318.                                                                       text : "Image" 
  319.                                                             }), 
  320.                                                             template : new sap.ui.commons.Image().bindProperty( 
  321.                                                                                 "src", "icon_url"), 
  322.                                                             width : "75px", 
  323.                                                             hAlign : "Center" 
  324.                                                   })); 
  325.                                                   oTable.addColumn(new sap.ui.table.Column({ 
  326.                                                             label : new sap.ui.commons.Label({ 
  327.                                                                       text : "Forecast" 
  328.                                                             }), 
  329.                                                             template : new sap.ui.commons.TextView().bindProperty( 
  330.                                                                                 "text", "fcttext_metric"), 
  331.                                                             width : "300px" 
  332.                                                   })); 
  333.                                                   //Create a model and bind the table rows to this model   
  334.                                                   var oModel1 = new sap.ui.model.json.JSONModel(); 
  335.                                                   //Get the forecastday array table from txt_forecast object   
  336.                                                   var aData = oModel.getProperty("/forecast/txt_forecast/forecastday"); 
  337.                                                   oModel1.setData({ 
  338.                                                             modelData : aData 
  339.                                                   }); 
  340.                                                   oTable.setModel(oModel1); 
  341.                                                   oTable.bindRows("/modelData"); 
  342.                                                   oTable.placeAt('content'); 
  343.                                         } 
  344.                               }); 
  345. </script> 
  346. </head> 
  347. <body class='sapUiBody'> 
  348.           <div id='content'></div> 
  349.           <p></p> 
  350.           <div id="footer"> 
  351.                     <div style="float: right; text-align: right;"> 
  352.                               <img src="http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png
  353.                                         border="0" alt=""><br> <span style="padding-top: 20px;">Weather          data provided by Weather Underground, Inc.</span> 
  354.                     </div> 
  355.           </div> 
  356. </body> 
  357. </html>

Viewing all articles
Browse latest Browse all 8124

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>