storedObjective = "";
storedSignal_type = "";
$.each(data, function(index, key) {
if (key.objective == storedObjective)
{
print_1 = "";
}
else
{
print_1 = key.objective;
storedObjective = key.objective;
}
if (key.signal_type == storedSignal_type)
{
print_2 = "";
}
else
{
print_2 = key.signal_type;
storedSignal_type = key.signal_type;
}
$('#myTable').append('<tr><td>'+print_1+'</td><td>'+print_2+'</td><td>'+key.signal_name+'</td></tr>');
console.log(print_1);
console.log(print_2);
if (print_1==="" && print_2==="")
{
$('tr').addClass('yes')
}
else{
$('tr').addClass('no')
}
});
代码的所有第一部分都可以正常工作。但最后,我的if
始终评估为true
,即使我console.log
这些变量肯定不是。python manage.py test
。我在这里缺少什么?
答案 0 :(得分:3)
您正在为每个现有行分配类(是/否),因此如果if
匹配一次,则所有现有表行将获得该类 - 如果else
匹配则相同。您可以简化append
并在那里添加课程。
var className = print_1==="" && print_2==="" ? 'yes' : 'no';
$('#myTable').append('<tr class="' + className + '"><td>'+print_1+'</td><td>'+print_2+'</td><td>'+key.signal_name+'</td></tr>');
答案 1 :(得分:2)
您需要将yes / no类仅添加到for each中的当前元素中,否则如果任何其他元素满足条件,则每个tr
将具有yes / no。
这里,只给当前元素一个类:
let $nextRow = $('<tr><td>'+print_1+'</td><td>'+print_2+'</td> <td>'+key.signal_name+'</td></tr>');
if (print_1==="" && print_2==="")
$nextRow.addClass('yes');
else
$nextRow.addClass('no');
$('#myTable').append($nextRow);
var data = [
{
"objective": "Awareness",
"signal_type": "Efficiency",
"signal_name": "CPM"
},
{
"objective": "Awareness",
"signal_type": "Quality",
"signal_name": "Click-thru Rate"
},
{
"objective": "Awareness",
"signal_type": "Quality",
"signal_name": "Frequency"
},
{
"objective": "Awareness",
"signal_type": "Quality",
"signal_name": "Interaction Rate"
},
{
"objective": "Awareness",
"signal_type": "Quality",
"signal_name": "Viewability"
},
{
"objective": "Awareness",
"signal_type": "Volume",
"signal_name": "Display Time"
},
{
"objective": "Awareness",
"signal_type": "Volume",
"signal_name": "Impression"
},
{
"objective": "Awareness",
"signal_type": "Volume",
"signal_name": "Reach"
},
{
"objective": "Conversion",
"signal_type": "Efficiency",
"signal_name": "Cost per Action"
},
{
"objective": "Conversion",
"signal_type": "Efficiency",
"signal_name": "ROI/ROAS"
},
{
"objective": "Conversion",
"signal_type": "Quality",
"signal_name": "Avg Order Value"
},
{
"objective": "Conversion",
"signal_type": "Volume",
"signal_name": "Conversion"
},
{
"objective": "Conversion",
"signal_type": "Volume",
"signal_name": "Revenue"
},
{
"objective": "Engagement",
"signal_type": "Efficiency",
"signal_name": "Cost per Click"
},
{
"objective": "Engagement",
"signal_type": "Quality",
"signal_name": "Avg Interaction Time"
},
{
"objective": "Engagement",
"signal_type": "Quality",
"signal_name": "Conversion Rate"
},
{
"objective": "Engagement",
"signal_type": "Volume",
"signal_name": "Click"
},
{
"objective": "Engagement",
"signal_type": "Volume",
"signal_name": "Interaction"
},
{
"objective": "Engagement",
"signal_type": "Volume",
"signal_name": "Interaction Time"
}
];
storedObjective = "";
storedSignal_type = "";
$.each(data, function(index, key) {
if (key.objective == storedObjective)
{
print_1 = "";
}
else
{
print_1 = key.objective;
storedObjective = key.objective;
}
if (key.signal_type == storedSignal_type)
{
print_2 = "";
}
else
{
print_2 = key.signal_type;
storedSignal_type = key.signal_type;
}
let $nextRow = $('<tr><td>'+print_1+'</td><td>'+print_2+'</td> <td>'+key.signal_name+'</td></tr>');
if (print_1==="" && print_2==="")
{
$nextRow.addClass('yes');
}
else{
$nextRow.addClass('no');
}
$('#myTable').append($nextRow);
});
.yes {
background-color: green;
}
.no {
background-color: red;
color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myTable">
</table>