![](/img/trans.png)
[英]datatable colour changing of rows based on condition in php codeigniter
[英]datatable ajax colour changing of rows based on condition in php
嗨,大家好,我有一个从ajax加载的数据表。
我需要在某些情况下更改行颜色。
像这样的脚本
$(document).ready(function() {
var dataTable = $('#example').DataTable( {
processing: true,
serverSide: true,
ajax: "ajax.php" // json datasource
} );
} );
像这样的HTML
<table id="example" >
<thead>
<tr>
<th>#</th>
<th>ID</th>
<th>Name</th>
<th>Status</th>
</tr>
</thead>
</table>
在ajax.php中
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $i;
$nestedData[] = $row["id"];
$nestedData[] = $row["name"];
$nestedData[] = $row["status"];
$data[] = $nestedData;
$i++;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format
它有效,但是我需要根据状态更改行颜色。
如果status == 1则为黄色,如果status == 2则为红色,如果status == 3则为blue。如何以这种格式添加此表行颜色。
您需要使用“ fnRowCallback”,如下所示:
$(document).ready(function() {
var dataTable = $('#example').DataTable({
processing: true,
serverSide: true,
ajax: "ajax.php",
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
if ( aData[3] == "1" )
{
$('td', nRow).css('background-color', 'Yellow');
}
else if ( aData[3] == "2" )
{
$('td', nRow).css('background-color', 'Red');
}
else if ( aData[3] == "3" )
{
$('td', nRow).css('background-color', 'Blue');
}
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.