Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added setSecondOrder() function #26

Open
wants to merge 7 commits into from

1 participant

@PingusPepan

Added a setSecondOrder() function which allows to order the table by a second (third, fourth) column. I find it usable and use it very extensively. However support for other datasources different than NetteDatabase have to be created. It implements a new method multipleOrderData(). Please, check the source, test it and let me know.

Use as follows:
For Grid:
$this->setSecondOrder(array('url', 'ASC')); // Order by a second column url ascending

$this->setSecondOrder(array(array('url', 'ASC'), array('name', 'DESC'))); // Order by a second column url ascending, name descending

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 28, 2012
  1. @PingusPepan

    Second order function

    PingusPepan authored
  2. @PingusPepan

    Remove backup file

    PingusPepan authored
  3. @PingusPepan
  4. @PingusPepan

    Fixed a typo

    PingusPepan authored
  5. @PingusPepan

    Fixed a typo

    PingusPepan authored
Commits on Sep 29, 2012
  1. @PingusPepan
  2. @PingusPepan

    Some bugfixes

    PingusPepan authored
This page is out of date. Refresh to see the latest.
View
6 DataSource/IDataSource.php
@@ -35,6 +35,12 @@ public function getCount($column = "*");
public function orderData($by, $way);
/**
+ * Sort data by given columns
+ * @param array $orders
+ */
+ public function multipleOrderData($orders);
+
+ /**
* Limit data to select
* @param int $limit
* @param int $offset
View
7 DataSource/NDataSource.php
@@ -40,6 +40,13 @@ public function orderData($by, $way)
$this->data->order($by." ".$way);
}
+ public function multipleOrderData($orders)
+ {
+ foreach($orders as $order){
+ $this->data->order($order[0]." ".$order[1]);
+ }
+ }
+
public function limitData($limit, $offset)
{
$this->data->limit($limit, $offset);
View
102 Grid.php
@@ -39,6 +39,9 @@ class Grid extends \Nette\Application\UI\Control
/** @var string */
protected $defaultOrder;
+ /** @var string */
+ protected $secondOrder;
+
/** @var IDataSource */
protected $dataSource;
@@ -135,8 +138,7 @@ protected function attached($presenter)
$this->orderData($this->order);
}
if(!$this->hasActiveOrder() && $this->hasDefaultOrder() && $this->hasEnabledSorting()){
- $order = explode(" ", $this->defaultOrder);
- $this->dataSource->orderData($order[0], $order[1]);
+ $this->orderData($this->defaultOrder);
}
$this->count = $this->getCount();
}
@@ -380,10 +382,56 @@ public function setMessageNoRecords($messageNoRecords)
*/
public function setDefaultOrder($order)
{
+ if(!is_array($order)){
+ $order = explode(" ", trim($order));
+ }
+ if($order[1] == 1 || strtoupper($order[1]) == "ASC"){
+ $order[1] = "ASC";
+ }else{
+ $order[1] = "DESC";
+ }
+ $order[2] = "default";
$this->defaultOrder = $order;
}
/**
+ * @param mixed $order
+ * @param mixed $way
+ */
+ public function setSecondOrder($order, $way = 0)
+ {
+ if(!is_array($order)){
+ $orderExp = explode(" ", $order);
+ if(count($orderExp) > 1){
+ $order = $orderExp[0];
+ $way = $orderExp[1];
+ }
+ if($way == 1 || strtoupper($way) == "ASC"){
+ $way = "ASC";
+ }else{
+ $way = "DESC";
+ }
+ $this->secondOrder = array($order, $way);
+ }else{
+ foreach($order as $key => $value){
+ if(is_array($value)){
+ if($value[1] == 1 || strtoupper($value[1]) == "ASC"){
+ $order[$key][1] = "ASC";
+ }else{
+ $order[$key][1] = "DESC";
+ }
+ }else{
+ $orderExp = explode(" ", $value);
+ if(count($orderExp) > 1){
+ $order[$key] = array($orderExp[0], $orderExp[1]);
+ }
+ }
+ }
+ $this->secondOrder = $order;
+ }
+ }
+
+ /**
* @param array $values
* @return array
*/
@@ -471,6 +519,14 @@ public function hasDefaultOrder()
/**
* @return bool
*/
+ public function hasSecondOrder()
+ {
+ return !empty($this->secondOrder) ? TRUE : FALSE;
+ }
+
+ /**
+ * @return bool
+ */
public function hasEnabledSorting()
{
return $this->enableSorting;
@@ -578,17 +634,51 @@ protected function filterData()
/**
* @param string $order
+ * @param bool $second
* @throws InvalidOrderException
*/
- protected function orderData($order)
+ protected function orderData($order, $second = false)
{
+ $orders = array();
try{
- $order = explode(" ", $order);
- if(in_array($order[0], $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$order[0]]->isSortable()){
- $this->dataSource->orderData($order[0], $order[1]);
+ if(!is_array($order)) {
+ $order = explode(" ", $order);
+ }
+ $component = $order[0];
+ if(isset($this['columns']->components[$order[0]]) && isset($this['columns']->components[$order[0]]->tableName)){
+ $order[0] = $this['columns']->components[$order[0]]->tableName;
+ }
+ if(is_array($order) && is_array($order[0])){
+ foreach($order as $value){
+ $orders[] = $this->orderData($value, true);
+ }
+ }elseif(is_array($order) && in_array($component, $this->getColumnNames()) && in_array($order[1], array("ASC", "DESC")) && $this['columns']->components[$component]->isSortable()){
+ $orders[] = $order;
+ }elseif(is_array($order) && isset($order[2]) && $order[2] == "default" && in_array($order[1], array("ASC", "DESC"))){
+ $orders[] = $order;
+ }elseif(is_array($order) && in_array($order[1], array("ASC", "DESC"))){
+ $orders[] = $order;
}else{
throw new InvalidOrderException("Neplatné seřazení.");
}
+ if($this->hasSecondOrder() && $second === false){
+ if(is_array($this->secondOrder[0])){
+ foreach($this->secondOrder as $secondOrder){
+ if(!in_array($secondOrder, $orders) || empty($orders)){
+ $orders[] = $this->orderData($secondOrder, true);
+ }
+ }
+ }else{
+ if(!in_array($this->secondOrder, $orders) || empty($orders)){
+ $orders[] = $this->orderData($this->secondOrder, true);
+ }
+ }
+ $this->dataSource->multipleOrderData($orders);
+ }elseif($second === true){
+ return $order;
+ }else{
+ $this->dataSource->orderData($order[0], $order[1]);
+ }
}
catch(InvalidOrderException $e){
$this->flashMessage($e->getMessage(), "grid-error");
Something went wrong with that request. Please try again.