2016-04-06 15 views
2

こんにちは、私はajaxからデータテーブルの読み込みがあります。
ある条件に対して行の色を変更する必要があります。このdatatable ajaxの色PHPの条件に基づいて行の変更

$(document).ready(function() { 
     var dataTable = $('#example').DataTable({ 
      processing: true, 
      serverSide: true, 
      ajax: "ajax.php" // json datasource 

     }); 
    }); 

それが動作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 

でこの

<table id="example" > 
<thead> 
<tr> 
<th>#</th> 
<th>ID</th> 
<th>Name</th> 
<th>Status</th> 
</tr> 
</thead> 

</table> 

のようなHTMLが、私は何を必要とするような
スクリプトは、状況に応じて、行の色を変更することです。
ステータスが== 1の場合は黄色、ステータスが== 2の場合は赤、ステータスが== 3の場合は赤、このタイプのフォーマットではこのテーブル行の色を追加します。次のように

+0

データテーブルのロード後は、各ループ機能を使用することができますし、それぞれの行で、ステータス列の値を取得し、それが列の値ですチェックして、あなたの状態を比較し、それが満たされます色を変更しました – Divyesh

答えて

3

あなたが「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'); 
      } 
     } 
    }); 
}); 
+0

ステータスを表示して同じ結果が必要な場合はどうしたらいいですか? –

+0

["visible":false、 "targets":3}] '' columnDefs 'を追加してください – Manikiran

+0

あなたの答えをありがとう): –

関連する問題