Amount in R$ (Brazilian currency) orders wrong

Amount in R$ (Brazilian currency) orders wrong

roddrigooroddrigoo Posts: 6Questions: 1Answers: 0

Hello, I use the DataTable where I pull the values ​​from a database, making a sum, works perfectly.

Then I format the total value in PHP, it stays for example: 1,234.00 and so far it displays correctly using this code.

echo "<td>R$ ".number_format($linha_vendas["vendastotal"], 2, ',', '.')."</td>";

The problem is at the time of sorting because it does not sort correctly if the value is in that format. If I change the display to 1,234.00 it orders correctly.

How can I resolve this by making it display 1,234.00 and ordering correctly?

Thank you

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    That should work correctly. Example here.

    Can you link to a test case showing the issue please?

    Allan

  • roddrigooroddrigoo Posts: 6Questions: 1Answers: 0

    Hi, Allan. Thanks for the reply.

    I did not get your example link .. You can test the error in the link below when trying to sort by "TOTAL VENDIDO" which is the third column. You will see that the amount R $ 855.80 is considered greater than R $ 46,344.56.

    http://www.garcomrei.com.br/dashboard/teste.php

    In the second link, below, when I set the display to type X,XXX,XX it sorts correctly.

    http://www.garcomrei.com.br/dashboard/teste2.php

    I wait, thanks

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    You have spaces in those values. If you remove the space, or use a narrow space, then it should work as expected.

    Allan

  • roddrigooroddrigoo Posts: 6Questions: 1Answers: 0

    Hello Allan, I have no space in the field.

    Only if I use the X,XXX,XX format does it work, if I use the format I want from Brazilian currency as X.XXX,XX it does not sort right.

    It would not be the case for me to use numeric-comma, I tried to use it myself but I could not.

    I continue to count on your help, thanks.

  • kthorngrenkthorngren Posts: 21,300Questions: 26Answers: 4,945

    I took your data and built this example:
    http://live.datatables.net/bobicote/1/edit

    On your page you have:

    $(document).ready(function() {
        $('#data-table').dataTable( {
            "aoColumns": [
                null,
                null,
                null,
                null,
                { "sType": "numeric-comma" },
                null,
                { "sType": "numeric-comma" }
            ]
        } );
    } );
    
    $(document).ready(function() {
        $('#data-table').DataTable( {
            "language": {
                "decimal": ",",
                "thousands": "."
            }
        } );
    } );
    

    I'm surprised you don't get the Cannot reinitialise DataTable error. Try combining the two. The "Position" column in my example relies on this. Remove it from the code and sorting won't work correctly. Could be that is all you need to do.

    The Office column uses a modified version of your sort plugin. It removes all the . then replaces the , with .. Also I used columns.render to remove the R$ (note the space after the R$) from the data for sorting. If the first option doesn't work then maybe this will.

    Kevin

  • roddrigooroddrigoo Posts: 6Questions: 1Answers: 0

    Hi Kevin, we're almost there! :)

    Many thanks for the help, now I put the code below at the bottom of the page and ordered correctly.

    $('#data-table').dataTable( { "language": { "decimal": ",", "thousands": "." } } );

    The problem is that now gives the error you said, what can be?

    See the completed code in this link: http://www.garcomrei.com.br/dashboard/teste3.php

    Thanks

  • allanallan Posts: 63,455Questions: 1Answers: 10,465 Site admin

    The documentation that the error message links to explains what is causing it and how to resolve it.

    Allan

  • roddrigooroddrigoo Posts: 6Questions: 1Answers: 0

    Hello Allan, I have read the documentation but I only have 1 table initialization code, this one below:

    $('#data-table').dataTable( { "language": { "decimal": ",", "thousands": "." } } );

    Can you see the code and see where I'm wrong to help me please?

    http://www.garcomrei.com.br/dashboard/teste3.php

    appreciate

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395
    Answer ✓

    Your DataTable is initialised on the page and then again in "../assets/js/page-table-manage.demo.min.js".

  • roddrigooroddrigoo Posts: 6Questions: 1Answers: 0

    Thank you so much tangerine, its works!!!!

This discussion has been closed.