Databable搜索所有分页记录

时间:2018-05-23 13:24:09

标签: javascript html datatable

我有一个数据表,我使用Ajax和SQL从数据库中获取数据。我还创建了一些搜索字段,以便搜索数据库中的特定列。

数据表使用分页,在搜索表时,它只搜索页面上显示的行。这是每页10条记录。

function myFunction() {
  var input, filter, table, tr, td, i;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();
  table = document.getElementById("example");
  tr = table.getElementsByTagName("tr");
  for (i = 0; i < tr.length; i++) {
    td = tr[i].getElementsByTagName("td")[4];
    if (td) {
      if (td.innerHTML.toUpperCase().indexOf(filter) > -1) {
        tr[i].style.display = "";
      } else {
        tr[i].style.display = "none";
      }
    }
  }
}
<div class="form-group">
  <label class="sr-only" for="inputEmail">Last name</label>
  <input type="text" id="myInput" onkeyup="myFunction()" class="form-control" placeholder="Last name">
</div>

php code

<?php
/* Database connection start */
$servername = "";
$username = "";
$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 =>'reservation_id',
    1 =>'unit_name',
    2 =>'checkin_agent',
    3 =>'quality_check_agent',
        4 =>'guest_full_name',
        5 =>'source',
        6 =>'no_guests',
        7 =>'checkin_date',
        8 =>'checkout_date',
        9 =>'entered',
        10 =>'checkin_time',
        11 =>'total',
        12 =>'complete',



);

    function getData($sql){
        global $conn ;//we use connection already opened
        $query = mysqli_query($conn, $sql) OR DIE ("Can't get Data from DB , check your SQL Query " );
        $data = array();
        foreach ($query as $row ) {
            $data[] = $row ;
        }
        return $data;
    }


// getting total number records without any search
    $sql = "SELECT reservation_id, unit_name, checkin_agent, quality_check_agent, guest_full_name, source, no_guests, DATE_FORMAT( checkin_date , '%e %b %y') as checkin, DATE_FORMAT( checkout_date , '%e %b %y') as checkout, DATE_FORMAT( entered , '%e %b %y') as entered, checkin_time, total, complete";
    $sql.=" FROM reservations2 INNER JOIN guest USING (guest_id) INNER JOIN unit USING (unit_id) ORDER BY checkin_date";
$query=mysqli_query($conn, $sql) or die('Error, insert query failed with:' . $error);
$totalData = count(getData("SELECT * FROM reservations2 "));
$totalFiltered = count(getData("SELECT * FROM reservations2 ")); // when there is no search parameter then total number rows = total number filtered rows.
if( !empty($requestData['search']['value']) ) {
    // if there is a search parameter
    $sql = "SELECT reservation_id, unit_name, checkin_agent, quality_check_agent, guest_full_name, source, no_guests, DATE_FORMAT( checkin_date , '%e %b %y' ) as checkin, DATE_FORMAT( checkout_date , '%e %b %y') as checkout, DATE_FORMAT( entered , '%e %b %y') as entered, checkin_time, total, complete";

    $sql.=" FROM reservations2 INNER JOIN guest USING (guest_id) INNER JOIN unit USING (unit_id)";
    $sql.=" WHERE total LIKE '".$requestData['search']['value']."%' ";    // $requestData['search']['value'] contains search parameter
    $sql.=" OR total LIKE '".$requestData['search']['value']."%' ";
    $sql.=" OR total LIKE '".$requestData['search']['value']."%' ";
    $query=mysqli_query($conn, $sql) or die("get-table-data.php: get employees");
    $totalFiltered = mysqli_num_rows($query); // modify total number filtered rows as per search result without limit in the query 
    $sql.=" LIMIT ".$requestData['start']." ,".$requestData['length']."   "; 
    $query=mysqli_query($conn, $sql) or die('Error, insert query failed with:' . $error); 
} else {    
    $sql = "SELECT reservation_id, unit_name, checkin_agent, quality_check_agent, guest_full_name, source, no_guests, DATE_FORMAT( checkin_date , '%e %b %y' ) as checkin, DATE_FORMAT( checkout_date , '%e %b %y') as checkout, DATE_FORMAT( entered , '%e %b %y') as entered, checkin_time, total, complete";

    $sql.=" FROM reservations2 INNER JOIN guest USING (guest_id) INNER JOIN unit USING (unit_id) ORDER BY checkin_date";
    $sql.=" LIMIT ".$requestData['start']." ,".$requestData['length']."    ";
    $query=mysqli_query($conn, $sql) or die('Error, insert query failed with:' . $error);
}
$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array(); 
    $nestedData[] = $row["reservation_id"];
    $nestedData[] = $row["unit_name"];
        $nestedData[] = $row["checkin_agent"];
            $nestedData[] = $row["quality_check_agent"];
    $nestedData[] = $row["guest_full_name"];
    $nestedData[] = $row["source"];
    $nestedData[] = $row["no_guests"];
    $nestedData[] = $row["checkin"];
    $nestedData[] = $row["checkout"];
    $nestedData[] = $row["entered"];
    $nestedData[] = $row["checkin_time"];
    $nestedData[] = $row["total"];
    $nestedData[] = $row["complete"];
    $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

?>

1 个答案:

答案 0 :(得分:1)

如果您使用的是php,可以按照以下示例操作: DataTable Server Side Example