2017-03-04 14 views
1

以下は、jsonのデータベーステーブルからDatatableにデータをフェッチするためのPHPスクリプトです。Datatable Jquery PHP Mysqlのチェックボックスとすべての機能をチェックする

<?php 
 
/* Database connection start */ 
 
$servername = "localhost"; 
 
$username = "root"; 
 
$password = "password1"; 
 
$dbname = "test"; 
 

 
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error()); 
 

 
/* Database connection end */ 
 

 

 
// storing request (ie, get/post) global array to a variable 
 
$requestData= $_REQUEST; 
 

 

 
$columns = array( 
 
// datatable column index => database column name 
 
\t 0 =>'employee_name', 
 
\t 1 => 'employee_salary', 
 
\t 2=> 'employee_age' 
 
); 
 

 
// getting total number records without any search 
 
$sql = "SELECT employee_name, employee_salary, employee_age "; 
 
$sql.=" FROM employee"; 
 
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); 
 
$totalData = mysqli_num_rows($query); 
 
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows. 
 

 

 
$sql = "SELECT employee_name, employee_salary, employee_age "; 
 
$sql.=" FROM employee WHERE 1=1"; 
 
if(!empty($requestData['search']['value'])) { // if there is a search parameter, $requestData['search']['value'] contains search parameter 
 
\t $sql.=" AND (employee_name LIKE '".$requestData['search']['value']."%' ";  
 
\t $sql.=" OR employee_salary LIKE '".$requestData['search']['value']."%' "; 
 

 
\t $sql.=" OR employee_age LIKE '".$requestData['search']['value']."%')"; 
 
} 
 
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); 
 
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result. 
 
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." "; 
 
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */ \t 
 
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); 
 

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

 
\t $nestedData[] = $row["employee_name"]; 
 
\t $nestedData[] = $row["employee_salary"]; 
 
\t $nestedData[] = $row["employee_age"]; 
 
\t 
 
\t $data[] = $nestedData; 
 
} 
 

 

 

 
$json_data = array(
 
\t \t \t "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. 
 
\t \t \t "recordsTotal" => intval($totalData), // total number of records 
 
\t \t \t "recordsFiltered" => intval($totalFiltered), // total number of records after searching, if there is no searching then totalFiltered = totalData 
 
\t \t \t "data"   => $data // total data array 
 
\t \t \t); 
 

 
echo json_encode($json_data); // send data as json format 
 

 
?>

そして、以下の私のDataTableのページです。

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

私は1つの行または複数または全ての行を選択して送信するために、第1列のチェックボックスを追加しようとmです。

しかし、私はそうすることができません、誰も私にそれをするのを助けることができますか?

答えて

1

jQuery DataTables Checkboxes jQuery DataTablesのプラグインを参照してください。

var table = $('#example').DataTable({ 
    'columnDefs': [ 
     { 
     'targets': 0, 
     'checkboxes': { 
      'selectRow': true 
     } 
     } 
    ], 
    'select': { 
     'style': 'multi' 
    }, 
    'order': [[1, 'asc']] 
}); 
関連する問題