简体   繁体   English

JSON数组到HTML表并在td标签内显示数据

[英]JSON array to an HTML table and display the data inside td tag

I have json array return as bellow 我有JSON数组返回为波纹管

{"id":16,"minutes":146}
{"id":17,"minutes":137}
{"id":18,"minutes":123}
{"id":22,"minutes":84}

I'm trying to render above json array inside table tbody td which json array id's equal to td id's and display the minutes inside td tag 我正在尝试在表tbody td内的json数组上方渲染,其中json数组id等于td id并在td标签内显示分钟

for example json id :16 minute:146 and display it in <td id="16">146</td> 例如json id:16分钟:146并显示在<td id="16">146</td>

    <table>
      <thead>
        <tr>
          <th>op</th>
          <th>Minutes</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>op1</td>
          <td id="16">0</td>
        </tr>
        <tr>
          <td>op2</td>
          <td id="17">0</td>
        </tr>
        <tr>
          <td>op3</td>
          <td id="18">0</td>
        </tr>
<!--....and goes on -->
      </tbody>
    </table>

js js

$.ajax({ url: statUrl, type: 'POST',
  data: {
      begin: begin,
      end: end
  },
  success: function(data){


  }
});

Your JSON is invalid it should only represent one object, a valid version of what you have will be 您的JSON无效,它只应代表一个对象,即您所拥有的有效版本

[{"id":16,"minutes":146},
{"id":17,"minutes":137},
{"id":18,"minutes":123},
{"id":22,"minutes":84}]

If your data IDs directly correspond to already existing DOM element IDs then it should be rather easy: 如果您的数据ID直接对应于已经存在的DOM元素ID,则应该非常简单:

for (var i = 0;  i < data.length; i++) {
   $('#' + data[i].id).text(data[i].minutes);
}

This is using jQuery ofc. 这是使用jQuery ofc。

you can use json_decode($json, true) in php to convert the json to an array then loop over it's elements and build your table. 您可以在php中使用json_decode($ json,true)将json转换为数组,然后遍历其元素并构建表。

If you want to do it client side, I think you must create table, tr and td elements manually and populate them. 如果要在客户端执行此操作,我认为您必须手动创建table,tr和td元素并填充它们。 ExtJS has ready grid for this. ExtJS为此已经准备好网格。

Server side is easier. 服务器端更容易。

I created a jsFiddle here: http://jsfiddle.net/5pKjW/11/ As Musa stated, the JSON you posted is not valid, it should be an array containing all the objects. 我在这里创建了一个jsFiddle: http : //jsfiddle.net/5pKjW/11/正如Musa所说,您发布的JSON无效,它应该是包含所有对象的数组。 The code following is basically what you need to do inside the success callback, just use data instead of result . 以下代码基本上是您需要在成功回调中执行的操作,仅使用data而不是result What I do is creating a table, appending a row for every element of the array and then appending the whole table to an element of the DOM. 我要做的是创建一个表,为数组的每个元素添加一行,然后将整个表添加到DOM的元素。

var result = [{"id":16,"minutes":146},{"id":17,"minutes":137},{"id":18,"minutes":123},{"id":22,"minutes":84}];
var $table = $('<table><thead><tr><th>op</th><th>Minutes</th></tr></thead>'), 
    $tbody = $('<tbody>').appendTo($table),
    i;
for (i = 0; i < result.length; i++){
    $tbody.append('<tr><td>op' + (i+1) + '</td><td id="' + result[i].id + '">0</td></tr>');
}
$table.appendTo('#container');

As TJ Crowder commented, a valid HTML4 id attribute can't start with a digit. 正如TJ Crowder所说,有效的HTML4 id属性不能以数字开头。 If I were you, I would prefix it with a string ( <td id="prefix' + result[i].id + '">0</td> ). 如果我是你,我会给它加上一个字符串( <td id="prefix' + result[i].id + '">0</td> )。

MrOBrian suggested to use a rendering engine. MrOBrian建议使用渲染引擎。 Maybe for a simple case like this, and if you don't need a rendering engine elsewhere, it's an overkill, but that's absolutely something worth considering, if you need something more complicated in the future. 也许对于像这样的简单情况,如果您在其他地方不需要渲染引擎,那就太过分了,但是如果将来需要更复杂的东西,那绝对值得考虑。

as suggested fixed json array to 如建议的固定json数组

{
 "25":72.3833,
 "17":116.3167,
 "16":25.75,
 "34":28.3333,
 "29":136.8831,
 "19":40.9166,
 "32":43.6,
 "22":83.9001
}

and js 和js

$.getJSON(statUrl, {begin: begin, end: end}, function(data) {

      $.each(data, function(key, val) {
          $('#op_' + key).text(Math.ceil(val));//also fixed td id
      });
});

so got the result as expected. 所以得到了预期的结果。 thanks for your time. 谢谢你的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM