Reputation: 53
I'm new with plugin DataTables in js, and i didn't find how i can select specific data to be sorted in a column. For example, in my table, i've a column with "Rating". I'd like to sort it only with the percentage and not with the other values.
<td>
<span class="rating">100.00%</span>
<span class="voteup">3 <img src='/images/voteup.png' alt='voteup' /></span>
<span class="votedown"> 0 <img src='/images/votedown.png' alt='votedown' /></span>
<br />
<span class="comment">0 comments</span> <br/>
<span class="views">17 views</span>
</td>
I load data directly from the dom (generated by php), here is my DataTables generation in js.
var oTable;
$(document).ready(function() {
oTable = $('#BuildList').dataTable({
"aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
"iDisplayLength": -1,
"aoColumns": [
{ "bSortable": false},
{ "bSortable": false},
{ "bSortable": false},
{ "asSorting": [ "asc" ] },
{ "asSorting": [ "desc" ] },
]
});
// To sort by default the column 4
oTable.fnSort([[3, 'asc']]);
});
Upvotes: 1
Views: 778
Reputation: 53
Problem solved
$.fn.dataTableExt.oSort['rating-desc'] = function(x,y) {
x = parseFloat($(x).first().html());
y = parseFloat($(y).first().html());
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$(document).ready(function() {
oTable = $('#BuildList').dataTable({
"aLengthMenu": [[25, 50, 100, -1], [25, 50, 100, "All"]],
"iDisplayLength": -1,
"aoColumns": [
{ "bSortable": false},
{ "bSortable": false},
{ "bSortable": false},
{ "asSorting": [ "asc" ] },
{ "asSorting": [ "desc" ], "sType": "rating" }
]
});
// To sort by default the column 4
oTable.fnSort([[3, 'asc']]);
});
Upvotes: 1