数据表ajax颜色根据php中的条件更改行

时间:2016-04-06 08:12:14

标签: php mysql ajax datatable

大家好我从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.how我添加这种格式的表格行颜色。

1 个答案:

答案 0 :(得分:3)

您需要使用“fnRowCallback”,如下所示:

getFirstData()
.then(getSecondData)
.catch(function(err){Throw err}) // catch the error if it throws
.then(createPage);