gpt4 book ai didi

jquery - 如何将 YADCF 实现到服务器端 DataTable 中

转载 作者:行者123 更新时间:2023-12-01 00:18:01 24 4
gpt4 key购买 nike

我正在尝试将列过滤器添加到服务器端 DataTables.js 请求(其使用的数据库很大)。当我从 YADCF 添加行时,我没有文本框的功能。将列过滤器添加到我的 php 文件的正确方法是什么?目前我有:index.php

<head>
<link rel="stylesheet" type="text/css" href="css/jquery.dataTables.css">


<script type="text/javascript" language="javascript" src="js/jquery.js"></script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.yadcf.js"></script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" >
$(document).ready(function() {
'use strict';
var dataTable = $('#employee-grid').DataTable( {
"processing": false,
"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>
<style>
div.container {
margin: 0 auto;
max-width:760px;
}
div.header {
margin: 100px auto;
line-height:30px;
max-width:760px;
}
body {
background: #f7f7f7;
color: #333;
font: 90%/1.45em "Helvetica Neue",HelveticaNeue,Verdana,Arial,Helvetica,sans-serif;
}
</style>
</head>
<body>
<div class="header"></h1></div>
<div class="container">
<table id="employee-grid" cellpadding="0" cellspacing="0" border="0" class="display" width="100%">
<thead>
<tr>
<th>Employee name</th>
<th>Salary</th>
<th>Age</th>
</tr>
</thead> </table> </div></body></html>

和我的sql请求文件:employee-grid-data.php

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

$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

?>

我尝试将我的数据表函数与位于以下位置的 YADCF 函数结合起来: http://yadcf-showcase.appspot.com/DOM_Ajax_Multiple_1.10.html 。但是,我无法使列过滤器工作。 YADCF 服务器端请求的正确语法是什么?或者我必须对 jquery.datatables.js 或 jquery.datatables.yadcf.js 进行更改吗?

我尝试创建变量 oTable,但仍然无法使用 yadcf.init(oTable) 来使文本框发挥作用。我错过了什么?

感谢您的帮助。

最佳答案

请参阅YADCF - Server side source example 。每个列过滤器将输入的值发送为 columns[X][search][value],其中 X 是从零开始的列索引。

因此,在服务器端脚本中包含三列的示例中,您将在以下变量中包含列过滤器:

  • $requestData['列'][0]['搜索']['值']
  • $requestData['列'][1]['搜索']['值']
  • $requestData['列'][2]['搜索']['值']

如果这些过滤器的内容不是空字符串,您需要调整 PHP 以考虑这些过滤器。

关于jquery - 如何将 YADCF 实现到服务器端 DataTable 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30763722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com