Sorting DataTables
Sorting DataTables
SteveScott517
Posts: 2Questions: 1Answers: 0
I was following this tutorial and I am still having trouble sorting.
My HTML is here. I am dynamically entering these rows.
... <td class="price" data-original="{{entry[1]}}" data-order="{{entry[1]}}">{{entry[1]}}</td>
I do some calculations and get this value
I change the value here:
$(sellPriceTd).data('order', price2);
where price2 is a floating point number.
My table definition is here:
$('#mainTable').dataTable( {
stateSave: true,
"dom":'<<"#filter"f><"#lenght_field"l>r<t>ip>',
"iDisplayLength": 100,
"aaSorting": [[ 4, "asc" ]],
"columnDefs":[
{"type": "string"},
{"type": "string"},
{
targets:[3],
data:{
_: "3.display",
type: "3.@data-order",
sort: "3.@data-order"
},
...
}
Any feedback is appreciated. This is vexing my website.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
In general I think you will need to use
$(sellPriceTd).attr('data-order', price2);
instead of$(sellPriceTd).data('order', price2);
. From the jQuery data() method docs:DataTables does not know about the changes made directly to the HTML via jQuery. You will need to tell DataTables about the change using the
invalidate()
method then to redraw the table usingdraw()
. You can usecell().invalidate()
orrow().invalidate()
depending on your selector.Something like this might work:
If this doesn't help please put together a test case for help in troubleshooting.
As a side note the default sort is the fifth column ("aaSorting": [[ 4, "asc" ]],) but you are setting the data-order attribute on the forth column. This may be expected, just wanted to point out that the above won't change the table order if a different column is being sorted.
Kevin
Wow thanks! My tables are sorting now, phew! The invalidation and redraw really helped.
Could you explain the sorting a bit? The way I understand it is aaSorting sets the default sorting, and data:{sort: "<col>.@data-order") tell it to sort by the data-order attribute? I want an initial default sorting, with the option to override by clicking on the column.
You are correct and you should have the option to sort by clicking on the columns. Do you not?
Doesn't look like you posted your full Datatables config. Do you have
ordering
or its legacy equivalentbSort
set tofalse
?Kevin