2015-11-11 29 views
0

我在我的應用程序中使用引導數據表。我使用服務器端使用Ajax從MySQL數據庫中獲取值。php引導數據表服務器

$(document).ready(function() { 
    var dataTable = $('#employee-grid').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax":{ 
      url :"employee-grid-data.php", // json datasource 
      type: "post", // method , by default get 
      error: function(){ // error handling 
      $(".employee-grid-error").html(""); 
      $("#employee-grid").append('<tbody class="employee-grid-error"><tr><thcolspan="3">No data found in the server</th></tr></tbody>'); 
      $("#employee-grid_processing").css("display","none"); 

     } 
    } 
}); 

在員工電網data.php

while($row=mysqli_fetch_array($query)) { // preparing an array 
    $nestedData=array(); 

    $nestedData[] = $row["employee_name"]; 
    $nestedData[] = $row["employee_salary"]; 
    $nestedData[] = $row["employee_age"]; 

    $data[] = $nestedData; 
} 

$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 

所以,我在找的是 - 我要添加單獨的類,每個TR一行。在數據表中我看不到任何tr。那麼我怎樣才能爲tr添加一個類。 (基於條件,tr的背景顏色不同)。

回答

0

datatables example可能會幫助你,它做一些非常類似於你想要完成的事情。

它使用createdRow init選項來定義一個函數,該函數對每個添加到數據表的行(通過ajax或任何其他源)調用一次。在這個函數中,你可以應用你的邏輯來爲任何符合條件的行添加一個類。

$(document).ready(function() { 
    $('#example').DataTable({ 
     "createdRow": function (row, data, index) { 
      if (data[5].replace(/[\$,]/g, '') * 1 > 150000) { 
       $('td', row).eq(5).addClass('highlight'); 
      } 
     } 
    }); 
}); 

(從數據表取出文檔代碼)