Browse Source

Merge pull request #102 from youyingxiang/master

详情页多列布局
Jiang Qinghua 5 years ago
parent
commit
d1e5ad8021
5 changed files with 193 additions and 4 deletions
  1. 11 3
      resources/views/show/panel.blade.php
  2. 7 0
      resources/views/show/row.blade.php
  3. 33 1
      src/Show.php
  4. 1 0
      src/Show/Panel.php
  5. 141 0
      src/Show/Row.php

+ 11 - 3
resources/views/show/panel.blade.php

@@ -6,9 +6,17 @@
 @endif
 <div class="box-body">
     <div class="form-horizontal mt-1">
-        @foreach($fields as $field)
-            {!! $field->render() !!}
-        @endforeach
+        @if($rows->isEmpty())
+            @foreach($fields as $field)
+                {!! $field->render() !!}
+            @endforeach
+        @else
+            <div>
+                @foreach($rows as $row)
+                    {!! $row->render() !!}
+                @endforeach
+            </div>
+        @endif
         <div class="clearfix"></div>
     </div>
 </div>

+ 7 - 0
resources/views/show/row.blade.php

@@ -0,0 +1,7 @@
+<div class="row">
+    @foreach($fields as $field)
+        <div class="col-md-{{ $field['width'] }}">
+            {!! $field['element']->render() !!}
+        </div>
+    @endforeach
+</div>

+ 33 - 1
src/Show.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin;
 
+use Closure;
 use Dcat\Admin\Contracts\Repository;
 use Dcat\Admin\Show\AbstractTool;
 use Dcat\Admin\Show\Divider;
@@ -9,6 +10,7 @@ use Dcat\Admin\Show\Field;
 use Dcat\Admin\Show\Newline;
 use Dcat\Admin\Show\Panel;
 use Dcat\Admin\Show\Relation;
+use Dcat\Admin\Show\Row;
 use Dcat\Admin\Show\Tools;
 use Dcat\Admin\Traits\HasBuilderEvents;
 use Illuminate\Contracts\Support\Arrayable;
@@ -85,6 +87,10 @@ class Show implements Renderable
      * @var Panel
      */
     protected $panel;
+    /**
+     * @var \Illuminate\Support\Collection
+     */
+    protected $rows;
 
     /**
      * Show constructor.
@@ -108,7 +114,7 @@ class Show implements Renderable
             default:
                 $this->setKey($id);
         }
-
+        $this->rows = new Collection();
         $this->builder = $builder;
 
         $this->initModel($model);
@@ -679,6 +685,11 @@ class Show implements Renderable
 
             $this->fields->each->fill($model);
             $this->relations->each->model($model);
+            $this->rows->each(function ($row) {
+                $row->getFields()->each(function ($field) {
+                    $field['element']->fill($this->model());
+                });
+            });
 
             $this->callComposing();
 
@@ -693,6 +704,27 @@ class Show implements Renderable
         }
     }
 
+    /**
+     * Add a row in Show.
+     *
+     * @param Closure $callback
+     *
+     * @return $this
+     */
+    public function row(Closure $callback)
+    {
+        $this->rows->push(new Row($callback, $this));
+        return $this;
+    }
+
+    /**
+     * @return \Illuminate\Support\Collection
+     */
+    public function rows()
+    {
+        return $this->rows;
+    }
+
     /**
      * Add a model field to show.
      *

+ 1 - 0
src/Show/Panel.php

@@ -59,6 +59,7 @@ class Panel implements Renderable
         $this->data = [
             'fields' => new Collection(),
             'tools'  => new Tools($this),
+            'rows' => $this->parent->rows(),
             'style'  => 'default',
             'title'  => trans('admin.detail'),
         ];

+ 141 - 0
src/Show/Row.php

@@ -0,0 +1,141 @@
+<?php
+
+namespace Dcat\Admin\Show;
+
+use Dcat\Admin\Show;
+use Illuminate\Contracts\Support\Renderable;
+use Illuminate\Support\Collection;
+
+class Row implements Renderable
+{
+
+    /**
+     * Callback for add field to current row.s.
+     *
+     * @var \Closure
+     */
+    protected $callback;
+
+    /**
+     * Parent show.
+     *
+     * @var Show
+     */
+    protected $show;
+
+    /**
+     * @var \Illuminate\Support\Collection
+     */
+    protected $fields;
+
+    /**
+     * Default field width for appended field.
+     *
+     * @var int
+     */
+    protected $defaultFieldWidth = 12;
+
+    /**
+     * Row constructor.
+     *
+     * @param \Closure $callback
+     * @param Show $show
+     */
+    public function __construct(\Closure $callback, Show $show)
+    {
+        $this->callback = $callback;
+
+        $this->show = $show;
+
+        $this->fields = new Collection();
+
+        call_user_func($this->callback, $this);
+    }
+
+    /**
+     * Render the row.
+     *
+     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
+     */
+    public function render()
+    {
+        return view('admin::show.row', ['fields' => $this->fields]);
+    }
+
+    /**
+     * @return \Illuminate\Support\Collection
+     */
+    public function getFields()
+    {
+        return $this->fields;
+    }
+
+
+    /**
+     * Set width for a incomming field.
+     *
+     * @param int $width
+     *
+     * @return $this
+     */
+    public function width($width = 12)
+    {
+        $this->defaultFieldWidth = $width;
+
+        return $this;
+    }
+
+    /**
+     * @param        $name
+     * @param string $label
+     *
+     * @return \Dcat\Admin\Show\Field
+     */
+    public function field($name, $label = '')
+    {
+        $field = $this->show->field($name, $label);
+        $this->pushField($field);
+        return $field;
+    }
+
+    /**
+     * Add field
+     * @param $name
+     *
+     * @return \Dcat\Admin\Show\Field|\Illuminate\Support\Collection
+     */
+    public function __get($name)
+    {
+        $field = $this->show->__get($name);
+        $this->pushField($field);
+        return $field;
+    }
+
+    /**
+     * @param $method
+     * @param $arguments
+     *
+     * @return \Dcat\Admin\Show\Field
+     */
+    public function __call($method, $arguments)
+    {
+        $field = $this->show->__call($method, $arguments);
+
+        $this->pushField($field);
+
+        return $field;
+    }
+
+    /**
+     * @param $field
+     */
+    public function pushField($field)
+    {
+        $this->fields->push([
+            'width' => $this->defaultFieldWidth,
+            'element' => $field,
+        ]);
+    }
+
+
+}