Sort by value in Datatables

Asked

Viewed 1,765 times

4

I am using the Bootstrap SB Admin2, which makes use of the component Datatables.

On my jsp page I am loading the following table:

<table class="table table-striped table-bordered table-hover" id="dataTables-example">
  <thead>
    <tr>
      <th>Código</th>
      <th>Nome RCA</th>
      <th>Meta</th>
    </tr>
  </thead>

  <tbody>
    <c:forEach var="rca" items="${listaTeste }">
      <tr class="odd gradeX">
        <td>${rca.codusur }</td>
        <td>${rca.nome }</td>
        <td style="text-align: right">${rca.meta }</td>
      </tr>
    </c:forEach>
  </tbody>
</table>

and the following code for javascript:

<script>
$(document).ready(function() {
    $('#dataTables-example').DataTable({
            responsive: true,
            "order": [2, 'asc']
    });
});
</script>

That way the code works, example result:

2512    Aa                         227,27
5769    Ma                       2.318,18
2945    Mb                       4.772,72

However, if I ask for jstl formatting in this way:

<td style="text-align: right"><fmt:formatNumber value="${rca.meta }" type="currency" /></td>

The table is ordered like this:

5769    Ma                      R$ 2.318,18
2512    Aa                      R$   227,27
2945    Mb                      R$ 4.772,72

From what I understand it seems that he is reading as string, some solution to this case? I have looked at the documentation and found nothing.

PS: I apologize for the length of the question, I’m new here, but I wanted to explain all the details.

I tried to change the Pattern in fmtNumber using pattern="#,##0.00", and the classification remains wrong, but using pattern="0.00" only then can he classify.

  • 1

    You tried using this plugin: http://datatables.net/plug-ins/sorting/currency?

  • I tried, this plugin already came along with the datatable. Just to see if I understood: is he the one who would do the formatting? Or would I have to take fmt out of the code? Because in both cases I couldn’t make it work.

2 answers

2


Correct will not work the code precisely due to formatting the number, for you to correctly sort the format of the number must be in 0.00.

What I would suggest to you would be the following, insert us td's the value already converted using the 0.00 mask and reconfigure the display formatting on startup of the datatables.

Thus:

<td style="text-align: right"><fmt:formatNumber value="${rca.meta }" pattern="0.00" /></td>

And the initialization of datatables would stay, remembering that you will need to add the ordering plugin datatables.

$('#dataTables-example').DataTable({
  //demais configurações
  "aoColumns": [
    //demais colunas
    { "sType": "numeric-comma" }, //aplica a máscara monetária
  ],
});

follows the reference to the list of plugins: https://datatables.net/plug-ins/sorting/

  • Using Numeric comma works, I tried to reconfigure with formated number and currency, but it doesn’t work, so I’ll leave anyway. I appreciate the help.

1

At that time the plugin is able to handle this problem using the language attribute to translate parts of the system to the native language of the system and to enter regional settings, to circumvent this problem use as follows.

$('#dataTables-example').DataTable( {
    "language": {
        "decimal": ",",
        "thousands": "."
    }
} );

For more information visit link

  • 1

    your solution worked for me!! valeuu

Browser other questions tagged

You are not signed in. Login or sign up in order to post.