Przeglądaj źródła

wip - 表格固定列支持组合表头功能

jqh 4 lat temu
rodzic
commit
c5a230b385

+ 46 - 10
resources/views/grid/fixed-table.blade.php

@@ -73,15 +73,33 @@
                 </table>
             </div>
 
-            @if($grid->leftVisibleColumns()->isNotEmpty())
+            @if ($grid->leftVisibleColumns()->isNotEmpty() || $grid->leftVisibleComplexColumns()->isNotEmpty())
                 <div class="table-wrap table-fixed table-fixed-left">
                     <table class="custom-data-table dataTable {{ $grid->formatTableClass() }} ">
                         <thead>
-                        <tr>
-                            @foreach($grid->leftVisibleColumns() as $column)
-                                <th {!! $column->formatTitleAttributes() !!}>{!! $column->getLabel() !!}{!! $column->renderHeader() !!}</th>
+
+                        @if ($grid->getComplexHeaders())
+                            <tr>
+                                @foreach($grid->leftVisibleComplexColumns() as $header)
+                                    {!! $header->render() !!}
+                                @endforeach
+                            </tr>
+                            <tr>
+                            @foreach($grid->leftVisibleComplexColumns() as $header)
+                                @if ($header->getColumnNames()->count() > 1)
+                                    @foreach($header->columns() as $column)
+                                        <th {!! $column->formatTitleAttributes() !!}>{!! $column->getLabel() !!}{!! $column->renderHeader() !!}</th>
+                                    @endforeach
+                                @endif
                             @endforeach
-                        </tr>
+                            </tr>
+                        @else
+                            <tr>
+                                @foreach($grid->leftVisibleColumns() as $column)
+                                    <th {!! $column->formatTitleAttributes() !!}>{!! $column->getLabel() !!}{!! $column->renderHeader() !!}</th>
+                                @endforeach
+                            </tr>
+                        @endif
                         </thead>
                         <tbody>
 
@@ -99,15 +117,33 @@
                 </div>
             @endif
 
-            @if($grid->rightVisibleColumns()->isNotEmpty())
+            @if ($grid->rightVisibleColumns()->isNotEmpty() || $grid->rightVisibleComplexColumns()->isNotEmpty())
                 <div class="table-wrap table-fixed table-fixed-right">
                     <table class="custom-data-table dataTable {{ $grid->formatTableClass() }} ">
                         <thead>
-                        <tr>
-                            @foreach($grid->rightVisibleColumns() as $column)
-                                <th {!! $column->formatTitleAttributes() !!}>{!! $column->getLabel() !!}{!! $column->renderHeader() !!}</th>
+                        @if ($grid->getComplexHeaders())
+                            <tr>
+                                @foreach($grid->rightVisibleComplexColumns() as $header)
+                                    {!! $header->render() !!}
+                                @endforeach
+                            </tr>
+                            <tr>
+                            @foreach($grid->rightVisibleComplexColumns() as $header)
+                                @if ($header->getColumnNames()->count() > 1)
+                                    @foreach($header->columns() as $column)
+                                        <th {!! $column->formatTitleAttributes() !!}>{!! $column->getLabel() !!}{!! $column->renderHeader() !!}</th>
+                                    @endforeach
+                                @endif
                             @endforeach
-                        </tr>
+                            </tr>
+                        @else
+                            <tr>
+                                @foreach($grid->rightVisibleColumns() as $column)
+                                    <th {!! $column->formatTitleAttributes() !!}>{!! $column->getLabel() !!}{!! $column->renderHeader() !!}</th>
+                                @endforeach
+                            </tr>
+                        @endif
+
                         </thead>
 
                         <tbody>

+ 37 - 9
src/Grid.php

@@ -47,12 +47,19 @@ class Grid
     protected $model;
 
     /**
-     * Collection of all grid columns.
+     * Collection of grid columns.
      *
      * @var \Illuminate\Support\Collection
      */
     protected $columns;
 
+    /**
+     * Collection of all grid columns.
+     *
+     * @var \Illuminate\Support\Collection
+     */
+    protected $allColumns;
+
     /**
      * Collection of all data rows.
      *
@@ -185,6 +192,7 @@ class Grid
     {
         $this->model = new Model(request(), $repository);
         $this->columns = new Collection();
+        $this->allColumns = new Collection();
         $this->rows = new Collection();
         $this->builder = $builder;
 
@@ -274,7 +282,7 @@ class Grid
      *
      * @param array $columns
      *
-     * @return Collection|void
+     * @return Collection|Column[]|void
      */
     public function columns($columns = null)
     {
@@ -295,6 +303,14 @@ class Grid
         }
     }
 
+    /**
+     * @return Collection|Column[]
+     */
+    public function allColumns()
+    {
+        return $this->allColumns;
+    }
+
     /**
      * Add column to grid.
      *
@@ -308,6 +324,23 @@ class Grid
         $column = $this->newColumn($field, $label);
 
         $this->columns->put($field, $column);
+        $this->allColumns->put($field, $column);
+
+        return $column;
+    }
+
+    /**
+     * @param string $field
+     * @param string $label
+     *
+     * @return Column
+     */
+    public function prependColumn($field = '', $label = '')
+    {
+        $column = $this->newColumn($field, $label);
+
+        $this->columns->prepend($column, $field);
+        $this->allColumns->prepend($column, $field);
 
         return $column;
     }
@@ -503,17 +536,12 @@ class Grid
         $rowSelector = $this->rowSelector();
         $keyName = $this->getKeyName();
 
-        $column = $this->newColumn(
+        $this->prependColumn(
             Grid\Column::SELECT_COLUMN_NAME,
             $rowSelector->renderHeader()
-        );
-        $column->setGrid($this);
-
-        $column->display(function () use ($rowSelector, $keyName) {
+        )->display(function () use ($rowSelector, $keyName) {
             return $rowSelector->renderColumn($this, $this->{$keyName});
         });
-
-        $this->columns->prepend($column, Grid\Column::SELECT_COLUMN_NAME);
     }
 
     /**

+ 16 - 5
src/Grid/ComplexHeader.php

@@ -5,6 +5,7 @@ namespace Dcat\Admin\Grid;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Grid\Column\Help;
 use Dcat\Admin\Widgets\Widget;
+use Illuminate\Support\Collection;
 
 class ComplexHeader extends Widget
 {
@@ -32,19 +33,29 @@ class ComplexHeader extends Widget
     {
         $this->grid = $grid;
         $this->label = admin_trans_field($label);
-        $this->columnNames = $columnNames;
+        $this->columnNames = collect($columnNames);
 
-        $this->setupAttributes();
+        $this->addDefaultAttributes();
     }
 
     /**
-     * @return array
+     * @return Collection
      */
     public function getColumnNames()
     {
         return $this->columnNames;
     }
 
+    /**
+     * @return Collection
+     */
+    public function columns()
+    {
+        return $this->columnNames->map(function ($name) {
+            return $this->grid->allColumns()->get($name);
+        })->filter();
+    }
+
     /**
      * 默认隐藏字段
      * 开启responsive模式有效.
@@ -107,9 +118,9 @@ class ComplexHeader extends Widget
         return $this->append((new Help($message, $style, $placement))->render());
     }
 
-    protected function setupAttributes()
+    protected function addDefaultAttributes()
     {
-        $count = count($this->columnNames);
+        $count = $this->columnNames->count();
 
         if ($count == 1) {
             $this->htmlAttributes['rowspan'] = 2;

+ 16 - 0
src/Grid/Concerns/CanFixColumns.php

@@ -63,4 +63,20 @@ trait CanFixColumns
     {
         return $this->fixColumns->rightColumns();
     }
+
+    /**
+     * @return Collection
+     */
+    public function leftVisibleComplexColumns()
+    {
+        return $this->fixColumns->leftComplexColumns();
+    }
+
+    /**
+     * @return Collection
+     */
+    public function rightVisibleComplexColumns()
+    {
+        return $this->fixColumns->rightComplexColumns();
+    }
 }

+ 16 - 9
src/Grid/Concerns/HasComplexHeaders.php

@@ -4,13 +4,14 @@ namespace Dcat\Admin\Grid\Concerns;
 
 use Dcat\Admin\Grid\Column;
 use Dcat\Admin\Grid\ComplexHeader;
+use Illuminate\Support\Collection;
 
 trait HasComplexHeaders
 {
     /**
-     * @var ComplexHeader[]
+     * @var ComplexHeader[]|Collection
      */
-    protected $complexHeaders = [];
+    protected $complexHeaders;
 
     /**
      * Merge cells.
@@ -26,13 +27,17 @@ trait HasComplexHeaders
             throw new \InvalidArgumentException('Invalid column names.');
         }
 
+        if (! $this->complexHeaders) {
+            $this->complexHeaders = new Collection();
+        }
+
         $this->withBorder();
 
         return $this->complexHeaders[$label] = new ComplexHeader($this, $label, $columnNames);
     }
 
     /**
-     * @return ComplexHeader[]
+     * @return ComplexHeader[]|Collection
      */
     public function getComplexHeaders()
     {
@@ -48,7 +53,7 @@ trait HasComplexHeaders
             return;
         }
 
-        $originalHeaders = $this->complexHeaders;
+        $originalHeaders = $this->complexHeaders->toArray();
         $originalColumns = $this->columns;
 
         $headersColumns = $this->complexHeaders = $this->columns = [];
@@ -56,7 +61,7 @@ trait HasComplexHeaders
         foreach ($originalHeaders as $header) {
             $headersColumns = array_merge(
                 $headersColumns,
-                $tmp = $header->getColumnNames()
+                $tmp = $header->getColumnNames()->toArray()
             );
             foreach ($tmp as &$name) {
                 if ($column = $originalColumns->get($name)) {
@@ -88,10 +93,12 @@ trait HasComplexHeaders
         );
 
         $this->columns = collect($this->columns);
-        $this->complexHeaders = array_merge(
-            $beforeHeaders,
-            array_values($originalHeaders),
-            $afterHeaders
+        $this->complexHeaders = collect(
+            array_merge(
+                $beforeHeaders,
+                array_values($originalHeaders),
+                $afterHeaders
+            )
         );
     }
 

+ 54 - 3
src/Grid/FixColumns.php

@@ -33,6 +33,16 @@ class FixColumns
      */
     protected $right;
 
+    /**
+     * @var Collection
+     */
+    protected $complexLeft;
+
+    /**
+     * @var Collection
+     */
+    protected $complexRight;
+
     /**
      * @var string
      */
@@ -53,6 +63,8 @@ class FixColumns
 
         $this->left = Collection::make();
         $this->right = Collection::make();
+        $this->complexLeft = Collection::make();
+        $this->complexRight = Collection::make();
     }
 
     /**
@@ -71,25 +83,64 @@ class FixColumns
         return $this->right;
     }
 
+    /**
+     * @return Collection
+     */
+    public function leftComplexColumns()
+    {
+        return $this->complexLeft;
+    }
+
+    /**
+     * @return Collection
+     */
+    public function rightComplexColumns()
+    {
+        return $this->complexRight;
+    }
+
     /**
      * @return \Closure
      */
     public function apply()
     {
         $this->grid->view($this->view);
+        $complexHeaders = $this->grid->getComplexHeaders();
 
         if ($this->head > 0) {
-            $this->left = $this->grid->columns()->slice(0, $this->head);
+            if ($complexHeaders) {
+                $this->complexLeft = $complexHeaders->slice(0, $this->head);
+                $this->left = $this->formatColumns($this->complexLeft);
+            } else {
+                $this->left = $this->grid->columns()->slice(0, $this->head);
+            }
         }
 
         if ($this->tail < 0) {
-            $this->right = $this->grid->columns()->slice($this->tail);
+            if ($complexHeaders) {
+                $this->complexRight = $complexHeaders->slice($this->tail);
+                $this->right = $this->formatColumns($this->complexRight);
+            } else {
+                $this->right = $this->grid->columns()->slice($this->tail);
+            }
         }
 
         $this->addStyle();
         $this->addScript();
     }
 
+    protected function formatColumns(Collection $complexHeaders)
+    {
+        return $complexHeaders
+            ->map(function (ComplexHeader $header) {
+                return $header->getColumnNames()->toArray();
+            })
+            ->flatten()
+            ->filter()
+            ->map(function ($name) {
+                return $this->grid->allColumns()->get($name);
+            });
+    }
     /**
      * @return $this
      */
@@ -114,7 +165,7 @@ class FixColumns
     if ($('.table-main').width() >= $('.table-main').prop('scrollWidth')) {
         $('.table-fixed').hide();
     } else {
-        var height = ($(window).height() - 200);
+        var height = ($(window).height() - 210);
         
         $('.table-main,.table-fixed').css({height: height + 'px'});
         $('.table-fixed-right').css({right: '15px'});