KoolReport's Forum

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

Table grouping sorts dates alphabetically #1736

Closed Nicholas Savill opened this topic on on Nov 27, 2020 - 3 comments

Nicholas Savill commented on Nov 27, 2020

Hi

I'm trying to use table grouping to group a database of room bookings by date. The data source has date in format Y-m-d and sorts the records by date. I am formatting the date to D d M Y (e.g. Fri 27 Nov 2020). The table grouping is resorting the data alphabetically (i.e. by day of week) not chronologically.

Any help to resolve is appreciated.

The data selection in the setup() method in BookingsReport.php is `

$this->src("mysql")
  ->query(
    "SELECT   booking_dt,
              start_tm,
              end_tm,
              BookingName,
              RoomName
     FROM     bookings
     ORDER BY booking_dt,
              start_tm"
  )
  ->pipe(new DateTimeFormat([
    "booking_dt" => "D d M Y",
    "start_tm" => "H:i",
    "end_tm" => "H:i",
  ]))
  ->pipe($this->dataStore("bookings"));

The report view is defined as

Table::create([
  "dataSource"=>$this->dataStore("bookings"),
  "columns"=>[
    "start_tm"=>[
      "label"=>"Start",
    ],
    "end_tm"=>[
      "label"=>"End",
    ],
    "BookingName"=>[
      "label"=>"Booking"
    ],
    "RoomName"=>[
      "label"=>"Room"
    ],
  ],
  "grouping"=>[
    "booking_dt",
  ],
]);

Many thanks

Nick

KoolReport commented on Nov 27, 2020

I suggest you put "booking_dt" => "Y-m-d" or simply don't format it if it is in Y-m-d already. And then in the grouping you do:

    "grouping"=>array(
        "booking_dt"=>array(
            "top"=>function($calculated_results){
                $booking_dt = $calculated_results["booking_dt"];
                return (new \DateTime($booking_dt))->format("D d M Y");
            },
        ),
Nicholas Savill commented on Nov 27, 2020

Thank you for your quick response. That worked well, though line 4 of the solution needed to be

$booking_dt = $calculated_results["{booking_dt}"];
KoolReport commented on Nov 27, 2020

Awesome and thank you for correcting me :)

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
solved

None