How to reload Table with new Data when bServerSide is true

How to reload Table with new Data when bServerSide is true

thileepanthileepan Posts: 9Questions: 0Answers: 0
edited March 2013 in General
Hi,

I would like to reload the table information with new information which is processed at server side. My code snippet is here,

[code]
function InitTable()
{
$(document).ready(function() {
$('#Table').dataTable( {
"bProcessing": true,
"bServerSide": true,
"sPaginationType": "full_numbers"
"sAjaxSource": url,
"sScrollY": "350px",
"bRetrieve": true,
"fnServerData": fnDataTablesPipeline,
} );
} );
}

function ReLoadTableOnClick()
{
$.ajax({
url: url,
data: { type: "3" },
success: function(data, textStatus){
var oTable = $('#Table').dataTable();
oTable.fnClearTable();
oTable.fnAddData(data.aaData);
oTable.fnDraw();
}
});
}
[/code]


I have added code for "fnDataTablesPipeline" as well. First time request to "InitTable" works great including pipeline pagination but the successive request to "ReLoadTableOnClick" not refresh the table with new information which is coming from server side. It shows the old data itself. Also, I would like to post the server side params like displaylenth for pagination and other params in my second request as well. Please help on this.

thanks

Replies

  • GregPGregP Posts: 500Questions: 10Answers: 0
    edited March 2013
    All you need is oTable.fnDraw().

    The DataTables object itself will be aware of the current pagination status, etc., and send it accordingly. If you need to extend the data being sent (like your type: "3" information), you can use fnServerParams

    If I'm being perfectly honest, I can't remember why, but I also pass "false" in the function. It has something to do with redrawing and filtering:

    [code]
    $('.someButton').on('click', function() {
    oTable.fnDraw(false);
    });
    [/code]
  • thileepanthileepan Posts: 9Questions: 0Answers: 0
    edited March 2013
    Hi GregP,

    Thank you for your quick reply and idea.

    fndraw doesn't work GregP. My goal is, I have to load new datatable information on my second request. The input parameters will differ totally on successive requests. From the Ajax respone, I have to clear my old data and load the new one.

    Also, first request (on initializing datatable) sends pagination, filters parameters but I would like to send all those parameters in my second request as well. Please help me.

    If you need any further information from my side, kindly let me know.

    thanks
  • GregPGregP Posts: 500Questions: 10Answers: 0
    How is the different information retrieved? It looks like it's from the same URL, so in what other way are you modifying which information should be retrieved? On fnDraw, pagination information is sent, along with any filters that have been defined before the draw is called.
  • thileepanthileepan Posts: 9Questions: 0Answers: 0
    In my function "ReLoadTableOnClick()", I will pass multiple parameters as ajax request input. Based on the input parameters, my server side code will reply back.

    fnDraw will not send any ajax request to server right? Please correct me if I am wrong.
  • thileepanthileepan Posts: 9Questions: 0Answers: 0
    Hi,

    Any help please?

    If I simply remove bServerSide then fnDraw() refresh the table with new information. But it doesn't work with bServerSide. But I would like to use bServerSide as true since my table might have huge number of entries.
  • thileepanthileepan Posts: 9Questions: 0Answers: 0
    Hi,

    As of now I understood that, fnDraw & fnClearTable will not work with bServerSide: true. Then what could be the best way to load the datatable with new information on every user click. User will make the selection and load the datatable so based on user selection, I will do the query in my datatabase and will return as json to datatable. First time loading (during datatable initialization) all works well. However, if the user make different selection then I have to load different information in datatable. Currently, to load different information I sending json ajax request. But, I not sure how to use the ajax response (aaData) to load into datatables by wiping the previous data.

    GregP/Allan, can you please help me to get this done.
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    > fnDraw will not send any ajax request to server right? Please correct me if I am wrong.

    Wrong. If you are using server-side processing, then fnDraw will _always_ make a request to the server. The whole point of server-side processing is that the data is held at the server so in order to do a draw it _must_ call the server.

    > Then what could be the best way to load the datatable with new information on every user click.

    Use fnServerParams and fnDraw . fnServerParams can be used to send extra parameters to the server, your selection information and fnDraw to trigger the request.

    Allan
  • thileepanthileepan Posts: 9Questions: 0Answers: 0
    Hi Allan,

    Thanks for looking into this.

    As you said, I tired several different things and I came to know that fnDraw sends request to server only if we didn't use "fnServerData": fnDataTablesPipeline" in datatables. If we use pipeline then request not sending.

    Does this conflicts by any means?

    Also, how can we change the "fnServerParams" value dynamically. Each request to server should hold different paramaters.

    thanks
  • thileepanthileepan Posts: 9Questions: 0Answers: 0
    Hi Allan,

    I understood how to use fnServerParams. Thanks for your help.

    Finally, I would like to achieve the below things. Please let me know the possibility.

    Server-side processing only for pipeline feature but sorting should work on client-side and sorting will only apply to the current page.

    thanks
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    > Does this conflicts by any means?

    Yes - you've got a local cache, so obviously it is going to returned the cached data. If you don't want the cached data either use the pipelining or clear its cache.

    Allan
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    > Server-side processing only for pipeline feature but sorting should work on client-side and sorting will only apply to the current page.

    No - absolutely not. This is not possible in DataTables. You either have sorting, paging and filtering at the client-side or the server-side it cannot be mixed.

    Allan
  • thileepanthileepan Posts: 9Questions: 0Answers: 0
    Thanks Allan. Great Plugin!

    I think using cached data, we can implement client side sorting and filtering. Does this make sense?

    If possible any working sample please.

    thanks once again :)
  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    edited March 2013
    As I say, it is not possible to use mixed server-side and client-side processing.

    Allan
This discussion has been closed.