paging server side

paging server side

mmontoyammontoya Posts: 84Questions: 27Answers: 4

I don't understand how to add paging when using Server Side. I don't seem to find a complete working example in the references.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Working example.

    Reference documentation. The key is to return the required parameters such as recordsTotal as detailed in the documentation.

    Allan

  • mmontoyammontoya Posts: 84Questions: 27Answers: 4

    Sorry, I am not a great programmer, have learned from Google and trial and error :)

    Where is the "magic" happening in the working examples? I don't see where it is telling it the number of records, pages, etc. Is it: SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )

    I am needing to build the Where statement using a form that is above the grid so I chose to use sessions. To get something to display I just put in a phony number of records at the end. Do I need to use the SSP command as in the example you provided?

    ```
    <?php
    require_once('config.php');
    //$staffID = access_control();
    $where = '';
    $dockDate = $_SESSION["dockDate"];
    $raNumber = $_SESSION["raNumber"];
    $ocNumber = $_SESSION["ocNumber"];
    $trackNumber = $_SESSION["trackNumber"];
    $cmNumber = $_SESSION["cmNumber"];
    $delim = '';

    if ($dockDate !='') { 
            $where = "LoadingDockDate='" . $dockDate . "'"; 
            $delim = " and ";
    }
    if ($raNumber !='') { 
            $where .= $delim . "RANumber like '%". $raNumber . "%'"; 
            $delim = " and ";
    }
    if ($ocNumber !='') { 
            $where .= $delim . "OCNumber like '%" . $ocNumber . "%'"; 
            $delim = " and ";
    }
    if ($trackNumber !='') { 
            $where .= $delim . "TrackingNumber like '%" . $trackNumber . "%'"; 
            $delim = " and ";
    }
    if ($cmNumber !='') { 
            $where .= $delim . "CreditMemoNumber='$cmNumber'"; 
            $delim = " and ";
    }
    
    if ($where =='') {
        $where = "LoadingDockDate='1700-01-01'";
    }
    $_SESSION['where'] = $where;
    
    $sql = "Select
                RAHeader.RA_ID,
                RAHeader.RANumber,
                RAHeader.OCNumber,
                RAHeader.OldRANumber,
                RAHeader.LoadingDockDate,
                CartonHeader.TrackingNumber,
                concat(CartonHeader.Qty , (case when IsPallet=1 then ' pallet' else '' end)) as QtyOut,
                CreditMemo.CreditMemoNumber,
                CreditMemo.CreditMemoDate
            From 
                RAHeader
                left Join CartonHeader on CartonHeader.RA_ID=RAHeader.RA_ID
                left Join CreditMemo on CreditMemo.RA_ID=RAHeader.RA_ID
            Where " . $where; 
    $result = $mysqli->query($sql);
    echo '{"data":[';
    $delim='';
    while ($row = $result->fetch_assoc())
    {
        echo $delim.json_encode($row);
        $delim=',';
    }   
    echo '],"options":[],"draw":1,"recordsTotal":"3","recordsFiltered":"3"}';
    
    <?php > ``` ?>
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    I don't see where it is telling it the number of records, pages, etc. Is it: SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )

    Correct. If you have a look at the SSP class it builds three different SQL statements and executes them to get the required information.

    Allan

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Do I need to use the SSP command as in the example you provided?

    No - you can write your own code, but you need to implement the protocol as described in the documentation.

    Allan

  • mmontoyammontoya Posts: 84Questions: 27Answers: 4

    Ok, it is showing the page navigation buttons, but way too many nav buttons needed for the small returned recordset, and instead of showing only 10 it is showing the entire recordset (75 records).

    Here is what is being sent:

    draw:1
    columns[0][data]:
    columns[0][name]:
    columns[0][searchable]:false
    columns[0][orderable]:false
    columns[0][search][value]:
    columns[0][search][regex]:false
    columns[1][data]:RANumber
    columns[1][name]:
    columns[1][searchable]:true
    columns[1][orderable]:true
    columns[1][search][value]:
    columns[1][search][regex]:false
    columns[2][data]:OCNumber
    columns[2][name]:
    columns[2][searchable]:true
    columns[2][orderable]:true
    columns[2][search][value]:
    columns[2][search][regex]:false
    columns[3][data]:OldRANumber
    columns[3][name]:
    columns[3][searchable]:true
    columns[3][orderable]:true
    columns[3][search][value]:
    columns[3][search][regex]:false
    columns[4][data]:LoadingDockDate
    columns[4][name]:
    columns[4][searchable]:true
    columns[4][orderable]:true
    columns[4][search][value]:
    columns[4][search][regex]:false
    columns[5][data]:TrackingNumber
    columns[5][name]:
    columns[5][searchable]:true
    columns[5][orderable]:true
    columns[5][search][value]:
    columns[5][search][regex]:false
    columns[6][data]:QtyOut
    columns[6][name]:
    columns[6][searchable]:true
    columns[6][orderable]:true
    columns[6][search][value]:
    columns[6][search][regex]:false
    columns[7][data]:CreditMemoNumber
    columns[7][name]:
    columns[7][searchable]:true
    columns[7][orderable]:true
    columns[7][search][value]:
    columns[7][search][regex]:false
    columns[8][data]:CreditMemoDate
    columns[8][name]:
    columns[8][searchable]:true
    columns[8][orderable]:true
    columns[8][search][value]:
    columns[8][search][regex]:false
    columns[9][data]:RA_ID
    columns[9][name]:
    columns[9][searchable]:true
    columns[9][orderable]:true
    columns[9][search][value]:
    columns[9][search][regex]:false
    columns[10][data]:CartonHeaderID
    columns[10][name]:
    columns[10][searchable]:true
    columns[10][orderable]:true
    columns[10][search][value]:
    columns[10][search][regex]:false
    order[0][column]:1
    order[0][dir]:asc
    start:0
    length:10
    search[value]:
    search[regex]:false
    

    and here is the response:

    data: [{RA_ID: "26207", CartonHeaderID: "27796", RANumber: "180053", OCNumber: "", OldRANumber: "",…},…]
    draw: 1
    options: []
    recordsFiltered: "75"
    recordsTotal: "75"
    

    if I am sending length: 10, why do i get the entire recordset showing on the first page? and when I click any of the nav buttons I get the same recordset showing.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Answer ✓

    recordsFiltered and recordsTotal should be integers - per the documentation.

    if I am sending length: 10, why do i get the entire recordset showing on the first page?

    Because whatever is doing the processing on the server-side is returning all 75 rather than just the 10 it should be.

    Allan

  • mmontoyammontoya Posts: 84Questions: 27Answers: 4

    I found the SSP js file but I am not sure how to use the various functions in it with my code. How do I tell the server to get records 11-20?

  • mmontoyammontoya Posts: 84Questions: 27Answers: 4
    edited February 2016

    ok. I am almost there. I figured out to use 'Limit' in the sql command.

    So there are 75 records total that need to be returned, 10 per page.
    but when I do this:

        $sql .= " Where " . $where; 
        $result = $mysqli->query($sql);
        $totalRecords = $result->num_rows;
    
        $sql .= " Limit " . $_POST['start'] . ','. $_POST['length'];
        $result = $mysqli->query($sql);
        $filteredRecords = $result->num_rows;
    
        echo '{"data":[';
        $delim='';
        while ($row = $result->fetch_assoc())
        {
            echo $delim.json_encode($row);
            $delim=',';
        }   
        echo '],"options":[],"draw":'.$draw.',"recordsTotal":'.$totalRecords.',"recordsFiltered":'.$filteredRecords.'}';
    
    

    I get this:
    Showing 1 to 10 of 10 entries (filtered from 75 total entries)

  • mmontoyammontoya Posts: 84Questions: 27Answers: 4

    Ahh, I think I get it. The filtered is if I were to pass a search, not the number being returned for the page.

        $sql .= " Where " . $where; 
        $result = $mysqli->query($sql);
        $totalRecords = $result->num_rows;
    
        $sql .= " Limit " . $_POST['start'] . ','. $_POST['length'];
        $result = $mysqli->query($sql);
    
        echo '{"data":[';
        $delim='';
        while ($row = $result->fetch_assoc())
        {
            echo $delim.json_encode($row);
            $delim=',';
        }   
        echo '],"options":[],"draw":'.$draw.',"recordsTotal":'.$totalRecords.',"recordsFiltered":'.$totalRecords.'}';
    
  • mmontoyammontoya Posts: 84Questions: 27Answers: 4

    Ok, here is an example of the server side code using the parameters sent (search text box, column order)

    //I have an initial search form the user submits before data is loaded
    //   The search form $_POST gets put into $where
    //   I never return the entire recordset
        $sql .= " Where " . $where; 
        $result = $mysqli->query($sql);
        $totalRecords = $result->num_rows;
    
    //has the user entered anything into the DataTable search textbox?
    //searchField is a concat of all the fields visible in the DataTable
        $search = $_POST['search'];
        if (!empty($search['value'])) {
            $sql .= " Having searchField like '%".$search['value']."%'"; 
        }
    
    //has the user clicked on column header to change sort?
        $orderBy = $_POST['order'];
        $orderCol =  intval($orderBy[0]['column']);
            $sql .= " order by " . $orderCol . " " . $orderBy[0]['dir']; 
    
        $result = $mysqli->query($sql);
        $filteredRecords = $result->num_rows;   
    
    //paging of data    
        $sql .= " Limit " . $_POST['start'] . ','. $_POST['length'];
        $result = $mysqli->query($sql);
        
    //return json to dataTables 
        echo '{"data":[';
        $delim='';
        while ($row = $result->fetch_assoc())
        {
            echo $delim.json_encode($row);
            $delim=',';
        }   
        echo '],"options":[],"draw":'.$draw.',"recordsTotal":'.$totalRecords.',"recordsFiltered":'.$filteredRecords.'}';
    
This discussion has been closed.