簡體   English   中英

Jquery 數據表單個列搜索

[英]Jquery Datatables Individual column searching

我正在嘗試在 Jquery 中為 Datatables 插件應用單個列搜索(選擇輸入) 我使用的服務器端處理,將數據從SQL Server正確地加載到我的表,但是當我要選擇一個輸入來過濾基於該值一列,你看到的過濾器不工作在這里 當我選擇一個國家(例如奧地利)時,數據不會過濾到奧地利。 這是我的索引頁

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html" charset=utf-8" />
  <title> Datatables Individual column searching using PHP Ajax Jquery </title>

<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css">

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap.min.js"></script>


 </head>
 <body>
  <div class="container">
   <h1 align="center">Datatables</h1>    
   <div class="table-responsive">
<table id="example" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
                <th>Country</th>
                <th>Territory</th>
                <th>Market</th>
                <th>Entname</th>
                <th>MTM</th>
                <th>Qty</th>
            </tr>
        </thead>

        <tfoot>
            <tr>
                <th>Country</th>
                <th>Territory</th>
                <th>Market</th>
                <th>Entname</th>
                <th>MTM</th>
                <th>Qty</th>
            </tr>
        </tfoot>
    </table>
   </div>
  </div>
 </body>
</html>

<script type="text/javascript" language="javascript" >
$(document).ready(function() {

      var dtable = $('#example').DataTable({  
            "processing": true,
            "serverSide": true,
            "deferRender": true,
            "lengthMenu": [ [25, 50, 100, -1], [25, 50, 100, "All"] ],
            "pageLength": -1,
            "lengthChange": true,
            "pagingType": "full_numbers",
            "columns": [
                {"data": "Country"},
                {"data": "Territory"},
                {"data": "Market"},
                {"data": "Entname"},
                {"data": "MTM"},
                {"data": "Qty"}
            ],
            "ajax": {
                url: 'fetch.php',
                type: 'POST'
            },   
            initComplete: function () {
            this.api().columns().every( function () {
            var column = this;
            var select = $('<select><option value=""></option></select>')
                    .appendTo( $(column.footer()).empty() )
                    .on( 'change', function () {
                        var val = $.fn.dataTable.util.escapeRegex(
                            $(this).val()
                        );

                        column
                            .search( val ? '^'+val+'$' : '', true, false )
                            .draw();
                    } );

                column.data().unique().sort().each( function ( d, j ) {
                    select.append( '<option value="'+d+'">'+d+'</option>' )
                } );
            } );
        }

        });

      $(".dataTables_filter input")
    .unbind() 
    .bind("keyup change", function(e) { 
        if(this.value.length >= 1 && e.keyCode == 13) { dtable.search(this.value).draw(); }
        if(this.value == "") { dtable.search("").draw(); }
        return;
    });

} );
</script>

這是我的 fetch.php

<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);


if (!empty($_POST) ) {

$ser="*****";
$db="*****";
$user="*****"; 
$pass="*****";
$dbDB = new PDO("odbc:Driver=ODBC Driver 13 for SQL Server;Server=*****;Database=*****;Port=1456", $user, $pass);
$MyTable="*****";

    function getData($sql){
        global $dbDB ;
        global $MyTable ;
        $result = $dbDB->query($sql);       
        $data= $result->fetchAll(PDO::FETCH_ASSOC);
        return $data; }

        $res = $dbDB->query("SELECT COUNT(*) FROM ".$MyTable);
        $recordsTotal = $res->fetchColumn();

    $draw = $_POST["draw"];
    $orderByColumnIndex  = $_POST['order'][0]['column'];
    $orderBy = $_POST['columns'][$orderByColumnIndex]['data'];
    $orderType = $_POST['order'][0]['dir']; 
    $start  = $_POST["start"];

    if ($_POST['length'] == -1) {$length = $recordsTotal;} 
    else {$length = $_POST['length'];}    


    if(!empty($_POST['search']['value'])){

        for($i=0 ; $i<count($_POST['columns']);$i++){
            $column = $_POST['columns'][$i]['data'];
            $where[]="$column like '%".$_POST['search']['value']."%'";
        }
        $where = "WHERE ".implode(" OR " , $where);


$sql = sprintf("SELECT COUNT(*) FROM %s %s", $MyTable , $where);
$r = $dbDB->query($sql);
$recordsFiltered = $r->fetchColumn();

         $sql = sprintf("SELECT Country,Territory,Market,Entname,MTM,Qty FROM %s %s ORDER BY %s %s OFFSET %d ROWS FETCH NEXT %d ROWS ONLY", $MyTable , $where ,$orderBy, $orderType ,$start,$length);
        $data = getData($sql);
    }

    else {

        $sql = sprintf("SELECT Country,Territory,Market,Entname,MTM,Qty FROM %s ORDER BY %s %s OFFSET %d ROWS FETCH NEXT %d ROWS ONLY", $MyTable ,$orderBy, $orderType ,$start,$length);
          $data = getData($sql);
        $recordsFiltered = $recordsTotal;
    }

        $response = array(
        "draw" => intval($draw),
        "recordsTotal" => $recordsTotal,
        "recordsFiltered" => $recordsFiltered,
        "data" => $data );

    echo json_encode($response);
} 

else {
    echo "NO POST Query from DataTable";
}

?>

我相信問題出在 javascript 部分而不是 php 文件中,只是為了顯示數據如何填充到表格中的概述。

知道問題出在哪里嗎?

檢查下面的例子

 // Setup - add a text input to each footer cell $('#item tfoot th').each( function () { var title = $(this).text(); $(this).html( '<input type="text" placeholder="Search '+title+'" />' ); } ); // DataTable var otable = $('#item').DataTable(); // Apply the search otable.columns().every( function () { var that = this; $( 'input', this.footer() ).on( 'keyup change', function () { if ( that.search() !== this.value ) { that .search( this.value ) .draw(); } } ); } );
 tfoot { display: table-header-group; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> <link rel="stylesheet" type="text/css" href="https://www.jqueryscript.net/demo/DataTables-Jquery-Table-Plugin/media/css/jquery.dataTables.css"> <table name="item" id="item" class="display" width="100%" cellspacing="0"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </thead> <tfoot> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date</th> <th>Salary</th> </tr> </tfoot> <tbody> <tr> <td>Tiger Nixon</td> <td>System Architect</td> <td>Edinburgh</td> <td>61</td> <td>2011/04/25</td> <td>$320,800</td> </tr> <tr> <td>Garrett Winters</td> <td>Accountant</td> <td>Tokyo</td> <td>63</td> <td>2011/07/25</td> <td>$170,750</td> </tr> <tr> <td>Ashton Cox</td> <td>Junior Technical Author</td> <td>San Francisco</td> <td>66</td> <td>2009/01/12</td> <td>$86,000</td> </tr> <tr> <td>Cedric Kelly</td> <td>Senior Javascript Developer</td> <td>Edinburgh</td> <td>22</td> <td>2012/03/29</td> <td>$433,060</td> </tr> <tr> <td>Airi Satou</td> <td>Accountant</td> <td>Tokyo</td> <td>33</td> <td>2008/11/28</td> <td>$162,700</td> </tr> </tbody> </table>

日期過濾器搜索怎么樣,即結束日期>開始日期到這個數據表。它會更方便。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM