Skip to content

Instantly share code, notes, and snippets.

@kinglozzer
Created July 24, 2014 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinglozzer/8d0cacdff40a0c958996 to your computer and use it in GitHub Desktop.
Save kinglozzer/8d0cacdff40a0c958996 to your computer and use it in GitHub Desktop.
Stacking data in X columns
<?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