ソースを参照

表格排序调整;数据详情关联关系功能增加width方法

jqh 4 年 前
コミット
0b8486bed1

+ 27 - 0
resources/assets/dcat/sass/components/_utilities.scss

@@ -74,6 +74,33 @@
   font-weight: 700;
 }
 
+// margin 1.5 pading 1.5
+.mt-1-5 {
+  margin-top: 1.5rem!important;
+}
+.mb-1-5 {
+  margin-bottom: 1.5rem!important;
+}
+.ml-1-5 {
+  margin-left: 1.5rem!important;
+}
+.mr-1-5 {
+  margin-right: 1.5rem!important;
+}
+
+.pt-1-5 {
+  padding-top: 1.5rem!important;
+}
+.pb-1-5 {
+  padding-bottom: 1.5rem!important;
+}
+.pl-1-5 {
+  padding-left: 1.5rem!important;
+}
+.pr-1-5 {
+  padding-right: 1.5rem!important;
+}
+
 // Font style
 .text-italic {
   font-style: italic;

+ 8 - 4
resources/views/show/container.blade.php

@@ -2,10 +2,14 @@
     <div class="col-md-12">{!! $panel !!}</div>
 
     @if($relations->count())
-        <div class="col-md-12 show-relation-container" style="top:10px">
-            @foreach($relations as $relation)
-                {!!  $relation->render() !!}
-            @endforeach
+        <div class="col-md-12">
+            <div class="row show-relation-container">
+                @foreach($relations as $relation)
+                    <div class="col-md-{{ $relation->width ?: 12 }}">
+                        {!!  $relation->render() !!}
+                    </div>
+                @endforeach
+            </div>
         </div>
     @endif
 </div>

+ 1 - 3
src/Grid/Column/HasHeader.php

@@ -52,9 +52,7 @@ trait HasHeader
      */
     public function sortable($cast = null)
     {
-        $sortName = $this->grid->model()->getSortName();
-
-        $sorter = new Sorter($sortName, $this->getName(), $cast);
+        $sorter = new Sorter($this->grid, $this->getName(), $cast);
 
         return $this->addHeader($sorter);
     }

+ 17 - 11
src/Grid/Column/Sorter.php

@@ -2,10 +2,16 @@
 
 namespace Dcat\Admin\Grid\Column;
 
+use Dcat\Admin\Grid;
 use Illuminate\Contracts\Support\Renderable;
 
 class Sorter implements Renderable
 {
+    /**
+     * @var Grid
+     */
+    protected $grid;
+
     /**
      * Sort arguments.
      *
@@ -20,11 +26,6 @@ class Sorter implements Renderable
      */
     protected $cast;
 
-    /**
-     * @var string
-     */
-    protected $sortName;
-
     /**
      * @var string
      */
@@ -33,13 +34,13 @@ class Sorter implements Renderable
     /**
      * Sorter constructor.
      *
-     * @param string $sortName
+     * @param Grid $grid
      * @param string $columnName
      * @param string $cast
      */
-    public function __construct($sortName, $columnName, $cast)
+    public function __construct(Grid $grid, $columnName, $cast)
     {
-        $this->sortName = $sortName;
+        $this->grid = $grid;
         $this->columnName = $columnName;
         $this->cast = $cast;
     }
@@ -51,7 +52,7 @@ class Sorter implements Renderable
      */
     protected function isSorted()
     {
-        $this->sort = app('request')->get($this->sortName);
+        $this->sort = app('request')->get($this->getSortName());
 
         if (empty($this->sort)) {
             return false;
@@ -60,6 +61,11 @@ class Sorter implements Renderable
         return isset($this->sort['column']) && $this->sort['column'] == $this->columnName;
     }
 
+    protected function getSortName()
+    {
+        return $this->grid->model()->getSortName();
+    }
+
     /**
      * @return string
      */
@@ -86,11 +92,11 @@ class Sorter implements Renderable
 
         if (! $this->isSorted() || $this->sort['type'] != 'asc') {
             $url = request()->fullUrlWithQuery([
-                $this->sortName => $sort,
+                $this->getSortName() => $sort,
             ]);
         } else {
             $url = request()->fullUrlWithQuery([
-                $this->sortName => [],
+                $this->getSortName() => [],
             ]);
         }
 

+ 1 - 1
src/Show/Field.php

@@ -138,7 +138,7 @@ class Field implements Renderable
      *
      * @return $this|array
      */
-    public function width(int $field = 8, int $label = 2)
+    public function width(int $field, int $label = 2)
     {
         $this->width = [
             'label' => $label,

+ 31 - 11
src/Show/Relation.php

@@ -4,6 +4,7 @@ namespace Dcat\Admin\Show;
 
 use Dcat\Admin\Grid;
 use Dcat\Admin\Show;
+use Dcat\Admin\Support\Helper;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Fluent;
 
@@ -37,6 +38,11 @@ class Relation extends Field
      */
     protected $model;
 
+    /**
+     * @var int
+     */
+    public $width = 12;
+
     /**
      * Relation constructor.
      *
@@ -70,11 +76,18 @@ class Relation extends Field
     }
 
     /**
-     * Render this relation panel.
+     * @param int $width
      *
-     * @return string
+     * @return $this
      */
-    public function render()
+    public function width(int $width, int $_ = 2)
+    {
+        $this->width = $width;
+
+        return $this;
+    }
+
+    protected function build()
     {
         $view = call_user_func($this->builder, $this->model);
 
@@ -84,18 +97,25 @@ class Relation extends Field
             return $view->render();
         }
 
-        if (! $view instanceof Grid) {
-            return $view;
+        if ($view instanceof Grid) {
+            return $view->setName($this->name)
+                ->title($this->title)
+                ->disableBatchDelete()
+                ->render();
         }
 
-        $view->setName($this->name)
-            ->title($this->title)
-            ->disableBatchDelete();
+        return Helper::render($view);
+    }
 
+    /**
+     * Render this relation panel.
+     *
+     * @return string
+     */
+    public function render()
+    {
         return <<<HTML
-<div class="mb-2">
-    {$view->render()}
-</div>
+<div class="mt-1-5">{$this->build()}</div>
 HTML;
     }
 }

+ 10 - 0
tests/Controllers/PainterController.php

@@ -7,6 +7,7 @@ use Dcat\Admin\Grid;
 use Dcat\Admin\Http\Controllers\AdminController;
 use Dcat\Admin\Show;
 use Tests\Models\Painter;
+use Tests\Models\Painting;
 
 class PainterController extends AdminController
 {
@@ -45,6 +46,15 @@ class PainterController extends AdminController
             $show->bio;
             $show->created_at;
             $show->updated_at;
+
+            $show->relation('paintings', function ($model) {
+                return Grid::make(Painting::where('painter_id', $model->getKey()), function (Grid $grid) {
+                    $grid->column('id')->sortable();
+                    $grid->column('title');
+                    $grid->column('body');
+                    $grid->column('completed_at')->sortable();
+                });
+            });
         });
     }