2016-02-21 167 views
0

我需要幫助jQuery插件DataTables服務器端處理和過濾。過濾DataTables服務器端處理

我只是不知道如何獲取顯示的數據僅限於特定的post_id。

當前有請求查看數據時,用戶鏈接到諸如www.example-site.com/post?id=123之類的東西,目標是讓DataTables僅顯示id = 123的數據。

我只能設置它只是從一個特定的表中拉出一切 - 我不知道如何告訴datatables只使用過濾器的id。

下面是HTML:

<script type="text/javascript" language="javascript" class="init"> 
    $(document).ready(function() { 
    $('#research').DataTable({ 
    "processing": true, 
    "serverSide": true, 
    "ajax": "server_processing.php" 
    }); 
}); 
</script> 

</head> 

<body> 

<table id="research" class="display" cellspacing="0" width="100%"> 

<thead> 
    <tr> 
     <th>Title</th> 
     <th>Link</th> 
     <th>Description</th> 
     <th>Type</th> 
     <th>Posted</th> 
    </tr> 
</thead>  
</table> 

這裏是SQL:

<?php 

$table = 'example_table'; 

$primaryKey = 'id'; 

$columns = array(
    array('db' => 'title', 'dt' => 0), 
    array('db' => 'link', 'dt' => 1), 
    array('db' => 'description', 'dt' => 2), 
    array('db' => 'category',  'dt' => 3), 
    array(
    'db'  => 'post_date', 
    'dt'  => 4, 
    'formatter' => function($d, $row) { 
     return date('M d, Y', strtotime($d)); 
     } 
    ) 
); 

$sql_details = array(
    'user' => '*USER*', 
    'pass' => '*PW*', 
    'db' => 'example_database', 
    'host' => 'localhost' 
); 

require('ssp.class.php'); 

echo json_encode(
    SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns) 
); 

?> 

我不是通過貿易程序員,但我通常能搞清楚該怎麼做了約搜索線上。我在這個完全損失...任何幫助將不勝感激。

回答

0
  1. 首先,你需要使用任何pure JavascriptjQuery-URL-Parser插件來獲得id查詢字符串變量在Javascript。
  2. 然後,您需要將此變量傳遞給ajax選項。例如:"ajax": "server_processing.php?id=" + id
  3. 最後,你需要篩選結果server_processing.php此查詢字符串變量設置使用$_SERVER['QUERY_STRING']

    $query_string = $_SERVER['QUERY_STRING']; 
    parse_str($query_string, $query_string_array); 
    
    ... 
    
    require('ssp.class.php'); 
    $where = "id = '".htmlspecialchars($query_string_array['id'], ENT_QUOTES)."'"; 
    
    echo json_encode(
        SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns, $where) 
    ); 
    

你可以發現更多的信息上$_SERVERhere

+0

我已經完成了以下內容:(1)檢索與'purl.js id'查詢字符串變量; (2)設置'ajax'選項來接收變量:''server_processing.php?id =「+ id';(3)設置** server_processing.php **來過濾查詢字符串變量。我知道我確實正在檢索'id'查詢字符串,並且** server_processing.php **可以過濾查詢字符串。我不知道的是,我是否正確地將'id'變量傳遞給了ajax選項,以及ajax是否正確發送了查詢字符串。有沒有辦法可以檢查ajax是否發送查詢? – bafox

+0

....我注意到我的腳本攔截器(ublock)已打開。它阻止了這個請求。你的解決方案工作 - 謝謝你。 – bafox

-2

$(document).ready(function() { 
 
    $('#example').DataTable({ 
 
     "order": [[ 0, 'desc' ]], // https://datatables.net/reference/option/order cara agar default descending 
 
     "iDisplayLength": 10, 
 
     "aLengthMenu": [[3,5, 10, 25, 50, -1], [3,5, 10, 25, 50, "All"]], 
 
     "processing": true, 
 
     "serverSide": true, 
 
     "ajax": { 
 
    \t \t "url": 'prosesmydevice.php', 
 
    \t \t "type": "GET", 
 
    \t \t "data": {"stdev_id": get_id} 
 
     }, 
 
     "scrollX":  true, 
 
     "scrollCollapse": true, 
 
     "paging":   true, 
 
     \t "stateSave": true,
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest')) { 
 
    
 
    //$getidloc=$_GET['action']; 
 
    $stdev_id=$_GET['stdev_id']; 
 

 
    // nama table 
 
    $table = 'stdevice'; 
 

 
    // Table's primary key 
 
    $primaryKey = 'id'; 
 

 
    // Array of database columns which should be read and sent back to DataTables. 
 
    // The `db` parameter represents the column name in the database, while the `dt` 
 
    // parameter represents the DataTables column identifier. In this case simple 
 
    // indexes 
 

 
    $columns = array( array('db' => 'devn.dev_name', 'dt' => 0, 'field' => 'dev_name'), 
 
         array('db' => 'std.dev_model', 'dt' => 1, 'field' => 'dev_model'), 
 
         array('db' => 'std.dev_serial', 'dt' => 2, 'field' => 'dev_serial'),

相關問題