Преглед изворни кода

增加自定义渲染表格分页信息功能

jqh пре 5 година
родитељ
комит
f1b95c6b03

+ 1 - 1
resources/views/filter/button.blade.php

@@ -4,7 +4,7 @@
             @if($only_scopes)data-toggle="dropdown"@endif
             @if($scopes->isNotEmpty()) style="border-right: 0" @endif
     >
-        <i class="feather icon-filter"></i>@if($show_filter_text)<span class="d-none d-sm-inline">&nbsp;&nbsp;{{ trans('admin.filter') }}</span>@endif
+        <i class="feather icon-filter"></i>@if($filter_text)<span class="d-none d-sm-inline">&nbsp;&nbsp;{{ trans('admin.filter') }}</span>@endif
 
         @if($valueCount) &nbsp;({!! $valueCount !!}) @endif
     </button>

+ 1 - 1
resources/views/grid/fixed-table.blade.php

@@ -142,5 +142,5 @@
 
     {!! $grid->renderFooter() !!}
 
-    @include('admin::grid.table-pagination')
+    {!! $grid->renderPagination() !!}
 </div>

+ 2 - 2
resources/views/grid/table-pagination.blade.php

@@ -1,6 +1,6 @@
-@if ($paginator = $grid->paginator())
+@if ($grid->allowPagination())
     <div class="box-footer d-block clearfix ">
-        {!! $paginator->render() !!}
+        {!! $grid->paginator()->render() !!}
     </div>
 @else
     <div class="box-footer d-block clearfix text-80 " style="height:48px;line-height:25px;">

+ 1 - 1
resources/views/grid/table.blade.php

@@ -53,7 +53,7 @@
 
     {!! $grid->renderFooter() !!}
 
-    @include('admin::grid.table-pagination')
+    {!! $grid->renderPagination() !!}
 
 </div>
 

+ 1 - 1
src/Admin.php

@@ -31,7 +31,7 @@ class Admin
     use HasAssets;
     use HasHtml;
 
-    const VERSION = '2.0.16-beta';
+    const VERSION = '2.0.17-beta';
 
     const SECTION = [
         // 往 <head> 标签内输入内容

+ 23 - 23
src/Grid.php

@@ -154,21 +154,21 @@ class Grid
      * @var array
      */
     protected $options = [
-        'show_pagination'        => true,
-        'show_filter'            => true,
-        'show_actions'           => true,
-        'show_quick_edit_button' => false,
-        'show_edit_button'       => true,
-        'show_view_button'       => true,
-        'show_delete_button'     => true,
-        'show_row_selector'      => true,
-        'show_create_button'     => true,
-        'show_bordered'          => false,
-        'table_collapse'         => true,
-        'show_toolbar'           => true,
-        'create_mode'            => self::CREATE_MODE_DEFAULT,
-        'dialog_form_area'       => ['700px', '670px'],
-        'table_class'            => ['table', 'custom-data-table', 'data-table'],
+        'pagination'        => true,
+        'filter'            => true,
+        'actions'           => true,
+        'quick_edit_button' => false,
+        'edit_button'       => true,
+        'view_button'       => true,
+        'delete_button'     => true,
+        'row_selector'      => true,
+        'create_button'     => true,
+        'bordered'          => false,
+        'table_collapse'    => true,
+        'toolbar'           => true,
+        'create_mode'       => self::CREATE_MODE_DEFAULT,
+        'dialog_form_area'  => ['700px', '670px'],
+        'table_class'       => ['table', 'custom-data-table', 'data-table'],
     ];
 
     /**
@@ -396,7 +396,7 @@ class Grid
 
     public function formatTableClass()
     {
-        if ($this->options['show_bordered']) {
+        if ($this->options['bordered']) {
             $this->addTableClass(['table-bordered', 'complex-headers', 'data-table']);
         }
 
@@ -533,7 +533,7 @@ class Grid
      */
     protected function prependRowSelectorColumn()
     {
-        if (! $this->options['show_row_selector']) {
+        if (! $this->options['row_selector']) {
             return;
         }
 
@@ -567,7 +567,7 @@ class Grid
      */
     public function renderCreateButton()
     {
-        if (! $this->options['show_create_button']) {
+        if (! $this->options['create_button']) {
             return '';
         }
 
@@ -581,7 +581,7 @@ class Grid
      */
     public function withBorder(bool $value = true)
     {
-        $this->options['show_bordered'] = $value;
+        $this->options['bordered'] = $value;
 
         return $this;
     }
@@ -699,7 +699,7 @@ HTML;
 
     protected function setUpOptions()
     {
-        if ($this->options['show_bordered']) {
+        if ($this->options['bordered']) {
             $this->tableCollapse(false);
         }
     }
@@ -713,7 +713,7 @@ HTML;
     {
         $this->tools->disableBatchActions($disable);
 
-        return $this->option('show_row_selector', ! $disable);
+        return $this->option('row_selector', ! $disable);
     }
 
     /**
@@ -733,7 +733,7 @@ HTML;
      */
     public function disableCreateButton(bool $disable = true)
     {
-        return $this->option('show_create_button', ! $disable);
+        return $this->option('create_button', ! $disable);
     }
 
     /**
@@ -753,7 +753,7 @@ HTML;
      */
     public function allowCreateButton()
     {
-        return $this->options['show_create_button'];
+        return $this->options['create_button'];
     }
 
     /**

+ 1 - 1
src/Grid/Concerns/CanFixColumns.php

@@ -41,7 +41,7 @@ trait CanFixColumns
     protected function applyFixColumns()
     {
         if ($this->fixColumns) {
-            if (! $this->options['show_bordered'] && ! $this->options['table_collapse']) {
+            if (! $this->options['bordered'] && ! $this->options['table_collapse']) {
                 $this->tableCollapse();
             }
 

+ 6 - 6
src/Grid/Concerns/HasActions.php

@@ -87,7 +87,7 @@ trait HasActions
      */
     protected function appendActionsColumn()
     {
-        if (! $this->options['show_actions']) {
+        if (! $this->options['actions']) {
             return;
         }
 
@@ -106,7 +106,7 @@ trait HasActions
      */
     public function disableActions(bool $disable = true)
     {
-        return $this->option('show_actions', ! $disable);
+        return $this->option('actions', ! $disable);
     }
 
     /**
@@ -128,7 +128,7 @@ trait HasActions
      */
     public function disableEditButton(bool $disable = true)
     {
-        $this->options['show_edit_button'] = ! $disable;
+        $this->options['edit_button'] = ! $disable;
 
         return $this;
     }
@@ -152,7 +152,7 @@ trait HasActions
      */
     public function disableQuickEditButton(bool $disable = true)
     {
-        $this->options['show_quick_edit_button'] = ! $disable;
+        $this->options['quick_edit_button'] = ! $disable;
 
         return $this;
     }
@@ -176,7 +176,7 @@ trait HasActions
      */
     public function disableViewButton(bool $disable = true)
     {
-        $this->options['show_view_button'] = ! $disable;
+        $this->options['view_button'] = ! $disable;
 
         return $this;
     }
@@ -202,7 +202,7 @@ trait HasActions
      */
     public function disableDeleteButton(bool $disable = true)
     {
-        $this->options['show_delete_button'] = ! $disable;
+        $this->options['delete_button'] = ! $disable;
 
         return $this;
     }

+ 2 - 2
src/Grid/Concerns/HasFilter.php

@@ -69,7 +69,7 @@ trait HasFilter
      */
     public function renderFilter()
     {
-        if (! $this->options['show_filter']) {
+        if (! $this->options['filter']) {
             return '';
         }
 
@@ -97,7 +97,7 @@ trait HasFilter
     {
         $this->filter->disableCollapse($disable);
 
-        return $this->option('show_filter', ! $disable);
+        return $this->option('filter', ! $disable);
     }
 
     /**

+ 54 - 7
src/Grid/Concerns/HasPaginator.php

@@ -6,6 +6,11 @@ use Dcat\Admin\Grid\Tools;
 
 trait HasPaginator
 {
+    /**
+     * @var Tools\Paginator
+     */
+    protected $paginator;
+
     /**
      * Per-page options.
      *
@@ -20,6 +25,16 @@ trait HasPaginator
      */
     protected $perPage = 20;
 
+    /**
+     * @var string
+     */
+    protected $paginatorClass = Tools\Paginator::class;
+
+    /**
+     * @var \Closure
+     */
+    protected $paginationCallback;
+
     /**
      * Paginate the grid.
      *
@@ -42,6 +57,30 @@ trait HasPaginator
         return $this->perPage;
     }
 
+    /**
+     * @param string $paginator
+     *
+     * @return $this
+     */
+    public function setPaginatorClass(string $paginator)
+    {
+        $this->paginatorClass = $paginator;
+
+        return $this;
+    }
+
+    /**
+     * @param \Closure
+     *
+     * @return $this
+     */
+    public function pagination(\Closure $callback)
+    {
+        $this->paginationCallback = $callback;
+
+        return $this;
+    }
+
     /**
      * Get the grid paginator.
      *
@@ -49,11 +88,7 @@ trait HasPaginator
      */
     public function paginator()
     {
-        if (! $this->options['show_pagination']) {
-            return;
-        }
-
-        return new Tools\Paginator($this);
+        return $this->paginator ?: ($this->paginator = new $this->paginatorClass($this));
     }
 
     /**
@@ -63,7 +98,7 @@ trait HasPaginator
      */
     public function allowPagination()
     {
-        return $this->options['show_pagination'];
+        return $this->options['pagination'];
     }
 
     /**
@@ -105,7 +140,7 @@ trait HasPaginator
     {
         $this->model->usePaginate(! $disable);
 
-        return $this->option('show_pagination', ! $disable);
+        return $this->option('pagination', ! $disable);
     }
 
     /**
@@ -119,4 +154,16 @@ trait HasPaginator
     {
         return $this->disablePagination(! $val);
     }
+
+    /**
+     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\View\View|string
+     */
+    public function renderPagination()
+    {
+        if ($callback = $this->paginationCallback) {
+            return $callback($this);
+        }
+
+        return view('admin::grid.table-pagination', ['grid' => $this]);
+    }
 }

+ 2 - 2
src/Grid/Concerns/HasTools.php

@@ -101,7 +101,7 @@ trait HasTools
      */
     public function disableToolbar(bool $val = true)
     {
-        return $this->option('show_toolbar', ! $val);
+        return $this->option('toolbar', ! $val);
     }
 
     /**
@@ -202,7 +202,7 @@ trait HasTools
     public function allowToolbar()
     {
         if (
-            $this->option('show_toolbar')
+            $this->option('toolbar')
             && (
                 $this->tools()->has()
                 || $this->allowExporter()

+ 4 - 4
src/Grid/Displayers/Actions.php

@@ -176,10 +176,10 @@ class Actions extends AbstractDisplayer
      */
     protected function resetDefaultActions()
     {
-        $this->disableView(! $this->grid->option('show_view_button'));
-        $this->disableEdit(! $this->grid->option('show_edit_button'));
-        $this->disableQuickEdit(! $this->grid->option('show_quick_edit_button'));
-        $this->disableDelete(! $this->grid->option('show_delete_button'));
+        $this->disableView(! $this->grid->option('view_button'));
+        $this->disableEdit(! $this->grid->option('edit_button'));
+        $this->disableQuickEdit(! $this->grid->option('quick_edit_button'));
+        $this->disableDelete(! $this->grid->option('delete_button'));
     }
 
     /**

+ 1 - 1
src/Grid/Model.php

@@ -173,7 +173,7 @@ class Model
     /**
      * @return AbstractPaginator|LengthAwarePaginator
      */
-    public function paginator(): AbstractPaginator
+    public function paginator(): ?AbstractPaginator
     {
         $this->buildData();
 

+ 1 - 1
src/Grid/Tools/ExportButton.php

@@ -79,7 +79,7 @@ JS;
     protected function renderExportSelectedRows()
     {
         if (
-            ! $this->grid->option('show_row_selector')
+            ! $this->grid->option('row_selector')
             || ! $this->grid->exporter()->option('show_export_selected_rows')
         ) {
             return;

+ 9 - 9
src/Grid/Tools/FilterButton.php

@@ -150,17 +150,17 @@ JS;
 
         $this->addScript();
 
-        $onlyScopes = ((! $filters || $this->parent->option('show_filter') === false) && ! $scopres->isEmpty()) ? true : false;
+        $onlyScopes = ((! $filters || $this->parent->option('filter') === false) && ! $scopres->isEmpty()) ? true : false;
 
         $variables = [
-            'scopes'           => $scopres,
-            'current_label'    => $this->currentScopeLabel(),
-            'url_no_scopes'    => $filter->urlWithoutScopes(),
-            'btn_class'        => $this->getElementClassName(),
-            'expand'           => $filter->expand,
-            'show_filter_text' => true,
-            'only_scopes'      => $onlyScopes,
-            'valueCount'       => $valueCount,
+            'scopes'        => $scopres,
+            'current_label' => $this->currentScopeLabel(),
+            'url_no_scopes' => $filter->urlWithoutScopes(),
+            'btn_class'     => $this->getElementClassName(),
+            'expand'        => $filter->expand,
+            'filter_text'   => true,
+            'only_scopes'   => $onlyScopes,
+            'valueCount'    => $valueCount,
         ];
 
         return view($this->view, $variables)->render();

+ 1 - 1
src/Grid/Tools/Paginator.php

@@ -17,7 +17,7 @@ class Paginator implements Renderable
     /**
      * @var \Illuminate\Pagination\LengthAwarePaginator
      */
-    protected $paginator = null;
+    public $paginator = null;
 
     /**
      * Create a new Paginator instance.