KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Problem with sorting datatables #1058

Open Elcid opened this topic on on Aug 26, 2019 - 14 comments

Elcid commented on Aug 26, 2019

Hi,

I have problems to order my datatables if I format the numbers to show default value of my currency.

Example 1 - this is a default ordering

If I sort in "Receita (R$)", it sorts just the first number and ignore all further.

Example:

My column code:

$columns['valorreceita'] = [
    'label' => (($this->params['categoria'] !== 0 AND $this->params['categoria'] === 8000000000) ? 'Receita bruta (R$)' : 'Receita (R$)'),
    'type' => 'number',
    'cssStyle' => 'text-align: right; width: 180px;',
    'formatValue' => function ($value, $row) {
        return number_format($value, 2, ',', '.');
    }
];

I realize if I remove the format and put a "type" string, we can order normally.

Default:

Sorting on "Receita (R$)":

What can I do? Thanks in advance.

David Winterburn commented on Aug 27, 2019

Hi Elcid,

Would you please set the column meta for Receita in report setup like this:

//MyReport.php
...
->pipe(new \koolreport\processes\ColumnMeta([
    "valorreceita" => [
        "type" => "number",
        "decimalPoint" => ",",
        "thousandSeparator" => ".",
    ]
]))
...

Then remove "formatValue" in DataTables' columns option in report view. Let us know if it works for you. Thanks!

Elcid commented on Aug 27, 2019

Hi,

Doesn't work. Follow the images:

My code in setup:

My code on datatables:

This is my result, sort by "Receita":

It orders correctly only if I put type "number" on my datatables and remove ColumnMeta. However, I need to show my currency format and my data ordered.

David Winterburn commented on Aug 28, 2019

Hi Elcid,

It seems strange as normally DataTables handles number column ordering quite well independently of number format. I'm not sure what causes the problem in your case yet but let's try this solution. Add the following property to your Datatables' columns:

$columns['valorreceita'] = [
    ...
    'data-order' => 'valorreceita',
];

This will add a data-order attribute which value is a number (instead of a formatted one) to the column. Let us know if it solves your sorting issue or not. Thanks!

Elcid commented on Aug 28, 2019

Doesn't work. I don't know what to do.

David Winterburn commented on Aug 28, 2019

Is there a link to your page where we could check? If there is but it's private, please send a link to our email support@koolphp.net. Thanks!

Elcid commented on Aug 28, 2019

No problem. You can see here: http://transparencia.rn.gov.br/receita-prevista-detalhada?consulta=1&mes=12&ano=2019&tipo=1

Thanks in advance.

David Winterburn commented on Aug 28, 2019

By the way, would you please let us know which version of Datagrid package you are using? Thanks!

Elcid commented on Aug 28, 2019

Version 2.78.0

I think I'm using an outdated version.

David Winterburn commented on Aug 28, 2019

I couldn't see the data-order attribute in your column. Would you mind posting your report view file php code here? Thanks!

David Winterburn commented on Aug 28, 2019

Oh, I think that version hasn't support data-order attribute yet. And its client datatables js could have some problem handling formatted column sorting. Are you able to update to a newer version of Datagrid?

Elcid commented on Aug 28, 2019

I have a 3 documents: one have a setup, another to table and another to graph. I put this code on table:

$columns['valorreceita'] = [
    'label' => (($this->params['categoria'] !== 0 AND $this->params['categoria'] === 8000000000) ? 'Receita bruta (R$)' : 'Receita (R$)'),
    'cssStyle' => 'text-align: right; width: 180px;',
    'data-order' => 'valorreceita',
    /*'formatValue' => function ($value, $row) {
        return number_format($value, 2, ',', '.');
    }*/
];

I test this too, but, nothing work.

$columns['valorreceita'] = [
    'label' => (($this->params['categoria'] !== 0 AND $this->params['categoria'] === 8000000000) ? 'Receita bruta (R$)' : 'Receita (R$)'),
    'cssStyle' => 'text-align: right; width: 180px;',
    'data-order' => 'valorreceita',
    'type' => 'number',
    'decimals' => 2,
    'decimalPoint' => ',',
    'thousandSeparator' => '.',
];

Just if i does not nothing format to my currency, this worked.

Elcid commented on Aug 28, 2019

I will go try update this, one moment.

Elcid commented on Aug 28, 2019

Dude, I need update datatables or koolreports?

I tryed update datatables, but don't worked.

David Winterburn commented on Aug 29, 2019

Hi Elcid,

Please update the Datagrid package. I couldn't see the data-order attribute in your number column so I think Datagrid hasn't been updated yet.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
bug
help needed

None