我正在使用DataTable创建交互式表。我有9列,其中5个是价值观。我想根据它们的具体情况更改每个单元格的背景颜色。Datatables:根据值更改单元格颜色
我已经开始尝试改变整个行颜色,因为这看起来更容易。但是我无法得到任何改变。
我的代码如下:
<head>
<link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.13/css/jquery.dataTables.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.js"></script>
<script>
$(document).ready(function(){
$('#countryTable').DataTable();
});
</script>
<script>
$(document).ready(function() {
$('#countryTable ').DataTable({
"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
if (aData[3] > 11.7)
{
$(nRow).css('color', 'red')
}
else if (aData[2] == "4")
{
$(nRow).css('color', 'green')
}
}
});
</script>
</head>
<body>
<table id ="countryTable" class="display" cellspacing="0" data-page-length='193'>
<thead>
<tr>
<th>Rank</th>
<th>Country</th>
<th>Code</th>
<th>Total</th>
<th>Beer</th>
<th>Wine</th>
<th>Spirits</th>
<th>Other</th>
<th>Score</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Estonia</td>
<td>EE</td>
<td>14.97</td>
<td>5.87</td>
<td>1.65</td>
<td>5.64</td>
<td>1.81</td>
<td>3 - Medium Risky</td>
</tr>
<tr>
<td>2</td>
<td>Belarus</td>
<td>BY</td>
<td>14.44</td>
<td>2.5</td>
<td>0.75</td>
<td>6.73</td>
<td>4.46</td>
<td>4 - Very Risky</td>
</tr>
</tbody>
我也使用下面的函数尝试,但没有运气。 如果任何人都可以帮助它会非常appriciataed,因为我非常想要Java脚本。
$(document).ready(function() {
$('#countryTable').DataTable({
"fnRowCallback": function(nRow, aData, iDisplayIndex, iDisplayIndexFull) {
if (aData[3] == "5")
{
$('td', nRow).css('background-color', 'Red');
}
else if (aData[3] == "4")
{
$('td', nRow).css('background-color', 'Orange');
}
}
});
尝试见状http://datatables.net/release-datatables/examples/advanced_init/row_callback.html – MVCNoob
当你使用'DataTable'初始化时使用1.10特定函数 – philantrovert