浏览代码

优化表格设置查询数据逻辑,取消toArray转化数组操作

jqh 4 年之前
父节点
当前提交
39b879e5e7
共有 4 个文件被更改,包括 57 次插入40 次删除
  1. 7 9
      src/Grid.php
  2. 7 26
      src/Grid/Column.php
  3. 5 5
      src/Grid/Row.php
  4. 38 0
      src/Support/Helper.php

+ 7 - 9
src/Grid.php

@@ -416,20 +416,18 @@ class Grid
 
         $collection = $this->processFilter();
 
-        $data = $collection->toArray();
-
         $this->prependRowSelectorColumn();
         $this->appendActionsColumn();
 
         Column::setOriginalGridModels($collection);
 
-        $this->columns->map(function (Column $column) use (&$data) {
-            $column->fill($data);
+        $this->columns->map(function (Column $column) use (&$collection) {
+            $column->fill($collection);
 
             $this->columnNames[] = $column->getName();
         });
 
-        $this->buildRows($data);
+        $this->buildRows($collection);
 
         $this->sortHeaders();
     }
@@ -449,14 +447,14 @@ class Grid
     /**
      * Build the grid rows.
      *
-     * @param array $data
+     * @param Collection $data
      *
      * @return void
      */
-    protected function buildRows(array $data)
+    protected function buildRows($data)
     {
-        $this->rows = collect($data)->map(function ($model) {
-            return new Row($this, $model);
+        $this->rows = $data->map(function ($row) {
+            return new Row($this, $row);
         });
 
         foreach ($this->rowsCallbacks as $callback) {

+ 7 - 26
src/Grid/Column.php

@@ -515,20 +515,19 @@ class Column
     /**
      * Fill all data to every column.
      *
-     * @param array $data
+     * @param \Illuminate\Support\Collection $data
      */
-    public function fill(array &$data)
+    public function fill($data)
     {
-        $name = $this->getSnakeName($this->name);
-
         $i = 0;
-        foreach ($data as $key => &$row) {
+
+        foreach ($data as $key => $row) {
             $i++;
             if (! isset($row['#'])) {
                 $row['#'] = $i;
             }
 
-            $this->original = $value = Arr::get($row, $name);
+            $this->original = $value = Arr::get($row, $this->name);
 
             $this->value = $value = $this->htmlEntityEncode($value);
 
@@ -536,35 +535,17 @@ class Column
 
             $this->processConditions();
 
-            Arr::set($row, $name, $value);
+            Helper::arraySet($row, $this->name, $value);
 
             if ($this->hasDisplayCallbacks()) {
                 $value = $this->callDisplayCallbacks($this->original);
-                Arr::set($row, $name, $value);
+                Helper::arraySet($row, $this->name, $value);
             }
         }
 
         $this->value = $value ?? null;
     }
 
-    protected function getSnakeName($name)
-    {
-        if (! Str::contains($name, '.')) {
-            return $name;
-        }
-
-        $names = explode('.', $name);
-        $count = count($names);
-
-        foreach ($names as $i => &$name) {
-            if ($i + 1 < $count) {
-                $name = Str::snake($name);
-            }
-        }
-
-        return implode('.', $names);
-    }
-
     /**
      * @return void
      */

+ 5 - 5
src/Grid/Row.php

@@ -36,7 +36,7 @@ class Row implements Arrayable
     public function __construct(Grid $grid, $data)
     {
         $this->grid = $grid;
-        $this->data = new Fluent($data);
+        $this->data = is_array($data) ? new Fluent($data) : $data;
     }
 
     /**
@@ -46,7 +46,7 @@ class Row implements Arrayable
      */
     public function getKey()
     {
-        return $this->data->get($this->grid->getKeyName());
+        return $this->data->{$this->grid->getKeyName()};
     }
 
     /**
@@ -123,7 +123,7 @@ class Row implements Arrayable
     /**
      * Get data of this row.
      *
-     * @return Fluent
+     * @return Fluent|\Illuminate\Database\Eloquent\Model
      */
     public function model()
     {
@@ -139,7 +139,7 @@ class Row implements Arrayable
      */
     public function __get($attr)
     {
-        return $this->data->get($attr);
+        return $this->data->{$attr};
     }
 
     /**
@@ -167,7 +167,7 @@ class Row implements Arrayable
     {
         if (is_null($value)) {
             return $this->output(
-                Arr::get($this->toArray(), $name)
+                Arr::get($this->data, $name)
             );
         }
 

+ 38 - 0
src/Support/Helper.php

@@ -862,4 +862,42 @@ class Helper
 
         return $html;
     }
+
+    /**
+     * Set an array item to a given value using "dot" notation.
+     *
+     * If no key is given to the method, the entire array will be replaced.
+     *
+     * @param  array|\ArrayAccess  $array
+     * @param  string  $key
+     * @param  mixed   $value
+     * @return array
+     */
+    public static function arraySet(&$array, $key, $value)
+    {
+        if (is_null($key)) {
+            return $array = $value;
+        }
+
+        $keys = explode('.', $key);
+        $default = null;
+
+        while (count($keys) > 1) {
+            $key = array_shift($keys);
+
+            if (! isset($array[$key]) || (! is_array($array[$key]) && ! $array[$key] instanceof \ArrayAccess)) {
+                $array[$key] = [];
+            }
+
+            if (is_array($array)) {
+                $array = &$array[$key];
+            } else {
+                $array[$key] = static::arraySet($array[$key], implode('.', $keys), $value);
+            }
+        }
+
+        $array[array_shift($keys)] = $value;
+
+        return $array;
+    }
 }