What is the best way to completly reload a DataTable?

What is the best way to completly reload a DataTable?

nunonuno Posts: 14Questions: 0Answers: 0
edited June 2012 in DataTables 1.9
Hi!

I have a DataTable that is created after a request to the server using the "sAjaxSource".
I would like to periodically call the server and reload the latest response updating the table. I would like to have the table updated or rebuilt without it disappearing and also that the user sorting options would be kept.

What would be the best approach?

Thx in advance :)

Replies

  • koosvdkolkkoosvdkolk Posts: 169Questions: 0Answers: 0
    Personally, I would make AJAX-requests, and add/remove rows with fnAddData/fnDeleteRow.
  • Taylor514ceTaylor514ce Posts: 74Questions: 8Answers: 0
    http://datatables.net/forums/discussion/8074/scroll-datatable-to-bottom#Item_6

    Use the modified fnReloadAjax() API call in that thread. The modification is to get / set the scrollTop() of the table so that on each reload it remembers the previous scroll position.
  • koosvdkolkkoosvdkolk Posts: 169Questions: 0Answers: 0
    Ok, but take care: In general it is not a good idea to modify the core code of a third party library...
  • Taylor514ceTaylor514ce Posts: 74Questions: 8Answers: 0
    The fnReloadAjax() function is just that, a function which the user must write or copy from this site in order to use. It isn't part of the "library" in that it isn't part of the distribution, and using it requires no modification to any of the .js files included in Datatables. To use it you must insert the 30-or-so lines of code it comprises into your own markup. Adding 2 more lines to give you the desired functionality isn't "modifying the core code of a... library", in my opinion. But I will concede the point that care must always be taken.
  • nunonuno Posts: 14Questions: 0Answers: 0
    Hi! I've copied and executed your code and it simply works!
    Thanks a lot Taylor! :)
  • Taylor514ceTaylor514ce Posts: 74Questions: 8Answers: 0
    You're welcome, though my contribution to the final solution was minimal. Isn't Datatable fantastic?
  • nunonuno Posts: 14Questions: 0Answers: 0
    I was gonna say yes, it's fantastic but the truth is I've found quite a number of issues when combining different features.

    For example, I'm using the details row feature which means I add a column to the table containing the expansion controls. This is done within the fnInitComplete callback.

    When I then call the fnReloadAjax() function I would assume datatables knows about it but it doesn't...
    I had to manually remove the details column:


    [code]
    reload:function(){
    $j('th:first-child, td:first-child').remove()
    oTable.fnReloadAjax()
    FL.create_details_col()
    },
    [/code]

    I've also had to given up the rows grouping because it doesn't work well together with sorting and the details rows...

    So, I'd say it's quite good but not yet fantastic...
  • nunonuno Posts: 14Questions: 0Answers: 0
    The code I posted doesn't really work as the AJAX call is assynchronous... working on it!
This discussion has been closed.