Sort with thousands separador doesn’t work

Sort with thousands separador doesn’t work

DurovniDurovni Posts: 3Questions: 0Answers: 0

Hi folks,

I haver to work with decimals (comma) and thousands ( dot) separators. I set the datatable to work with it And it works fine. But it does not work when the Number is smaller the 1000 and of course does not have thousand separators.

Numbers are
1.234,13
876,40
32,79
1.004,23

When I click column head to sort it Goes like this
1.004,23
1.234,13
32,79
876,40

Can anyone help?

Thanks in avance

Moises

Replies

  • allanallan Posts: 62,994Questions: 1Answers: 10,368 Site admin

    Hi Moises,

    Your example data appears to work okay here. Perhaps you can link to a test case showing the issue (per the forum rules)?

    Thanks,
    Allan

  • DurovniDurovni Posts: 3Questions: 0Answers: 0

    Hi Allan,

    Thank you very much for your answer, by your exemple I could find what I was doing wrong.

    There was a mistake in my js file, decimal and thousands were surrounded by " ".. When I fix it per you code it worked beautifuly.

    The wrong code
    $('#example').DataTable( {
    language: {
    "url": "//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/Portuguese-Brasil.json",
    "decimal": ",",
    "thousands": "."

    }
    

    } );

    Many thanks

  • DurovniDurovni Posts: 3Questions: 0Answers: 0

    PS. How do I mark this as answered?

  • allanallan Posts: 62,994Questions: 1Answers: 10,368 Site admin

    You don't - this thread was started with the "New Discussion" button rather than "Ask a Question". Just seeing your reply above let's me know its answered now.

    Good to hear you've got it working now!

    Allan

  • a_ja_j Posts: 10Questions: 1Answers: 0

    New here, @allan Could you help check this
    Column 3 isn't consistent when sorting. It just doesn't go from high to low.
    Column 8 is also a tricky data and just don't know if there is any way to that too.

    Will appreciate any help.

  • allanallan Posts: 62,994Questions: 1Answers: 10,368 Site admin

    isn't a currency symbol that DataTables picks up by default I'm afraid - it would need either a tweak in DataTables to add it, or use the formatting numbers plug-in.

    Alternatively use orthogonal data which is what I would recommend, assuming you can control the HTML output?

    Orthogonal data would also work for the final column in the table since it could have a numerical value, while the display is still the human readable output.

    Allan

  • a_ja_j Posts: 10Questions: 1Answers: 0

    I'm using v1.10+, shouldn't format numbers be built in?
    Wouldn't mind if you could help edit the code to solve column 3 issue.
    Column 8 seems like a complicated one.

  • colincolin Posts: 15,236Questions: 1Answers: 2,598

    Hi @a_j ,

    here's column 3 working. Yep, column 8 is bespoke, you would need to do something like this - take the "billions" for example, and multiply up the number that you return.

    Cheers,

    Colin

  • a_ja_j Posts: 10Questions: 1Answers: 0

    @colin Thanks alot, really appreciate.
    Guess i would just disable sort for the 8th column for now.

  • allanallan Posts: 62,994Questions: 1Answers: 10,368 Site admin

    You could use this plug-in as inspiration for sorting human language numbers if you can't do orthogonal ordering.

    I'm using v1.10+, shouldn't format numbers be built in?

    No. That plug-in is very aggressive. It will strip out everything that isn't a number, so it has to be targeted at specific columns. For that reason it remains a plug-in.

    Allan

  • a_ja_j Posts: 10Questions: 1Answers: 0

    @allan is there a way to target span attribute instead of the span content?
    I could make the JS generate a non-formatted number string into an attribute.
    E.G
    <span rel="tooltip" class="marketcapXRP" data-number="1200000.00">₦1.2 Million</span>

    For the above code, how will i tell datatable to sort span attribute (data-number) instead of span content?

  • a_ja_j Posts: 10Questions: 1Answers: 0
    edited September 2018

    Sorry, never knew there was data-order attribute option that can be added to the <td> for sorting.
    The issue is now solved.
    Thanks

This discussion has been closed.