简体   繁体   English

jQuery附加到HTML表

[英]jQuery append to HTML table

I am trying to populate a blank table with data returned from Ajax request. 我试图用从Ajax请求返回的数据填充空白表。 The table populates inside the correct columns, but all the data data is clumped up (in a long string) and all the interest rate data is clumped up (in a long string). 该表填充在正确的列中,但是所有数据数据都聚集在一起(长字符串),所有利率数据都聚集在一起(长字符串)。 Despite this the Console.log() line works perfect and shows each item, enumerated, all on seperate lines. 尽管如此, Console.log()行仍然可以完美地工作,并在单独的行上显示枚举的每个项目。

My code is below: 我的代码如下:

HTML 的HTML

<table id="table" border="1">
    <tr>
        <th>Date</th>
        <th>Value</th>
    </tr>
    <tr>
        <td id='col1'></td>
        <td id='col2'></td>
    </tr>
  </table>

Javascript/jQuery Javascript / jQuery

$.getJSON("api", function(data) {
     for (var x =1; x <= data.count -1 ; x++){
         $("#col1").append(data.observations[x].date);
         $("#col2").append(data.observations[x].value);
         console.log(x, data.observations[x].date, data.observations[x].value);
      }
 })

How can I rewrite it so that each date and interest rate is on a seperate row. 我该如何重写它,以便每个日期和利率都在单独的行上。 Example: row1:date1 and value1, row2: date2 and value2, etc.. 示例:row1:date1和value1,row2:date2和value2,依此类推。

PS Answer should include $.getJSON(api, data) and NOT included $.parseJSON(data) or $.each(data)) or success: function (data)) PS答案应包括$.getJSON(api, data) ,不包括$.parseJSON(data)$.each(data))success: function (data))

Try dynamically generating a new <tr> row for each result within your table. 尝试为表中的每个结果动态生成一个新的<tr>行。 Example below will also give you unique id's for each new <td> column dynamically added eg col-1, col-2 etc. 下面的示例还将为您动态添加的每个新<td>列(例如col-1, col-2等)提供唯一的ID。

 var i = 1; var j = 2; for( i=1; i<=10; i++) { $("#table").append("<tr><td id='col-" + i + "'>" + "col-" + i + "</td><td id='col-" + j + "'>" + "col-" + j + "</td></tr>"); i++; j = j+2; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table" border="1"> <tr> <th>Date</th> <th>Value</th> </tr> </table> 

And your script would be something like: 您的脚本将类似于:

$.getJSON("api", function(data) 
{
    var x = 1;
    var z = 2;

     for (x =1; x <= data.count -1 ; x++){
         $("#table").append("<tr><td id='col" + x + "'>" + data.observations[x].date + "</td><td id='col" + z + "'>" + data.observations[x].value + "</td></tr>");

         x++;
         z = z+2;
      }
 });

 $.getJSON("api", function(data) { var $table = $("#table") for (var x =1; x <= data.count -1 ; x++){ var $row = $("<tr>"), dateCell = $("td").text(data.observations[x].date), valueCell = $("td").text(data.observations[x].value) $row.append(dateCell).append($valueCell) $table.append($row) console.log(x, data.observations[x].date, data.observations[x].value); } }) 
 <table id="table" border="1"> <tr> <th>Date</th> <th>Value</th> </tr> <tr> <td id='col1'></td> <td id='col2'></td> </tr> </table> 

(since I don't have access to your API, this is completely untested) (由于我无权访问您的API,因此未经测试)

Here's a solution with a sample JSON file. 这是带有示例JSON文件的解决方案。

JS Fiddle DEMO JS Fiddle演示

HTML Code: HTML代码:

<table id="table" border="1">
    <thead>
      <tr>
          <th>Date</th>
          <th>Value</th>
      </tr>
    </thead>
    <tbody>

    </tbody>
  </table>

JS: JS:

  onSuccess: function (response) {
            response.forEach(function(row) {
        $('table#table tbody').append("<tr class='tablerow'><td class='col1'>"+row.date+"</td><td class='col2'>"+row.value+"</td></tr>");
      });
  }

You may have to change the HTML table as well (ie separate the header and body by thead and tbody). 您可能还必须更改HTML表(即,用thead和tbody分隔标题和正文)。 Above code will add rows based on the response length. 上面的代码将根据响应长度添加行。 Hope this helps. 希望这可以帮助。

here is my method, make a template and then replace the variables with the values, I have commented out the main lines and made a mockup demo. 这是我的方法,创建一个模板,然后用值替换变量,我注释掉了主行并制作了一个模型演示。

 //$.getJSON("api", function(data) { var data = [1,2,3,4]; for (var x of data){ //var html = `<tr><td>$1</td><td>$2</td></tr>`.replace('$1', data.observations[x].date).replace('$2', data.observations[x].value); var html = `<tr><td>$1</td><td>$2</td></tr>`.replace('$1', 1).replace('$2', 2); $('#table').append(html); //console.log(x, data.observations[x].date, data.observations[x].value); } //}) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="table" border="1"> <tr> <th>Date</th> <th>Value</th> </tr> </table> 

Append a row and cells for each records : 为每个记录追加一行和单元格:

for (var x=0 ; x<data.count ; x++){
     var obs = data.observations[x];
   $('<tr/>').append(
    $("<td/>").text(obs.date),
    $("<td/>").text(obs.value)
  ).appendTo("#table")
}

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

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