Pagination and AJAX

Pagination and AJAX

JAzevedoJAzevedo Posts: 8Questions: 0Answers: 0
edited March 2013 in General
Hi,

I'm using datatables and I don't if it's possible to load only some row from database and have pagination from all of them. The ideia is to load and show, for example, 5 rows and when we click on 2, of the pagination, we get the next 5 rows with AJAX.

Is this possible?

Best regards,
JA

Replies

  • JAzevedoJAzevedo Posts: 8Questions: 0Answers: 0
    Anyone can help me?
  • rage10940rage10940 Posts: 48Questions: 0Answers: 0
    So your asking if you can do ajax powered data, and maintain pagination? if so do this :



    [code]

    "sPaginationType": "full_numbers",
    "iDisplayLength": 10,
    "aLengthMenu": [5, 10],
    "sAjaxDataProp": "waiting",
    "sAjaxSource": 'https://www.finaidtest.com/index.php/studentqueue_controller/ajaxcall',

    [/code]

    now if your using fnreloadajax you must pass these paramters to maintain your state :

    [code]

    setInterval(function() {
    table1.fnReloadAjax(null,null,true);
    }, 1000); < == one second reload

    [/code]

    hopefully this helps
  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    Or you might be describing server-side processing: http://datatables.net/usage/server-side

    See also: http://datatables.net/usage/#data_sources

    Allan
  • JAzevedoJAzevedo Posts: 8Questions: 0Answers: 0
    Thanks for the responses.
    what I was looking for it's something similar to the server side, but I'm using an external ajax call to insert rows in my datatable, with the fnAddData. In this case, is it possible to do something like server side?

    best regards,
    JA
  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    You can make your own Ajax call with fnServerData - but fnAddData cannot be used with a server-side processing table.

    Allan
  • JAzevedoJAzevedo Posts: 8Questions: 0Answers: 0
    The problem is that I don't understand how can I update the table with the fnServerData. With the fnAddData it's simple because I control and insert the data, from the ajax request, on the datatable as I want.
  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    fnServerData lets you make the Ajax call, as you can see int eh documentation, and you simply pass back the data you want to the passed in callback function. That callback is effectively fnAddData, with a few changes needed for server-side processing.

    Allan
  • JAzevedoJAzevedo Posts: 8Questions: 0Answers: 0
    edited April 2013
    Once again, thanks for the reply.

    I'm trying to implement by my self a simple "program" to understand the Server-side processing plugin.

    I have this html:

    [code]


    $(document).ready(function(){

    $('#table').dataTable({

    "bJQueryUI": true,
    "iDisplayStart": 20,
    "iDisplayLength": 10,
    "bProcessing": true,
    "bServerSide": true,
    "sAjaxSource": "ajax.php",
    "sPaginationType": "full_numbers",
    "oLanguage": {
    "sInfo": "Got a total of _TOTAL_ entries to show (_START_ to _END_)",
    "sSearch": "Search:",
    "sLengthMenu": "Display _MENU_ records",
    "oPaginate": {
    "sPrevious": "Previous"
    }
    },
    });



    });











    Column 1
    Column 2






    <!-- -->

    [/code]

    And my ajax.php is that:

    [code]<?php

    session_start();

    if(isset($_SESSION['sEcho']))
    $_SESSION['sEcho']=$_SESSION['sEcho']+1;
    else
    $_SESSION['sEcho']=1;

    $count = 0;
    $data = array();

    for ($i=0;$i<100;$i++) {

    $teste[] = ['hello'.$i, 'hellox'.$i];
    $count = $count+1;

    }

    /*
    * Paging
    */

    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {

    $ini = $_GET['iDisplayStart'];
    $count = $_GET['iDisplayLength'];

    $i = 0;
    while ($i<$count) {

    $data[] = $teste[$ini];
    $ini = $ini+$i;

    }

    }

    if(!$data) {

    $data = $teste;
    }



    $output = array(
    "sEcho" => intval($_SESSION['sEcho']),
    "iTotalRecords" => $count*10,
    "iTotalDisplayRecords" => 10,
    "aaData" => array()
    );

    foreach($data as $t) {

    $output['aaData'][] = $t;

    }

    echo json_encode($output);

    ?>[/code]

    But I get an error with a message "DataTables warning (table id='table'): DataTables Warning: JSON data from server could not be parsed. This is caused by a JSON formating error".

    When I comment this part of the code:

    [code]/*
    * Paging
    */

    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {

    $ini = $_GET['iDisplayStart'];
    $count = $_GET['iDisplayLength'];

    $i = 0;
    while ($i<$count) {

    $data[] = $teste[$ini];
    $ini = $ini+$i;

    }

    }[/code]

    I don't have the error message, but I don't understand why. I declare the iDisplayStart and the iDisplayLength in my html file.
  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    Look at the return from the server in Firebug or Inspector. There will be a JSON error. What it is - I have no idea.

    Allan
  • JAzevedoJAzevedo Posts: 8Questions: 0Answers: 0
    edited April 2013
    I fix the problem... I used this code: [code]"fnServerData": function ( sSource, aoData, fnCallback ) {
    jQuery.ajax( {
    "dataType": 'json',
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": fnCallback
    } );
    }[/code]

    But, the iDisplayStart and the iDisplayLength are null and this is impossible to entry in the if cycle. I realize that the problem is in the way that I declare iDisplayStart.
  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    If all you need is to use POST then use sServerMethod - http://datatables.net/release-datatables/examples/server_side/post.html

    Allan
  • JAzevedoJAzevedo Posts: 8Questions: 0Answers: 0
    Hi Allan,

    Thanks for the replies, that helped me alot.
    In fact, now I can get the value that I have posted in my .js file.
    But, one last question...How can I get the value of the pagination to parse to my .php file by aoData?

    Best regards,
    JA
  • allanallan Posts: 63,389Questions: 1Answers: 10,449 Site admin
    I don't quite understand - the pagination information required is contained within iDisplayStart and iDisplayLength .

    Allan
  • JAzevedoJAzevedo Posts: 8Questions: 0Answers: 0
    I made a mistake (I was reseting the aoData values). Now everything looks fine. Thanks for everything :)
This discussion has been closed.