jqh 4 роки тому
батько
коміт
93783d1518

+ 47 - 0
resources/views/grid/async-table.blade.php

@@ -0,0 +1,47 @@
+
+<div class="dcat-box">
+
+    <div class="d-block pb-0">
+        @include('admin::grid.table-toolbar')
+    </div>
+
+    {!! $grid->renderFilter() !!}
+
+    {!! $grid->renderHeader() !!}
+
+    <div class="{!! $grid->formatTableParentClass() !!}">
+        <table class="{{ $grid->formatTableClass() }}" id="{{ $tableId }}" >
+            <thead>
+            @if ($headers = $grid->getVisibleComplexHeaders())
+                <tr>
+                    @foreach($headers as $header)
+                        {!! $header->render() !!}
+                    @endforeach
+                </tr>
+            @endif
+            <tr>
+                @foreach($grid->getVisibleColumns() as $column)
+                    <th {!! $column->formatTitleAttributes() !!}>{!! $column->getLabel() !!}{!! $column->renderHeader() !!}</th>
+                @endforeach
+            </tr>
+            </thead>
+
+            @if ($grid->hasQuickCreate())
+                {!! $grid->renderQuickCreate() !!}
+            @endif
+
+            <tbody class="async-tbody"></tbody>
+        </table>
+    </div>
+
+    {!! $grid->renderFooter() !!}
+</div>
+
+<script>
+Dcat.ready(function () {
+    // history.pushState({}, '', '/tests');
+    $.get('{!! $asyncUrl !!}', function () {
+
+    });
+});
+</script>

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

@@ -56,5 +56,3 @@
     {!! $grid->renderPagination() !!}
 
 </div>
-
-

+ 50 - 0
src/Grid.php

@@ -40,6 +40,7 @@ class Grid
 
     const CREATE_MODE_DEFAULT = 'default';
     const CREATE_MODE_DIALOG = 'dialog';
+    const ASYNC_NAME = '_async_';
 
     /**
      * The grid data model instance.
@@ -183,6 +184,11 @@ class Grid
      */
     protected $show = true;
 
+    /**
+     * @var bool
+     */
+    protected $async = false;
+
     /**
      * Create a new grid instance.
      *
@@ -272,6 +278,38 @@ class Grid
         return $this->addColumn('#', $label ?: '#');
     }
 
+    /**
+     * 启用异步渲染功能.
+     *
+     * @param bool $async
+     *
+     * @return $this
+     */
+    public function async(bool $async = true)
+    {
+        $this->async = $async;
+
+        if ($async) {
+            $this->view('admin::grid.async-table');
+            $this->addVariables([
+                'currentUrl' => $this->request->fullUrl(),
+                'asyncUrl'   => Helper::fullUrlWithoutQuery([static::ASYNC_NAME => 1]),
+            ]);
+        }
+
+        return $this;
+    }
+
+    /**
+     * 判断是否允许查询数据.
+     *
+     * @return bool
+     */
+    public function buildable()
+    {
+        return $this->async && $this->request->get(static::ASYNC_NAME);
+    }
+
     /**
      * Batch add column to grid.
      *
@@ -431,6 +469,18 @@ class Grid
      */
     public function build()
     {
+        if (! $this->buildable()) {
+            $this->callBuilder();
+            $this->handleExportRequest();
+
+            $this->prependRowSelectorColumn();
+            $this->appendActionsColumn();
+
+            $this->sortHeaders();
+
+            return;
+        }
+
         if ($this->built) {
             return;
         }