KoolReport's Forum

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

Sort by date #2054

Open noa opened this topic on on May 4, 2021 - 7 comments

noa commented on May 4, 2021

Hey, I'm trying to use a Datagrid template, client_rowgroup I would love to know how to sort the date column sorted by date and not alphabetically

Sebastian Morales commented on May 5, 2021

What're values of your "month" column that you intended to sort by?

noa commented on May 5, 2021

Hi thanks for the quick reply. The values ​​I wanted to sort are strings of month and year as in the example above of February 2021 and April 2021 and I would like to know if there is a way to sort by date (if necessary I will change the format in the database to date and not string)

Sebastian Morales commented on May 5, 2021

Since the "month" column can't be sorted by its value, we need another column based on its value, which can be sorted. Pls try this code:

    DataTables::create(array(
        ...
        "columns" => array(
        	...
            "standardMonth" => array(
            	"visible" => false, //no need to show this column
            	"formatValue" => function($row, $row, $colKey) {
            		$month = $row["month"];
            		$standardMonth = convertMonthYear($month); // use DateTime class to convert $month to standard date value, such as "2021-01-31", or just a simple year month "2021-01"
            		return $standardMonth;
            	}
            ),
        	"month" => array(
        		"data-order" => "standardMonth", //order this column by "standardMonth" column
        	)
        )
    ));

Let us know if you have any further question. Rgds,

Gal Engineers commented on May 5, 2021

Thanks again, I'm getting an error message: Fatal error: Redefinition of parameter $row from where he supposed to know all the params that the function gets?

Sebastian Morales commented on May 6, 2021

Oh, I made a mistake. Pls change this line:

    "formatValue" => function($row, $row, $colKey) {

to:

    "formatValue" => function($value, $row, $colKey) {

Tks,

noa commented on May 6, 2021

I apologize but now I'm getting a new error message: Thanks in advance for all the help !!

Sebastian Morales commented on May 6, 2021

I forgot to mention, this line is a mocked up code:

    $month = $row["month"];
    $standardMonth = convertMonthYear($month);

It meant you'd have to write your own code to convert from your month and/or year to a standard date format string like "2021-01-31", which we use to sort by. I don't know exactly your original month date value so I can't give specific guidance.

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

None