如何使用jQuery DataTables插件实现服务器端处理?

时间:2012-07-11 12:40:56

标签: php jquery mysql datatables server-side

我正在使用jQuery DataTables插件来显示一组由多个表组合而来的结果。我试图通过插件实现server-side processing以改善页面加载时间。我无法弄清楚如何推断example DataTables给我的场景。有什么想法/建议吗?

使用Javascript:

$('#results').dataTable({
        "sAjaxSource": "../server_processing.php",  
        "bProcessing": true,
        "bServerSide": true,
        "bDeferRender": false, 
})

原始PHP功能:

function build_data_list(){

    global $org_id;
    global $dbh;
    global $req_ids;
    global $user_list;

    $sth = $dbh->query ("SELECT 
                        l4.name as L4name, 
                        l3.name as L3name, 
                        l2.name as L2name, 
                        l1.name as L1name,
                        u.id,
                        u.last_name,
                        u.first_name,
                        FROM user_grp_indx ugi, groups l4, groups l3, groups l2, groups l1, users p
                        WHERE
                        ugi.user_id = u.id
                        AND l4.id =  ugi.grp_id
                        AND l4.parent = l3.id
                        AND l3.parent = l2.id
                        AND l2.parent = l1.id
                        ORDER BY u.id", PDO::FETCH_ASSOC);

    $row = $sth->fetch();

    $item['user_id'] =  $row['id'];

    $item['user_info'] = '<a href="../users/index.php?pq=';
    $item['user_info'] .= $row['id'].'">';
    $item['user_info'] .= $row['last_name']. ", " . $row['first_name'] . "</a>";    

    $item['l1_name'] = $row['L1name'];
    $item['l2_name'] = $row['L2name'];
    $item['l3_name'] = $row['L3name'];
    $item['l4_name'] = $row['L4name'];

    for ($i=0; $i < sizeof($req_ids) ; $i++ ){
            $item['req'.$i] = (chk_req_status($item['user_id'],$req_ids[$i]) ? "<span title=\"Yes\"></span><img src=\"../../media/icons/tick.png\" alt=\"Yes\" />" :
                                                    "<span title=\"No\"></span><img src=\"../../media/icons/cross.png\" alt=\"No\" />");
    }

    $old_L1id = $row['L1id'];
    $old_user_id = $row['id'];

    while ($row = $sth->fetch()){

        $L1id =  $row['L1id'];
        $user_id =  $row['id'];

        if ($L1id == $old_L1id && $user_id == $old_user_id ){

            $item['l2_name'] .= "<br/>" . $row['L2name'];
            $item['l3_name'] .= "<br/>" . $row['L3name'];
            $item['l4_name'] .= "<br/>" . $row['L4name'];

            continue;

        }   

        $user_list[] = $item;     
        $old_L1id = $L1id; 
        $old_user_id = $user_id;

        $item['user_id'] =  $row['id'];

        $item['user_info'] = '<a href="../users/index.php?pq=';
        $item['user_info'] .= $row['id'].'">';
        $item['user_info'] .= $row['last_name']. ", " . $row['first_name'] . "</a>";  

        //add inital level stuff to the new record.
        $item['l1_name'] = $row['L1name'];
        $item['l2_name'] = $row['L2name'];
        $item['l3_name'] = $row['L3name'];
        $item['l4_name'] = $row['L4name'];  

        for ($i=0; $i < sizeof($req_ids) ; $i++ ){
            $item['req'.$i] = (chk_req_status($item['user_id'],$req_ids[$i]) ? "<span title=\"Yes\"></span><img src=\"../../media/icons/tick.png\" alt=\"Yes\" />" :
                                                    "<span title=\"No\"></span><img src=\"../../media/icons/cross.png\" alt=\"No\" />");
        }

    }

}

原始HTML / PHP:

<tbody>
    <?php foreach($user_list as $item){

        print "<tr>";
        print "<td class=\"hidden\">{$item['user_id']}</td>";
        print "<td>{$item['user_info']}</td>";
        print "<td>{$item['l1_name']}</td>";
        print "<td>{$item['l2_name']}</td>";
        print "<td>{$item['l3_name']}</td>";
        print "<td>{$item['l4_name']}</td>";

        for ($i=0; $i < sizeof($req_ids) ; $i++ ){
            print '<td>'.(  chk_req_status($item['user_id'],$req_ids[$i]) ? "<span title=\"Yes\"></span><img src=\"../../media/icons/tick.png\" alt=\"Yes\" />":
                "<span title=\"No\"></span><img src=\"../../media/icons/cross.png\" alt=\"No\" />").'</td>';
        }

        print "</tr>";  

    } ?>
</tbody>

1 个答案:

答案 0 :(得分:1)

这可以提供任何帮助吗?

Server-side processing | PHP with MySQL

  

服务器端处理| PHP与MySQL   此脚本是DataTables进行测试和开发的基础,因此它始终是最新的,并且将始终使用服务器端处理实现DataTables支持的所有功能(正则表达式过滤除外) - 出于数据库访问速度的原因)。

     

要在您自己的服务器上使用代码,只需更改$ aColumns数组以列出您希望从数据库中包含的列,将$ sIndexColumn设置为索引的列(用于速度),将$ sTable设置为表名,最后将数据库连接参数填入$ gaSql。请注意,此脚本使用的json_encode需要PHP 5.2或更高版本。对于与旧版本兼容的此脚本版本,请参阅PHP 4兼容版本。