How to prepend data to a dataTable ?
How to prepend data to a dataTable ?
CyrilF
Posts: 10Questions: 2Answers: 0
Hi,
I'm new with dataTable and I'd like to improve a code I got that uses this feature to sort columns. I want to add new rows at the front via AJAX.
I found that the fnAddData() function could do the trick but this is not clear how to use it in the documentation.
Here is my HTML code:
<table id="uses" class="table table-condensed table-striped table-bordered table-hover table-highlight table-checkable"
data-provide="datatable"
data-info="true"
data-search="true">
<thead>
<tr>
<th data-mdata="euroDate" data-sortable="true">Date</th>
<th data-mdata="ignoreColumnAccents" data-sortable="true">Immeuble</th>
<th data-mdata="ignoreColumnAccents" data-sortable="true">Utilisateur</th>
<th data-mdata="ignoreColumnAccents" class="hidden-xs hidden-sm">Terminal</th>
<th data-mdata="ignoreColumnAccents">Machine</th>
<th>Durée</th>
<th>Montant</th>
</tr>
</thead>
<tbody>
....
</tbody>
</table>
And my JS:
$(function() {
$("#uses").dataTable().fnAddData([
"08.02.2016 15:05:18",
"17th O'Connore Street",
"John Doe",
"TH33",
"MM26",
"15'",
"21.0"
], false);
});
I get this error and no clear explanation on why:
DataTables warning (table id = 'uses'): Requested unknown parameter {mData function} from the data source for row 1
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
The current way to add a new row is by using row.add()
https://datatables.net/examples/api/add_row.html
You've confirmed your table to expect objects, so you need to use objects in your fnAddData /
row.add()
call.Not also that the order of the data in the table is entirely defined by the sorting that is applied to the table. If you want the new row to appear at the top of the table, you need to make sure that the data in it will cause it to be sorted to the top.
Allan
Sorry I'm a bit busy these days.
With
I get
My helpers:
That error suggests your function is returning undefined or null for that row / column.
What function ?
Whatever function you have assigned to that column for data - the error message notes that it is a function. Without access to the page I can't really say more I'm afraid.
Allan
Can you paste your DataTable configuration?
The first column uses euroDate() function and the 4 nexts ignoreColumnAccents(). But I cannot figure out how to pass those functions to row.add(). It should simply add the values directly.
Here is the function that init the dataTable:
Utilize following in your columnDef
https://datatables.net/examples/advanced_init/column_render.html
How should I pass columnDef to row.add()?
You don't. The render function should be attached your 'co' variable which is then attached to 'tableConfig.aoColumns.push (co);'
When you add a new row and have assigned a render function to a column, DataTables will use it. Otherwise, it will simply output whatever data was passed to it within your array. Also, within the render function you will call either ignoreColumnAccents() or euroDate();
http://live.datatables.net/miyucida/4/edit?html,js,output
I came up with a quick example to illustrate how I think you could make your DataTable init a lot simpler. As far as your original question about prepending new data, that is where the hidden "Sort Order" column comes into play. Click on the [Add Data] button to see the new data get added and sorted to show up at the top.
Obviously it would be much more complex in your case, but ultimately you want to make any new data have a lower/higher value, depending on your sort direction, than what currently exists for that column in the table.
OK this is more clear for me about this.
But it seems that I have a completely different API. Here is the configuration passed to dataTable() in the initDataTableHelper() function and shown in FireBug for the table tag in my first post:
As you can see, there is no "columnDef", no "render", etc.
aoColumns and aoColumnDefs performs the same functionality, so you are good. Your two functions "euroData" and "ignoreColumnAccents" should be called within your mData function for the appropriate column.
Now referring to your error
There was an issue with the data for the second column.
I tried to pass a function like in the init:
The "15'" and "21.0" cells are correctly added but I still have an issue with the other columns.
I also tried to pass directly the function euroDate() or ignoreColumnAccents() like
With the same error :/
You should not be adding a new row like that. The original way you were adding it is correct.
The way you configured your table and columns applies to current and future rows.
The mData functions attached to your table init should look something like this
If you still have issues, read this link https://legacy.datatables.net/ref#mData and consider using mRender.
I don't understand why you are trying to pass
mData
into therow.add()
function!Just pass the data object you want to use:
Allan
@jr42.gordon : OK.
Yes this is how the configuration was done by initDataTableHelper() with:
using
OK, I'll read it!
@allan : that doesn't work, that's why I'm trying to figure out how to do!
I would strongly recommend you use
columns.render
and notcolumns.data
(ormData
in old terminology). Usingcolumns.data
as a function can get really complicated.OK thank you. You said mData in old terminology so you mean that I should rewrite initDataTableHelper() with the new API?
I would suggest using the 1.10 style if you are using 1.10. A convert guide is available.
It don't fix the issue though - as I say, use
columns.render
, notcolumns.data
(regardless of if you are using the old or new style).Allan
I would recommend using "columns.data" option if all you are doing is displaying/manipulating a single variable data within an object.
I would recommend using "columns.render" option you require additional content within the cell, in addition to the associated object variable data, or if your display/filter/sort values are all different.
Viewing the links provided by Allan will give you good examples to use.