From c27a3f616c8337e7f49b2df4d490f62a3d0fc9f8 Mon Sep 17 00:00:00 2001 From: leandrogehlen Date: Mon, 30 Nov 2015 11:40:28 -0200 Subject: [PATCH] Refactored code to normalize data --- TreeGrid.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/TreeGrid.php b/TreeGrid.php index f18ee44..f3ec272 100644 --- a/TreeGrid.php +++ b/TreeGrid.php @@ -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) { @@ -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; } }