Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use orthogonal data to sort in DataTables?

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?

like image 571
Richard Avatar asked Nov 17 '15 11:11

Richard


People also ask

How do you create orthogonal data?

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 ...

What is FixedColumns in DataTables?

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 orthogonal data set?

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.

How much data can DataTables handle?

The maximum number of rows that a DataTable can store is 16,777,216.


1 Answers

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.

like image 137
Gyrocode.com Avatar answered Oct 09 '22 18:10

Gyrocode.com