复选框和检查Datatable Jquery PHP Mysql中的所有功能

Posted

技术标签:

【中文标题】复选框和检查Datatable Jquery PHP Mysql中的所有功能【英文标题】:Checkbox & Check all function in Datatable Jquery PHP Mysql 【发布时间】:2017-07-24 13:13:39 【问题描述】:

下面是从json中的数据库表获取数据到Datatable的php脚本;

<?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
	0 =>'employee_name', 
	1 => 'employee_salary',
	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
	$sql.=" AND ( employee_name LIKE '".$requestData['search']['value']."%' ";    
	$sql.=" OR employee_salary LIKE '".$requestData['search']['value']."%' ";

	$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  */	
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

$data = array();
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

?>

下面是我的数据表页面;

		<script type="text/javascript" language="javascript" >
			$(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><th colspan="3">No data found in the server</th></tr></tbody>');
							$("#employee-grid_processing").css("display","none");
							
						
					
				 );
			 );
		</script>
	

我正在尝试在第一列中添加复选框以选择一行或多行或所有行并提交。

但是我做不到,谁能帮我做一下?

【问题讨论】:

【参考方案1】:

见jQuery DataTables Checkboxes jQuery DataTables 插件。

var table = $('#example').DataTable(
   'columnDefs': [
      
         'targets': 0,
         'checkboxes': 
            'selectRow': true
         
      
   ],
   'select': 
      'style': 'multi'
   ,
   'order': [[1, 'asc']]
);

【讨论】:

以上是关于复选框和检查Datatable Jquery PHP Mysql中的所有功能的主要内容,如果未能解决你的问题,请参考以下文章

如何从 jQuery DataTable 中的所有页面中选择所有复选框

当前页面的 JQuery DataTables 列复选框

带有复选框控件的 ASP.Net GridView 和 Jquery dataTable

带有Rails的DataTable无法提交多个页面项目

如何根据列值对每一行显示复选框 - JQuery DataTable

带有复选框的 JQuery DataTable 选择行