KoolReport's Forum

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

Ordering column inside DataTables with 'data-order' is not working #2117

Closed Aljaz Ketis opened this topic on on Jun 1, 2021 - 8 comments

Aljaz Ketis commented on Jun 1, 2021

I have an Datatable with some data and I want to display a string, which works, but I want to order it by another column which is a number and it is not working. I have done that by assigning the column:

'data-order' => 'column_name_to_be_sorted'

It does not throw any error in console, and it only changes the arrow icon and not changing column order in anyway.

Sebastian Morales commented on Jun 1, 2021

Would you mind showing your DataTables' create code? Here's a similar topic for you to consult:

https://www.koolreport.com/forum/topics/1307#p7259

Aljaz Ketis commented on Jun 1, 2021

I cannot show you the querry but you can see here i add the columnMeta to all numbers type:"num-fmt", becouse it cannot be ordered correct if I change to "number":

->pipe(
                 new ColumnMeta(
                    array(
                        "nmb_rooms"            => array(
                            "label" => "Nr. of Rooms",
                            "type"  => "num-fmt",
                        ),
                        "cc"                   => array(
                            "type" => "num-fmt",
                        ),
                        "time_spent"           => array(
                            "type" => "num-fmt",
                        ),
                        "diff"                 => array(
                            "type" => "num-fmt",
                        ),
                        "cc_formatted"         => array(
                            "label" => "Expected time spent",
                            "type"  => "string",
                        ),
                        "time_spent_formatted" => array(
                            "label" => "Actual time spent",
                            "type"  => "string",
                        ),
                        "diff_formatted"       => array(
                            "label" => "Time difference",
                            "type"  => "string",
                        ),
                    )
                )
            )

and here I create the Datatable inside a view

<?php
                DataTables::create(
                    array(
                        "dataSource" => $this->dataStore("demo"),
                        "options"    => array(
                            "searching" => true,
                        ),
                        "columns"    => array(
                            "nmb_rooms",
                            "cc_formatted"         => array(
                                'data-order' => 'cc',
                            ),
                            "time_spent_formatted" => array(
                                'data-order' => 'time_spent',
                            ),
                            "diff_formatted"       => array(
                                'data-order' => 'diff',
                            ),
                        ),
                    ),
) ?>

"cc, time_spent, diff" is a number of total seconds between 2 dates and "diff_formatted, ..." are calculated columns which display the duration in hours and minutes as a string

Sebastian Morales commented on Jun 2, 2021

I think that you need to include the columns specified by "data-order" as well for it to work. If you don't want to show those columns, just set its "visible" => false like this:

 DataTables::create(
                    array(
                        "dataSource" => $this->dataStore("demo"),
                        "options"    => array(
                            "searching" => true,
                        ),
                        "columns"    => array(
                            "nmb_rooms",
                            "cc_formatted"         => array(
                                'data-order' => 'cc',
                            ),
                            "time_spent_formatted" => array(
                                'data-order' => 'time_spent',
                            ),
                            "diff_formatted"       => array(
                                'data-order' => 'diff',
                            ),
                            "cc" => array(
                                'visible' => false,
                            ),
                            "time_spent" => array(
                                'visible' => false,
                            ),
                            "diff" => array(
                                'visible' => false,
                            ),
                        ),
                    ), 

Let us know how this works. Rgds,

Aljaz Ketis commented on Jun 2, 2021

Added the columns, now on arrow click data changes, but not in asc or desc manner..I did not hide the columns by which it should be ordered and I can see that now it is ordering randomly.

Sebastian Morales commented on Jun 2, 2021

Pls share some screenshots of those not correctly ordered. Tks,

Aljaz Ketis commented on Jun 2, 2021

here are the pics of when i format the colun directly, and if I format the column with the property data-order

formatted_desc example

original_desc example

 "time_spent" => array(
                                'visible' => true,
                            ),
                            "time_spent_formatted" => array(
                                'data-order' => 'time_spent',
                                'label' => "time_spent_formatted"
                            ),
Sebastian Morales commented on Jun 2, 2021

Pls try this to see if it helps:

                            "time_spent_formatted" => array(
                                'data-order' => 'time_spent',
                                "type" => "num-fmt",
                            ),
Aljaz Ketis commented on Jun 2, 2021

It works thank you, but why did we need to put the type inside this one, if it is used as string and we have "time_spent" as "num-fmt"...anyhow please update the docs...there is no indication that we need to display columns and hide them, if we want to use this feature and no indication that we need to put the type to "num-fmt", it is really frustrating that it is not documented properly.

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
help needed

DataGrid