Adding rows with data-order

Adding rows with data-order

tward392tward392 Posts: 2Questions: 1Answers: 0

I'm using:

  $dataTable.row.add($("<tr><td data-order="1">someformatting 1</td></tr>")[0]);

to add a bunch of rows to a table, basically the someformatting can contain some type of CSS formatting and span's, icons, and things, but the data-order doesn't seem to work correctly.

If I pull the actual table out and put it in jsfiddle the sorting works fine so I assume it has to do with the rows being added one at a time.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 65,628Questions: 1Answers: 10,913 Site admin

    I'll need to look into that - that should have worked. Thanks for letting me know.

    Another option is to add the data using an object, rather than a Node. See this section of the API for details.

    Allan

  • kthorngrenkthorngren Posts: 22,423Questions: 26Answers: 5,156
    edited March 20 Answer ✓

    A few things:

    1. The HTML5 data attributes. for sorting only work with an HTML table and the table must have at least one row when Datatables initializes. This way Datatables will find data-order in the row during initialization to start using it. If data-order is not in the HTML table when initialized then normal ordering of the data in the column will be used. In other words if you are using row.add() with an empty table then data-order won't be used. See the HTML5 orthogonal data docs for more details.
    2. You need to use draw() to have Datatables resort the table after using row.add().
    3. You are using double quotes to enclose the string and within the string you are using double quotes. I'm surprised you aren't getting a syntax error. Try surrounding the string with single quotes, like this:
    $dataTable.row.add($('<tr><td data-order="1">someformatting 1</td></tr>')[0]).draw();
    

    Also note the added draw(). Here is a running example with your code snippet:
    https://live.datatables.net/fabosexi/1/edit

    If you still need help then please provide a test case showing the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • tward392tward392 Posts: 2Questions: 1Answers: 0
    1. Adding a row to the table with data-order="1" seems to have fixed it.
    2. That was a typo, sorry.
Sign In or Register to comment.