Sorting with jQuery datables, pretty easy but with images having images and "-" is bit difficult. I manage to do it for string, date and int but for images I can't think of anyway,
What I really want is to sort images and leave row which has value "-" at bottom,
JS
$.extend($.fn.dataTableExt.oSort, {
"date-eu-asc": function (a, b) {
if (a == '-') return 1;
else if (b == '-') return -1;
else {
//return ((a < b) ? -1 : ((a > b) ? 1 : 0));
// what going to go here ?
}
},
"date-eu-desc": function (a, b) {
if (a == '-') return 1;
else if (b == '-') return -1;
else {
//return ((a < b) ? 1 : ((a > b) ? -1 : 0));
// what going to go here ?
}
}
});
$('#table').dataTable({
"aoColumns": [null, {
type: 'date-eu',
targets: 0
}]
});
HTML
<table id="table">
<thead>
<tr>
<th>Normal</th>
<th>Image</th>
</tr>
</thead>
<tbody>
<tr>
<td>5</td>
<td data-order=2>
<img src='http://t1.gstatic.com/images?q=tbn:ANd9GcTU-gTaS7b67Ii3MMFaIRNQBdCinr9tNrU31GakpQGq9zok7cjKFg' />
</td>
</tr>
<tr>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td>10</td>
<td data-order=1>
<img src='http://www.fancyicons.com/free-icons/112/must-have/png/16/stock_index_down_16.png' />
</td>
</tr>
<tr>
<td>20</td>
<td>-</td>
</tr>
</tbody>
</table>
I made it work myself now,
HTML
<table id="table">
<thead>
<tr>
<th>Normal</th>
<th>Image</th>
</tr>
</thead>
<tbody>
<tr>
<td>5</td>
<td data-order=2>
<img src='http://t1.gstatic.com/images?q=tbn:ANd9GcTU-gTaS7b67Ii3MMFaIRNQBdCinr9tNrU31GakpQGq9zok7cjKFg' />
</td>
</tr>
<tr>
<td>-</td>
<td data-order='-'>-</td>
</tr>
<tr>
<td>10</td>
<td data-order=1>
<img src='http://www.fancyicons.com/free-icons/112/must-have/png/16/stock_index_down_16.png' />
</td>
</tr>
<tr>
<td>20</td>
<td data-order='-'>-</td>
</tr>
</tbody>
</table>
JS
$.extend($.fn.dataTableExt.oSort, {
"date-eu-asc": function (a, b) {
if (a == '-') return 1;
else if (b == '-') return -1;
else {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}
},
"date-eu-desc": function (a, b) {
if (a == '-') return 1;
else if (b == '-') return -1;
else {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
}
});
$('#table').dataTable({
"aoColumns": [null, {
type: 'date-eu',
targets: 0
}]
});
Added data-order = "-" for rows i want to keep at the bottom.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments