Created
July 24, 2014 09:30
-
-
Save lozcalver/8d0cacdff40a0c958996 to your computer and use it in GitHub Desktop.
Stacking data in X columns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ColumnedList extends SS_ListDecorator { | |
/** | |
* @param int $columns | |
* @return array | |
*/ | |
public function stack($columns) { | |
$total = $this->list->count(); | |
$perColumn = ceil($total / $columns); | |
$result = array_chunk($this->list->toArray(), $perColumn); | |
foreach($result as $i => $column) { | |
$result[$i] = new ArrayList($column); | |
} | |
return $result; | |
} | |
/** | |
* Stacks data in columns | |
* | |
* @param int $columns The target number of columns | |
* @param string $children Name of the control under which children can be iterated on | |
* @return ArrayList | |
*/ | |
public function Stacked($columns, $children = 'Children') { | |
$stacked = $this->stack($columns); | |
$result = new ArrayList(); | |
foreach($stacked as $list) { | |
$list = self::create($list); | |
$result->push(new ArrayData(array( | |
$children => $list | |
))); | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment