0

I want to implement a Datatable into a theme, which gets the data via an Ajax Request. Once the document is loaded, I build the HTML part for the datatable. The problem is: Once I click a sort function (for example sort one row ascending) it uses the original data to sort (which is given in the .php file) instead of the new JQuery loaded datatable. So probably I need to reinitialize the datatable or anything else?

HTML Part:

<tbody id="accountList">
    <!-- List all accounts -->
    <tr>
        <td>username@hostname-de</td>
        <td>PS</td>
        <td>350000</td>
        <td>45000</td>
        <td>Victor Ibarbo</td>
        <td>30 / 30</td>
        <td>224500</td>
        <td><label class="label label-success">Online</label></td>
    </tr>
</tbody>

JQuery part:

function buildAccountList(){
    $.ajax({
        url: "/database/accounts.php",
        type: "POST",
        data: {action: "selectAccounts"},
        success: function (response) {
            var opt = '';
            $.each(response, function(i, e){
                opt +='<tr>';
                opt += '<td>' + e.email + '</td>';
                opt += '<td>' + e.platform + '</td>';
                opt += '<td>' + e.coins + '</td>';
                opt += '<td>' + e.password + '</td>';
                opt += '<td>' + e.tradepileCards + '</td>';
                opt += '<td>' + e.tradepileValue + '</td>';
                opt += '<td>' + e.enabled + '</td>';
                opt += '</tr>';
            });
            $('#accountList').html(opt);
        },
        dataType: "json"
    });
}

The creation of the table works fine, but as I described, once I press a sort function it uses the old table (given by the html file).

kentor
  • 16,553
  • 20
  • 86
  • 144
  • I am using JQuery 1.10 I think. Is there a way to see the "version" of the datatable? – kentor Dec 15 '14 at 18:46
  • DataTables 1.10.2 . But not a problem to change the version if necessary – kentor Dec 15 '14 at 21:38
  • Any chance your /database/accounts.php can return JSON-formatted data? https://datatables.net/examples/data_sources/ajax.html – Jeromy French Dec 15 '14 at 21:42
  • Ah damn I didnt see that this is a old question, I managed this later. But maybe you can help me with my next datatable problem: http://stackoverflow.com/questions/27465239/edit-jquery-datatable-fields . Thanks anyways! – kentor Dec 15 '14 at 21:43
  • Which "old question" is that? You can "close" your question and link to it... – Jeromy French Dec 15 '14 at 21:52
  • possible duplicate of [Update datatable with ajax request](http://stackoverflow.com/questions/27452949/update-datatable-with-ajax-request) – Jeromy French Dec 17 '14 at 20:56

0 Answers0