2016-02-21 166 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'),

相关问题