javascript - 如何在数据网格中显示由具有属性和值的对象组成的数组?

标签 javascript jquery arrays json datagrid

我有一个由对象组成的数组。每个对象都有属性和值。属性是纬度、经度和名称。我需要在数据 GridView 中显示此数组。下面是我的数组,

enter image description here

谁能分享例子或任何建议

谢谢!

最佳答案

试试这个:

$(function(){
var data = [{
  "Latitude": 18.00,
  "Longitude": 23.00,
  "Name": "Pune"
}, {
  "Latitude": 14.00,
  "Longitude": 24.00,
  "Name": "Mumbai"
}, {
  "Latitude": 34.00,
  "Longitude": -4.00,
  "Name": "Delhi"
},{
  "Latitude": 23.00,
  "Longitude": 23.00,
  "Name": "Jaipur"
}];
$.each(data,function(index,value) {

      row="<tr><td>"+value.Latitude+"</td><td>"+value.Longitude+"</td><td>"+value.Name+"</td><tr>";
  
  $("#grid").append(row);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="grid">
  <tr>
    <th>
      Latitude
    </th>
    <th>
      Longitude
    </th>
    <th>
      Place
    </th>
  </tr>
</table>

动态标题

但只有当你所有的对象都具有相同的属性时它才会起作用

$(function() {
  var data = [{
    "Latitude": 18.00,
    "Longitude": 23.00,
    "Name": "Pune"
  }, {
    "Latitude": 14.00,
    "Longitude": 24.00,
    "Name": "Mumbai"
  }, {
    "Latitude": 34.00,
    "Longitude": -4.00,
    "Name": "Delhi"
  }, {
    "Latitude": 23.00,
    "Longitude": 23.00,
    "Name": "Jaipur"
  }];
  header = "<tr>"
  $.each(data[0], function(prop, value) {
    header += "<th>" + prop + "</th>";
  });
  header += "</tr>"
  $("#grid").append(header);

  $.each(data, function(index, obj) {

    row = "<tr>";
    $.each(obj, function(prop, value) {
      row += "<td>" + value + "</td>";
    });
    row += "</tr>";

    $("#grid").append(row);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="grid">
</table>

关于javascript - 如何在数据网格中显示由具有属性和值的对象组成的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636024/

相关文章:

javascript - 国际重定向脚本不起作用

php - 我的数据库中有超过 100 万条记录。如何更快地分页......?

javascript - 如何在隐藏子元素时删除 div 元素

c# - 将一维编码的二维数组划分为多个扇区

php - 无法在函数 PHP 中获取数组

java - 在对象数组中,可以在索引处创建对象的新实例吗?

javascript - 在每个页面上引导导航栏( Vanilla javascript 解决方案)

javascript - 创建重复值的JS数组

通过 ScriptManager.RegisterStartupScript 调用时 JavaScript 回调函数无法找到控件

jquery - iphone/css - 单击时如何防止黑色半透明覆盖在项目上