简体   繁体   English

服务器端数据表中的分页不起作用(未更改URL)

[英]pagination in server side datatable not working (without changing the URL)

i have a datatable for which my jquery is as follows, 我有一个数据表,我的jquery如下,

$('#example').DataTable({
    "lengthMenu": [[10,15,20,25,-1], [10,15,20,25,'All']],
    "processing": true,
    "serverSide": true,
    ajax: {
        url: 'url2',
        dataSrc: 'data'
    },
    columns: [
        { data: 'column1' },
        { data: 'column2' },
        { data: 'column3' },
        { data: 'column4' },
        { data: 'column5' },
        { data: 'column6' }
    ]
}); 

this is working properly. 这工作正常。 The only issue is although its showing pagination links, they are not clickable and all the rows are displayed in first page itself. 唯一的问题是,尽管它显示了分页链接,但它们不可单击,并且所有行都显示在首页本身中。

for eg. 例如 if there are 100 rows, the links are getting generated 1-10 but all the 100 records are showing on the first page itself. 如果有100行,则会生成1-10个链接,但所有100条记录都显示在首页上。

I've referred , 我已提及
https://datatables.net/examples/data_sources/server_side https://datatables.net/examples/data_sources/server_side

what am i doing wrong here? 我在这里做错了什么?
Thank you for your suggestions 谢谢你的建议

Server side code - 服务器端代码-

$total_records = $this->model2->getTotal();

$query['results'] = $this->model1->get_Data();


$data = array('data' => $query['results'],
                                "draw" =>  (isset($_REQUEST["draw"]) ? $_REQUEST["draw"] : 0),
                                "recordsTotal" =>  $total_records,
                                "recordsFiltered" => $total_records
                                 );
echo json_encode($data);  

I think i know what am i doing wrong, when i print $_GET in my php code it comes out to be empty. 我想我知道我在做什么错,当我在PHP代码中打印$ _GET时,结果为空。 But it is supposed to have the limit and offset value. 但是它应该具有极限值和偏移值。 How to i send limit offset in $_GET? 如何在$ _GET中发送限价抵消?

You need to know how many records go on a page, and how many records there are. 您需要知道页面上有多少条记录,以及有多少条记录。

Your SQL will end in OFFSET 30 LIMIT 10 (for example). 例如,您的SQL将以OFFSET 30 LIMIT 10结尾。

First get total Records. 首先获得总记录。 Page will come through your URL and default to page 1 if not set. 页面将通过您的URL进入,如果未设置,则默认为页面1。

The offset is calculated like this (example, page 3): 偏移量的计算如下(示​​例,第3页):

$totalPages = ceil($totalRecords / $numPerPage); // round up the way.
$offset = ($page * $numPerPage) - $numPerPage; // (3 * 10 = 30) - 10 = offset 20

Your SQL will therefore grab records with OFFSET 20 LIMIT 10 因此,您的SQL将使用OFFSET 20 LIMIT 10获取记录

This is what your server.php should looks like. 这就是您的server.php外观。 Think you are missing some lines of code: 认为您缺少一些代码行:

  <?php
    $table = 'employees';
    $primaryKey = 'id'; // Table's primary key

    $columns = array(
        array( 'db' => 'id', 'dt' => 0 ),
        array( 'db' => 'first_name', 'dt' => 1 ),
        array( 'db' => 'last_name',  'dt' => 2 ),
        array( 'db' => 'position',   'dt' => 3 ),
        array( 'db' => 'date',     'dt' => 4 ),
         array( 'db' => 'updated',     'dt' => 5 ),
    );

    $sql_details = array(
        'user' => 'username',
        'pass' => 'password',
        'db'   => 'database',
        'host' => 'localhost'
    );

    require( 'ssp.class.php' );

    echo json_encode(
        SSP::simple( $_POST, $sql_details, $table, $primaryKey, $columns )
    );
    ?>

End result: https://databasetable-net.000webhostapp.com/ 最终结果: https : //databasetable-net.000webhostapp.com/

This should get you in the right direction. 这应该为您指明正确的方向。 Think the ssp.class.php file might need added too, unsure if you have it (you can find it on github). 认为可能也需要添加ssp.class.php文件,不确定是否有(可以在github上找到)。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM