当我使用for循环时,在
for (var h = 0; h < xt.length; h++) {
var heart = '';
console.log(xt[h]);
if (row[0] == xt[h]) {
heart = 'icon-heart';
} else {
heart = 'icon- heart1';
}
}
但是当我使用return指令时,循环停止了:
for (var h = 0; h < xt.length; h++) {
var heart = '';
console.log(xt[h]);
if (row[0] == xt[h]) {
heart = 'icon-heart';
} else {
heart = 'icon-heart1';
}
return '<i class="' + heart + '" onClick="addFAv(this)" data-id="' + row[0] + '" data-name="' + row[1] + '"></i> <a href="#" class="icon-line-clipboard i-circled i-small"></a> <a href="#" class="icon-file-alt2 i-circled i-small"></a>';
}
你知道为什么吗?
函数中的整个代码是
$('#recevabilite').dataTable({
"scrollY": "200px",
"scrollCollapse": true,
"paging": false,
stateSave: true,
"dom": '<"#recev.top">irt',
"columnDefs": [{
"visible": false,
"targets": [3]
},
{
"render": function(data, type, row) {
// var date1 = new Date()+10J;
//affichage des icones
var x = localStorage.getItem("listeFavoris");
xt = x.split(',');
for (var h = 0; h < xt.length; h++) {
var heart = '';
console.log(xt[h]);
if (row[0] == xt[h]) {
heart = 'icon-heart';
} else {
heart = 'icon-heart1';
}
return '<i class="' + heart + '" onClick="addFAv(this)" data-id="' + row[0] + '" data-name="' + row[1] + '"></i> <a href="#" class="icon-line-clipboard i-circled i-small"></a> <a href="#" class="icon-file-alt2 i-circled i-small"></a>';
}
谢谢
发布于 2019-06-13 04:08:08
当您执行return
时,它将结束函数,因此您不会执行循环的任何进一步迭代。
您需要连接每次循环迭代的值,然后在循环后返回最终结果:
"render": function(data, type, row) {
// var date1 = new Date()+10J;
//affichage des icones
var x = localStorage.getItem("listeFavoris");
var xt = x.split(',');
var html = '';
for (var h = 0; h < xt.length; h++) {
var heart = '';
console.log(xt[h]);
if (row[0] == xt[h]) {
heart = 'icon-heart';
} else {
heart = 'icon-heart1';
}
html += '<i class="' + heart + '" onClick="addFAv(this)" data-id="' + row[0] + '" data-name="' + row[1] + '"></i> <a href="#" class="icon-line-clipboard i-circled i-small"></a> <a href="#" class="icon-file-alt2 i-circled i-small"></a>';
}
return html;
}
发布于 2019-06-15 05:00:15
我已经找到解决方案了
{"render": function(data, type, row) {
// var date1 = new Date()+10J;
//affichage des icones
var x = localStorage.getItem("listeFavoris");
var xt = x.split(',');
var html = '<a href="#" class="icon-line-clipboard i-circled i-small"></a> <a href="#" class="icon-file-alt2 i-circled i-small"></a>';
for (var h = 0; h < xt.length; h++) {
var heart = '';
console.log(xt[h]);
if (row[0] == xt[h]) {
heart = 'icon-heart';
h = xt.length; }
html += '<i class="' + heart + '" onClick="addFAv(this)" data-id="' + row[0] + '" data-name="' + row[1] + '"></i>';
}
if (heart == '') {
html += '<i class="icon-heart1" onClick="addFAv(this)" data-id="' + row[0] + '" data-name="' + row[1] + '"></i>';}
return html;
}, "targets": 4}
https://stackoverflow.com/questions/56568936
复制相似问题