hi formatted nunber desc ro asc

hi formatted nunber desc ro asc

Kaman43500Kaman43500 Posts: 5Questions: 2Answers: 0

Hi I'm creating a table using DataTables. but in one column I have counts as
2016 / 1
2016 / 2
2017 / 1
2017 / 2
2017 / 3

Can not I sort it out properly?

jQuery.extend( jQuery.fn.dataTableExt.oSort, {

"formatted-num": function ( a ) {
    a.replace(/[^\d\-\.\/]/g, '');
    return parseFloat(a);
},

"formatted-num-asc": function ( a, b ) {
    return a - b;
},

"formatted-num-desc": function ( a, b ) {
    return b - a;
}

} );

$(document).ready(function() {
$('#ornek').DataTable({
'columdefs' : [
{ type: 'formatted-num', targets: 0 },
{ type: 'formatted-num', targets: 0 },
null,
null,
null,
null
]
});
} );

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin
    edited November 2017

    That sorting plug-in would format the data in that column as 20161, 20162. Its a bit hacky, but that would work up to 9 after the /. So if they are quarters, it should be fine. If it is months, you'd need to pad it with a 0.

    If you can link to the page showing the issue, that would be useful.

    Allan

  • Kaman43500Kaman43500 Posts: 5Questions: 2Answers: 0

    hi thanks Allan

    my work in here : http://jsfiddle.net/fm8so8na/9/

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin

    https://jsfiddle.net/fm8so8na/10/

    A few changes I've made:

    1. I used the pad approach I mentioned above. Note that I used String.prototype.padStart, so it will only work in browsers which support that function (Chrome, Edge, Firefox, Safari apparently all do at their latest versions). Replace that if you need it to work with older ones.
    2. I've used DataTables 1.10.16 rather than 1.9.4 as you had
    3. It should be columnDefs not columndefs (it is case sensitive).

    Allan

  • Kaman43500Kaman43500 Posts: 5Questions: 2Answers: 0

    Hello, thank you very much, allan. you are very good :) but at the same time I am trying to use it with an asp.net repeater. I have a labels in the repeater. Can you help me? Thank you so much

  • allanallan Posts: 63,464Questions: 1Answers: 10,466 Site admin
    Answer ✓

    Not there I'm afraid. The support I can offer doesn't extend to ASP.NET server side. You'd be best asking on StackOverflow or similar for a generic ASP.NET question.

    Allan

  • Kaman43500Kaman43500 Posts: 5Questions: 2Answers: 0

    hello allan. thank you so much. the error was caused by me. In ASP.NET, instead of passing the label to <% # Eval ("Number")%> write. thank you so much

This discussion has been closed.