I am using DataTables v1.10. I want to make a column sortable by a numeric value, when the value shown in the column is not numeric.
I understand that I need to use orthogonal data to do this, as described here.
However, my sort function is not working. This is my code:
var myData = [{
'country': 'France',
'all_games': 7,
'won_games': 4
}, {
'country': 'England',
'all_games': 13,
'won_games': 13
}, {
'country': 'Germany',
'all_games': 2,
'won_games': 0
}];
var columns = [{
"data": "country",
"title": "Country"
}, {
"data": "outcomes_str",
"title": 'Games won',
"render": {
"_": "display",
"sort": "sort"
}
}];
$.each(myData, function (i, d) {
d.outcomes_str = {};
d.outcomes_str.sort = (d.all_games > 0) ? (d.won_games / d.all_games) * 100 : 0;
d.outcomes_str.display = d.won_games + '/' + d.all_games + ' (' + Math.round(d.outcomes_str.sort * 10) / 10 + '%)';
console.log(d.outcomes_str);
});
drawTable(myData, 'localTable');
function drawTable(data, tableId) {
var html = '<table class="table table-bordered table-hover ';
html += '" cellspacing="0" width="100%" id="myTable"></table>';
$('#table').append(html);
$("#myTable").DataTable({
data: data,
columns: columns,
paging: false
});
}
});
JSFiddle here: http://jsfiddle.net/07nk5wob/33/
What am I doing wrong?
Orthogonal data for a table can be provided through the data source array / object (note, objects are much easier to work with, since you don't need to remember array indexes!) as predefined values (typically this is done with ajax loaded data or a Javascript provided data source), or they can be computed on-the-fly as ...
FixedColumns provides the option to freeze one or more columns to the left or right of a horizontally scrolling DataTable. This allows information in the fixed columns to remain visible even when scrolling through the data set. This can be particularly useful if you wish to show a large number of columns.
What is Orthogonality in Statistics? Simply put, orthogonality means “uncorrelated.” An orthogonal model means that all independent variables in that model are uncorrelated. If one or more independent variables are correlated, then that model is non-orthogonal.
The maximum number of rows that a DataTable can store is 16,777,216.
See my corrected answer.
Basically, you need to explicitly state column data type with type: "num"
for numeric data, otherwise it may default to alphabetical sort.
{
"data": "outcomes_str",
"title": 'Games won',
"type": "num",
"render": {
"_": "display",
"sort": "sort"
}
}
See updated jsFiddle for code and demonstration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With