KoolReport's Forum

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

Row and Column Sort #22

Closed Hallie Gromek opened this topic on on Jun 16, 2017 - 5 comments

Hallie Gromek commented on Jun 16, 2017

I want the rows and the columns to sort ascending, however I can't get the COLUMN HEADERS to sort correctly.

Here's my code `

->pipe(new Sort(array(
"Types" => "asc"
)))	
->pipe(new ColumnsSort(array(
 "territory" => "asc"
 )))

KoolReport commented on Jun 16, 2017

Hi Hallie,

Please try this:

->pipe(new ColumnsSort(array(
    "{name}"=>"asc",
)))

Above code will sort columns by its name. More information you may view the Columns Sort documentation

Let us know if you need further assistance.

Hallie Gromek commented on Jun 20, 2017

Hi, This actually removes some of the rows within the grid? If I remove the column sort I have 6 rows of information and when I add it in I have 5 rows? `

->pipe(new Sort(array(
"Types" => "asc"
 )))

->pipe(new ColumnsSort(array(
"{name}" => "asc"
 )))

David Winterburn commented on Jun 21, 2017

Hi Hallie,

Thanks for your feedback! This is actually a bug of the ColumnsSort process. Please replace the content of ColumnsSort.php with this code and let us know if it fixes your problem

ColumnsSort.php:

<?php
/**
 * This file contains class to handle data sorting
 *
 * @author KoolPHP Inc (support@koolphp.net)
 * @link https://www.koolphp.net
 * @copyright 2008-2017 KoolPHP Inc
 * @license https://www.koolreport.com/license#mit-license
 */

/* Usage
 * ->pipe(new ColumnsSort(array(
 * 		"{name}"=>"desc",
 * 		"{label}"=>"asc",
 * 		//"{name}"=>function() {},
 * 		//"{label}"=>function() {},
 * )))
 *  */
namespace koolreport\processes;
use \koolreport\core\Process;
use \koolreport\core\Utility;

class ColumnsSort extends Process
{
  protected $nameSort;
  protected $labelSort;
  protected $fixedColumns;
  protected $sortedColumns;
  protected $meta;
  
  function onInit()
  {
    $this->nameSort = Utility::get($this->params, '{name}', null);
    $this->labelSort = Utility::get($this->params, '{label}', null);
    $this->fixedColumns = Utility::get($this->params, 'fixedColumns', null);
  }
  
  protected function onMetaReceived($metaData)
	{
    $this->meta = $metaData;
		return $metaData;		
	}
  
	protected function onInput($row)
	{
    if (! isset($this->sortedColumns)) {
      $columns = array_keys($row);
      $columnsToSort = array();
      $fixedColumns = isset($this->fixedColumns) ? $this->fixedColumns : array();
      foreach ($columns as $i => $colName)
        if (! array_key_exists($i, $fixedColumns) &&
            ! array_key_exists($colName, $fixedColumns))
          $columnsToSort[$i] = $colName;
      if (isset($this->nameSort)) {
        $sort = $this->nameSort;
        uasort($columnsToSort, function($a, $b) use ($sort) {
          $cmp = 0;
          if (is_string($sort)) {
            $cmp = is_numeric($a) && is_numeric($b) ? 
                $a - $b : strcmp($a, $b);
            $cmp = $sort === 'asc' ? $cmp : - $cmp;
          }
          else if (is_callable($sort)) 
            $cmp = $sort($a, $b);
          return $cmp;
        });
      }
      if (isset($this->labelSort)) {
        $sort = $this->nameSort;
        $meta = $this->meta;
        uasort($columnsToSort, function($a, $b) use ($sort, $meta) {
          $cmp = 0;
          $a = isset($meta[$a]['label']) ? $meta[$a]['label'] : '';
          $b = isset($meta[$b]['label']) ? $meta[$a]['label'] : '';
          if (is_string($sort)) {
            $cmp = is_numeric($a) && is_numeric($b) ? 
                $a - $b : strcmp($a, $b);
            $cmp = $sort === 'asc' ? $cmp : - $cmp;
          }
          else if (is_callable($sort)) 
            $cmp = $sort($a, $b);
          return $cmp;
        });
      }
      $sortedColumns = array();
      foreach ($fixedColumns as $i => $pos)
        if (is_numeric($i))
          $sortedColumns[$pos] = $columns[$i];
        else
          $sortedColumns[$pos] = $i;
      $i = 0;
      foreach ($columnsToSort as $column) {
        while (isset($sortedColumns[$i]))
          $i++;
        $sortedColumns[$i] = $column;
        $i++;
      }
      ksort($sortedColumns);
      $this->sortedColumns = $sortedColumns;
    }
    $newRow = array();
    foreach ($this->sortedColumns as $column)
      $newRow[$column] = $row[$column];
    $this->next($newRow);
	}
}

Thanks!

Hallie Gromek commented on Jun 21, 2017

Worked!!! thank you !

KoolReport commented on Jun 21, 2017

Great! Anything please let us know!

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