Skip to content
This repository has been archived by the owner on Oct 8, 2023. It is now read-only.

Commit

Permalink
Refactored code to normalize data
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrogehlen committed Nov 30, 2015
1 parent 1f9fc4d commit c27a3f6
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions TreeGrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,10 @@ public function renderTableFooter()
*/
public function renderItems()
{
$rows = [];
$models = array_values($this->dataProvider->getModels());
$keys = $this->dataProvider->getKeys();
$rows = [];
$models = $this->buildTree($models,$this->parentRoot);
$models = $this->normalizeData($models,$this->parentRoot);
foreach ($models as $index => $model) {
$key = $keys[$index];
if ($this->beforeRow !== null) {
Expand Down Expand Up @@ -383,25 +383,25 @@ protected function guessColumns()
}
}
}

/**
* build tree
* @param array $elements
* Normalize tree data
* @param array $data
* @param string $parentId
* @return array
*/
protected function buildTree(array $elements, $parentId = null) {
$branch = [];
foreach ($elements as $key => $element) {
protected function normalizeData(array $data, $parentId = null) {
$result = [];
foreach ($data as $element) {
if ($element[$this->parentColumnName] == $parentId) {
$children = $this->buildTree($elements, $element[$this->keyColumnName]);
$branch[$key] = $element;
$result[] = $element;
$children = $this->normalizeData($data, $element[$this->keyColumnName]);
if ($children) {
$branch = $branch + $children;
$result = array_merge($result, $children);
}
}
}
return $branch;
return $result;
}

}

0 comments on commit c27a3f6

Please sign in to comment.