Datatables - reload table using HTML

Datatables - reload table using HTML

ArudianArudian Posts: 11Questions: 5Answers: 1

HI,

I am working on some legacy code.

Basicly how it works is first time table is generated, table content is generated like this

                    <tbody>
                        @Html.Raw(ViewBag.HTML)
                    </tbody>

ViewBag.HTML = is simply an html string returned by a controller ("<tr><td class=\" details-control2\"></td><td> ....).

Next datatable is initilized with all of its settings and some custom functions run on this table.

Now through Ajax request i'can retrive new 'html' string for tbody part, and with Jquery i replace old table content like this

$("#someTable tbody").html(response);

Data gets replaced fine, but non of the custom functions (like adding urls to some fields, or changing values to % exists anymore. I would like to be able to reintilize datatable based on its new content.

Tried couple of things but nothing really worked for me

fnDraw(); - redraws table with old values.

Running functions after new data is loaded won't work as it needs to be run during intialization.

I also need this to be reloaded without refreshing the page.

rewriting code to not return this weird html string would take a long time, so i would appriciate any tip on how can this be 'reloaded'

Best regards,

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin
    Answer ✓

    Hi,

    You need to use clear() to remove the old rows and then rows.add() to add the new rows.

    Allan

This discussion has been closed.