each
jqueryループの結果を別のループ内に連結したいのですが、期待した結果が得られません。
$.each(data, function () {
counter++;
var i = 0;
var singlebar;
var that = this;
tableRow = '<tr>' +
'<td>' + this.foo + '</td>' + //<---- should terminate this instead
$.each(this.bar, function(){
singlebar = '<td>' + that.bar[i].baz + '</td>';
tableRow += singlebar;
console.log(singlebar); // outputs "<td>the correct data</td>"
});
'</tr>';
$(tableRow).appendTo('#mainTable > tbody');
});
ネストされた内部の部分each
は、返される文字列に追加されません。
コンソールで期待される結果を得ることができconsole.log(singlebar)
ますが、プライマリeach
ループ内でそれらの結果を連結することはできません。
私も試しました:
$.each(this.bar, function(){
tableRow += '<td>' + that.bar[i].baz + '</td>';
});
これも、目的のコンテンツを追加しません。
each
このネストされたデータを繰り返し処理し、プライマリステートメントが構築しているテーブルの真ん中に追加するにはどうすればよいですか?
解決策(sushanth reddy に感謝)
このような方法で連結しようとし続けるべきではありませんでした。tableRow
一度にすべてではなく、文字列の各セクションを作成時に追加する必要がありました。もっと似たもの:
tableRow = '<tr>' +
'<td>' + this.foo + '</td>';
$.each(this.bar, function(){
singlebar = '<td>' + that.bar[i].baz + '</td>';
tableRow += singlebar;
});
tableRow += '</tr>';