最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Appending td to rows with $.each loop JQuery - Stack Overflow

programmeradmin4浏览0评论

I am trying to add data to table cells via an each loop in JQuery. My code for the loop is below.

 $.getJSON(filter, function (data) {
        $.each(data, function (key, val) {
           var entry = $('<tr>').append(
            $.each(val, function (key, val) {      
                $('<td contenteditable="true">').text(val);
            }));

            // var entry = $('<tr>').append(
            //     $('<td contenteditable="true">').text(val.firstName),
            //     $('<td contenteditable="true">').text(val.lastName),
            //     $('<td contenteditable="true">').text(val.address),
            //     $('<td contenteditable="true">').text(val.city),
            //     $('<td contenteditable="true">').text(val.state),
            //     $('<td contenteditable="true">').text(val.zipcode)
            // );
            entry.appendTo(table);

The loop code is not populating the table with the rows. If I run the mented section below the $.each loop instead it populates fine. I can log the value of val in the loop and the data is ing in fine. I can even log to the td Any ideas on why this is not working will be greatly appreciated.

I am trying to add data to table cells via an each loop in JQuery. My code for the loop is below.

 $.getJSON(filter, function (data) {
        $.each(data, function (key, val) {
           var entry = $('<tr>').append(
            $.each(val, function (key, val) {      
                $('<td contenteditable="true">').text(val);
            }));

            // var entry = $('<tr>').append(
            //     $('<td contenteditable="true">').text(val.firstName),
            //     $('<td contenteditable="true">').text(val.lastName),
            //     $('<td contenteditable="true">').text(val.address),
            //     $('<td contenteditable="true">').text(val.city),
            //     $('<td contenteditable="true">').text(val.state),
            //     $('<td contenteditable="true">').text(val.zipcode)
            // );
            entry.appendTo(table);

The loop code is not populating the table with the rows. If I run the mented section below the $.each loop instead it populates fine. I can log the value of val in the loop and the data is ing in fine. I can even log to the td Any ideas on why this is not working will be greatly appreciated.

Share Improve this question asked May 23, 2017 at 15:22 Oscar DulzaidesOscar Dulzaides 1691 silver badge9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

The issue is because you cannot call $.each() from within the append() method - at least not in the manner you're attempting to. You need to first append the tr elements, then add the td to that in the inner loop.

Alternatively you can build the HTML in a single string and call append() once, like this:

$.getJSON(filter, function(data) {
  $.each(data, function(key, val) {
    var tds = val.map(function(value) {
      return ' <td contenteditable="true">' + value + '</td>';
    });
    table.append('<tr>' + tds.join('') + '</tr>');
  });
});

Each method just iterates, you want to give back the element and for that you use map instead of each :

$.map(val, function (key, val) {      
    return $('<td contenteditable="true">').text(val);
})

append can accept a function and will use it's return value. You need to map the results within your append and return that result.

$.each(data, function (key, val) {
    var entry = $('<tr>').append(
        return $.map(val, function (key, val) {      
            return $('<td contenteditable="true">').text(val);
        }));
});
发布评论

评论列表(0)

  1. 暂无评论