Server-side processing sluggish? - Page 2

Server-side processing sluggish?

2»

Answers

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    I think you mean the draw() method - there is no draw option in DataTables.

    If you want to trigger a search after your keyup action, then yes, you need to call draw() regardless of whether it is server-side or client-side.

    Allan

  • Randy CashRandy Cash Posts: 30Questions: 1Answers: 0

    But is the draw method necessary for the pagination to work?

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    I'm afraid I don't really understand. The draw() method tells DataTables to redraw the table based on the setting configured via the API. The pagination controls will automatically redraw the table themselves (by calling the draw() method).

    Allan

  • Randy CashRandy Cash Posts: 30Questions: 1Answers: 0

    Sorry, I'm probably not doing a very good job of explaining.

    The Javascript code for my table has:

            <script type="text/javascript">
            $(document).ready(function(){
              $('#aadt').DataTable({
                "processing": true,
                "serverSide": true,
                "ajax":{
                  "url": "script.php",
                  "dataSrc": "data",
                  "type": "POST"
                },
                "fixedHeader": true,
                "scrollY": "75vh",
                "scrollX": "900px",
                "scrollCollapse": true,
                "paging": true,
                // "scroller": true,
                // "deferLoading": [50, 7997],
                "colummns": []
              });
            });
            </script>
    

    Despite the fact that I have paging set to true, and as you said, the pagination part of my PHP script looks fine, the pagination doesn't work. Each time I click on a new page, it does send a request to the server, but only returns the same results from page one.

    I do not have any code for a draw method, because I never saw any on the server-side processing example, so I didn't think it was essential.

    Is the reason the table is not re-drawing when I click on the next page because I do not have a draw method in my code?

    Thanks. Sorry for the confusion.

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin
    edited June 2016

    it does send a request to the server, but only returns the same results from page one.

    Good - that's half the battle. You don't need to call draw() if it is already doing the draw for you.

    I believe the issue is what I mentioned in a previous reply:

    Not the page, just the data from it. It shows that draw is being returned as 0 in the JSON data which is not valid. It should always be 1 or higher.

    The JSON data from the server should never have the draw parameter in it as value 0. See the server-side documentation for details about that parameter. (note this parameter has nothing to do with the draw() method - other than a similar name).

    Allan

  • Randy CashRandy Cash Posts: 30Questions: 1Answers: 0
    edited June 2016

    I'll look through the server-side documentation again. I should mention that JsonLint said my JSON data was valid.

  • Randy CashRandy Cash Posts: 30Questions: 1Answers: 0

    Ok, so the draw parameter needs to be sent as part of the data request, and it needs to be cast as an integer. Like this?

    <script type="text/javascript">
            $(document).ready(function(){
              $('#aadt').DataTable({
                "processing": true,
                "serverSide": true,
                "ajax":{
                  "url": "script.php",
                  "dataSrc": "data",
                  "type": "POST",
                  "draw": 1,
                },
                "fixedHeader": true,
                "scrollY": "75vh",
                "scrollX": "900px",
                "scrollCollapse": true,
                "paging": true,
                // "scroller": true,
                // "deferLoading": [50, 7997],
                "colummns": []
              });
            });
            </script>
    
  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    No - DataTables sends a draw parameter and you need to send one back.

    From the manual:

    Returned data
    draw - The draw counter that this object is a response to - from the draw parameter sent as part of the data request.

    Allan

  • Randy CashRandy Cash Posts: 30Questions: 1Answers: 0

    Isn't this where I send the parameter back?

        $output = array(
            "draw" => intval(isset($_GET['draw'])),
            "recordsTotal" => $iTotal,
            "recordsFiltered" => $iFilteredTotal,
            "data" => array()
        );
    
  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    As I mentioned before:

    That the moment you are intval()ing the result of the isset(). I'm not sure what would happen with that!

    Just intval the $_GET['draw'] parameter.

    Allan

  • Randy CashRandy Cash Posts: 30Questions: 1Answers: 0
    edited June 2016

    Ok, changed the code as you suggested. Gave me an undefined index error for 'draw'. I changed $_GET to $_POST, like so:

        $output = array(
            "draw" => intval($_POST['draw']),
            "recordsTotal" => $iTotal,
            "recordsFiltered" => $iFilteredTotal,
            "data" => array()
        );
    
    

    Now "draw" returns a value of 1. However, the table is still only loading the first page with each network request.

    Since I'm server-side processing, and have my type set to 'POST', do I need to change every instance of $_GET to $_POST in the PHP script?

    Thanks.

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin

    If you have the client-side sending POST requests, then yes, you would use $_POST rather than $_GET. Without a link to a test case I can't really say if that is the case or not.

    Allan

  • Randy CashRandy Cash Posts: 30Questions: 1Answers: 0

    Replaced every instance of $_GET with $_POST. Broke my PHP script. Does not like this line:

      $sWhere .= $columns[$i]." LIKE '%".addslashes( $_POST['search'] )."%' OR ";
    

    It gives me the following warning:

    PHP Warning: addslashes() expects parameter 1 to be string, array given in C:\inetpub\wwwroot\script.php on line 111

    Not too familiar with correct PHP syntax. Any suggestions?

  • allanallan Posts: 63,353Questions: 1Answers: 10,444 Site admin
    Answer ✓

    Its really difficult to say what the issue is without being able to see the page. Your code above appears to be sending a POST request - your browser's network tools will tell you if that is actually the case.

    Allan

  • Randy CashRandy Cash Posts: 30Questions: 1Answers: 0

    Yeah, I understand. I wish I had a page to link to. It is indeed sending a post request. Like I said, if I don't replace $_GET with $_POST, it returns the first page, valid json, and a "draw" of 1. It just doesn't paginate. I'm sure the PHP needs more tweaking.

  • Randy CashRandy Cash Posts: 30Questions: 1Answers: 0

    Going to mark this thread as answered. The script loads the data faster, which was the original purpose for the thread. Just have to figure out how to write the correct PHP script to paginate with MSSQL. Thanks Allan.

  • benpeterbenpeter Posts: 14Questions: 5Answers: 0

    @Randy Cash I followed your discussion and i couldnt make my datatable paginate. Did you manage to tweak the script to paginate. Kindly share the code..

This discussion has been closed.