瀏覽代碼

增加 Grid::simplePaginate() 方法,支持使用 simplePaginate 分页功能

jqh 4 年之前
父節點
當前提交
55ba2e91a0

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

@@ -6,7 +6,7 @@
     <li class="page-item previous"><a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev"></a></li>
     @endif
 
-    <!-- Pagination Elements -->
+    @if(! empty($elements))
     @foreach ($elements as $element)
         <!-- "Three Dots" Separator -->
         @if (is_string($element))
@@ -24,6 +24,7 @@
             @endforeach
         @endif
     @endforeach
+    @endif
 
     <!-- Next Page Link -->
     @if ($paginator->hasMorePages())

+ 10 - 0
src/Grid/Concerns/HasPaginator.php

@@ -45,6 +45,16 @@ trait HasPaginator
         $this->model()->setPerPage($perPage);
     }
 
+    /**
+     * 是否使用 simplePaginate 方法分页.
+     *
+     * @param bool $value
+     */
+    public function simplePaginate(bool $value = true)
+    {
+        $this->model()->simple($value);
+    }
+
     /**
      * @return int
      */

+ 31 - 0
src/Grid/Model.php

@@ -100,6 +100,11 @@ class Model
      */
     protected $sortName = '_sort';
 
+    /**
+     * @var bool
+     */
+    protected $simple = false;
+
     /**
      * @var Grid
      */
@@ -180,6 +185,28 @@ class Model
         return $this->paginator;
     }
 
+    /**
+     * 是否使用 simplePaginate方法进行分页.
+     *
+     * @param bool $value
+     *
+     * @return $this
+     */
+    public function simple(bool $value = true)
+    {
+        $this->simple = $value;
+
+        return $this;
+    }
+
+    /**
+     * @return string
+     */
+    public function getPaginateMethod()
+    {
+        return $this->simple ? 'simplePaginate' : 'paginate';
+    }
+
     /**
      * @param int              $total
      * @param Collection|array $data
@@ -214,10 +241,14 @@ class Model
      * Enable or disable pagination.
      *
      * @param bool $use
+     *
+     * @reutrn $this;
      */
     public function usePaginate($use = true)
     {
         $this->usePaginate = $use;
+
+        return $this;
     }
 
     /**

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

@@ -79,7 +79,7 @@ class Paginator implements Renderable
         $parameters = [
             'first' => $this->paginator->firstItem(),
             'last'  => $this->paginator->lastItem(),
-            'total' => $this->paginator->total(),
+            'total' => method_exists($this->paginator, 'total') ? $this->paginator->total() : '...',
         ];
 
         $parameters = collect($parameters)->flatMap(function ($parameter, $key) {

+ 6 - 4
src/Repositories/EloquentRepository.php

@@ -168,7 +168,7 @@ class EloquentRepository extends Repository implements TreeRepository
         }
 
         $model->getQueries()->unique()->each(function ($value) use (&$query) {
-            if ($value['method'] === 'paginate') {
+            if ($value['method'] === 'paginate' || $value['method'] === 'simplePaginate') {
                 $value['arguments'][1] = $this->getGridColumns();
             } elseif ($value['method'] === 'get') {
                 $value['arguments'] = [$this->getGridColumns()];
@@ -370,14 +370,16 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     protected function setPaginate(Grid\Model $model)
     {
-        $paginate = $model->findQueryByMethod('paginate')->first();
+        $paginateMethod = $model->getPaginateMethod();
 
-        $model->rejectQuery(['paginate']);
+        $paginate = $model->findQueryByMethod($paginateMethod)->first();
+
+        $model->rejectQuery(['paginate', 'simplePaginate']);
 
         if (! $model->allowPagination()) {
             $model->addQuery('get', [$this->getGridColumns()]);
         } else {
-            $model->addQuery('paginate', $this->resolvePerPage($model, $paginate));
+            $model->addQuery($paginateMethod, $this->resolvePerPage($model, $paginate));
         }
     }