Prechádzať zdrojové kódy

表格新增模式调整

jqh 5 rokov pred
rodič
commit
9a5d63ced8

+ 2 - 2
resources/views/grid/quick-create/form.blade.php

@@ -1,5 +1,5 @@
 <thead>
-<tr class="quick-create">
+<tr class="{{ $elementClass }}" @if($hidden) style="display: none" @endif>
     <td colspan="{{ $columnCount }}" style="height: 43px;padding-left: 57px;background-color: #f9f9f9; vertical-align: middle;">
 
         <span class="create" style="color: #bdbdbd;cursor: pointer;display: block;">
@@ -11,7 +11,7 @@
                 &nbsp;{!! $field->render() !!}
             @endforeach
                 &nbsp;
-            <button class="btn btn-primary btn-sm">{{ __('admin.submit') }}</button>&nbsp;
+            <button type="submit" class="btn btn-primary btn-sm">{{ __('admin.submit') }}</button>&nbsp;
             <a href="javascript:void(0);" class="cancel">{{ __('admin.cancel') }}</a>
             {{ csrf_field() }}
         </form>

+ 0 - 1
src/Controllers/ExtensionController.php

@@ -82,7 +82,6 @@ class ExtensionController extends Controller
 
         $grid->disablePagination();
         $grid->disableCreateButton();
-        $grid->disableQuickCreateButton();
         $grid->disableDeleteButton();
         $grid->disableBatchDelete();
         $grid->disableFilterButton();

+ 0 - 1
src/Controllers/LogController.php

@@ -60,7 +60,6 @@ class LogController extends Controller
 
         $grid->disableCreateButton();
         $grid->disableQuickEditButton();
-        $grid->disableQuickCreateButton();
         $grid->disableEditButton();
         $grid->disableViewButton();
 

+ 1 - 2
src/Controllers/PermissionController.php

@@ -213,10 +213,9 @@ class PermissionController extends Controller
         $grid->created_at;
         $grid->updated_at->sortable();
 
-        $grid->disableCreateButton();
-        $grid->showQuickCreateButton();
         $grid->disableEditButton();
         $grid->showQuickEditButton();
+        $grid->createMode(Grid::CREATE_MODE_DIALOG);
 
         $grid->tools(function (Grid\Tools $tools) {
             $tools->batch(function (Grid\Tools\BatchActions $actions) {

+ 1 - 2
src/Controllers/RoleController.php

@@ -105,12 +105,11 @@ class RoleController extends Controller
         }
 
         $grid->disableBatchDelete();
-        $grid->disableCreateButton();
-        $grid->showQuickCreateButton();
         $grid->disableEditButton();
         $grid->showQuickEditButton();
         $grid->disableFilterButton();
         $grid->quickSearch(['id', 'name', 'slug']);
+        $grid->createMode(Grid::CREATE_MODE_DIALOG);
 
         $grid->actions(function (Grid\Displayers\Actions $actions) {
             $roleModel = config('admin.database.roles_model');

+ 0 - 1
src/Controllers/RouteController.php

@@ -61,7 +61,6 @@ class RouteController extends Controller
             $grid->disableActions();
             $grid->disableCreateButton();
             $grid->disableExporter();
-            $grid->disableQuickCreateButton();
 
             $grid->filter(function (Grid\Filter $filter) use ($colors) {
                 $values = array_keys($colors);

+ 1 - 2
src/Controllers/UserController.php

@@ -118,11 +118,10 @@ class UserController extends Controller
             $grid->updated_at->sortable();
 
             $grid->disableBatchDelete();
-            $grid->disableCreateButton();
-            $grid->showQuickCreateButton();
             $grid->showQuickEditButton();
             $grid->disableFilterButton();
             $grid->quickSearch(['id', 'name', 'username']);
+            $grid->createMode(Grid::CREATE_MODE_DIALOG);
 
             $grid->actions(function (Grid\Displayers\Actions $actions) {
                 if ($actions->key() == AdministratorModel::DEFAULT_ID) {

+ 2 - 33
src/Grid.php

@@ -38,7 +38,7 @@ class Grid
         }
 
     const CREATE_MODE_DEFAULT = 'default';
-    const CREATE_MODE_MODAL = 'modal';
+    const CREATE_MODE_DIALOG = 'dialog';
     const CREATE_MODE_QUICK = 'quick';
 
     /**
@@ -163,7 +163,6 @@ class Grid
         'show_delete_button'       => true,
         'show_row_selector'        => true,
         'show_create_button'       => true,
-        'show_quick_create_button' => false,
         'show_bordered'            => false,
         'show_toolbar'             => true,
         'show_exporter'            => false,
@@ -519,7 +518,7 @@ HTML
      */
     public function renderCreateButton()
     {
-        if (! $this->options['show_create_button'] && ! $this->options['show_quick_create_button']) {
+        if (! $this->options['show_create_button']) {
             return '';
         }
 
@@ -693,26 +692,6 @@ HTML;
         return $this->disableCreateButton(! $val);
     }
 
-    /**
-     * @param bool $disable
-     *
-     * @return $this
-     */
-    public function disableQuickCreateButton(bool $disable = true)
-    {
-        return $this->option('show_quick_create_button', ! $disable);
-    }
-
-    /**
-     * @param bool $val
-     *
-     * @return $this
-     */
-    public function showQuickCreateButton(bool $val = true)
-    {
-        return $this->disableQuickCreateButton(! $val);
-    }
-
     /**
      * If allow creation.
      *
@@ -723,16 +702,6 @@ HTML;
         return $this->options['show_create_button'];
     }
 
-    /**
-     * If grid show quick create button.
-     *
-     * @return bool
-     */
-    public function allowQuickCreateButton()
-    {
-        return $this->options['show_quick_create_button'];
-    }
-
     /**
      * @param string $mode
      *

+ 0 - 1
src/Grid/Concerns/HasTools.php

@@ -195,7 +195,6 @@ trait HasTools
                 $this->tools()->has()
                 || $this->allowExporter()
                 || $this->allowCreateButton()
-                || $this->allowQuickCreateButton()
                 || $this->allowResponsive()
                 || ! empty($this->variables['title'])
             )

+ 19 - 11
src/Grid/Tools/CreateButton.php

@@ -13,14 +13,17 @@ class CreateButton implements Renderable
      */
     protected $grid;
 
+    protected $mode;
+
     public function __construct(Grid $grid)
     {
         $this->grid = $grid;
+        $this->mode = $grid->option('create_mode');
     }
 
-    protected function renderQuickCreateButton()
+    protected function renderDialogCreateButton()
     {
-        if (! $this->grid->option('show_quick_create_button')) {
+        if ($this->mode !== Grid::CREATE_MODE_DIALOG) {
             return;
         }
 
@@ -30,19 +33,28 @@ class CreateButton implements Renderable
         [$width, $height] = $this->grid->option('dialog_form_area');
 
         Form::modal($new)
-            ->click(".{$this->grid->rowName()}-create")
+            ->click(".{$this->grid->getName()}-create")
             ->success('LA.reload()')
             ->dimensions($width, $height)
             ->render();
 
-        $text = $this->grid->option('show_create_button') ? '<i class="fa fa-clone"></i>' : "<i class='ti-plus'></i><span class='hidden-xs'> &nbsp; $new</span>";
+        return "<a data-url='$url' class='btn btn-sm btn-success {$this->grid->getName()}-create'><i class='ti-plus'></i><span class='hidden-xs'>&nbsp; $new</span></a>";
+    }
 
-        return "<a data-url='$url' class='btn btn-sm btn-success {$this->grid->rowName()}-create'>$text</a>";
+    protected function renderQuickCreateButton()
+    {
+        if ($this->mode !== Grid::CREATE_MODE_QUICK) {
+            return;
+        }
+
+        $new = trans('admin.new');
+
+        return "<a class='btn btn-sm btn-success {$this->grid->getName()}-quick-create-button'><i class='ti-plus'></i><span class='hidden-xs'>&nbsp; $new</span></a>";
     }
 
     protected function renderCreateButton()
     {
-        if (! $this->grid->option('show_create_button')) {
+        if ($this->mode && $this->mode !== Grid::CREATE_MODE_DEFAULT) {
             return;
         }
 
@@ -56,10 +68,6 @@ class CreateButton implements Renderable
 
     public function render()
     {
-        if (! $this->grid->option('show_create_button') && ! $this->grid->option('show_quick_create_button')) {
-            return;
-        }
-
-        return "<div class='btn-group' style='margin-right:3px'>{$this->renderCreateButton()}{$this->renderQuickCreateButton()}</div>";
+        return "<div class='btn-group' style='margin-right:3px'>{$this->renderCreateButton()}{$this->renderQuickCreateButton()}{$this->renderDialogCreateButton()}</div>";
     }
 }

+ 38 - 12
src/Grid/Tools/QuickCreate.php

@@ -254,27 +254,49 @@ CSS
     {
         $url = request()->url();
 
-        $script = <<<JS
+        $uniqueName = $this->parent->getName();
 
+        $script = <<<JS
 (function () {
-    $('.quick-create .create').click(function () {
-        $('.quick-create .create-form').show();
-        $(this).hide();
+    var ctr = $('.{$uniqueName}-quick-create'),
+        btn = $('.{$uniqueName}-quick-create-button');
+    
+    btn.click(function () {
+        ctr.toggle().click();
     });
     
-    $('.quick-create .cancel').click(function () {
-        $('.quick-create .create-form').hide();
-        $('.quick-create .create').show();
+    ctr.click(function () {
+        ctr.find('.create-form').show();
+        ctr.find('.create').hide();
     });
     
-    $('.quick-create .create-form').submit(function (e) {
+    ctr.find('.cancel').click(function () {
+        if (btn.length) {
+            ctr.hide();
+            return;
+        }
+        
+        ctr.find('.create-form').hide();
+        ctr.find('.create').show();
+        return false;
+    });
+
+    ctr.find('.create-form').submit(function (e) {
         e.preventDefault();
+        if (ctr.attr('working')) {
+            return;
+        }
+        
+        ctr.attr('working', 1);
+        LA.NP.start();
     
         $.ajax({
             url: '{$url}',
             type: 'POST',
             data: $(this).serialize(),
-            success: function(data, textStatus, jqXHR) {
+            success: function(data) {
+                LA.NP.done();
+                ctr.attr('working', '');
                 console.info(data);
                 
                 if (data.status == true) {
@@ -287,7 +309,9 @@ CSS
                     LA.warning(data.message)
                 }
             },
-            error:function(xhq, textStatus){
+            error:function(xhq){
+                LA.NP.done();
+                ctr.attr('working', '');
                 var json = xhq.responseJSON;
                 if (typeof json === 'object') {
                     if (json.message) {
@@ -326,8 +350,10 @@ JS;
         $this->script();
 
         $vars = [
-            'columnCount' => $columnCount,
-            'fields'      => $this->fields,
+            'columnCount'  => $columnCount,
+            'fields'       => $this->fields,
+            'elementClass' => $this->parent->getName().'-quick-create',
+            'hidden'       => $this->parent->option('create_mode') === Grid::CREATE_MODE_QUICK,
         ];
 
         return view('admin::grid.quick-create.form', $vars)->render();

+ 0 - 1
src/SimpleGrid.php

@@ -16,7 +16,6 @@ class SimpleGrid extends Grid
         $this->disableCreateButton();
         $this->disableActions();
         $this->disableExporter();
-        $this->disableQuickCreateButton();
         $this->disablePerPages();
 
         $this->option('row_selector_clicktr', true);

+ 0 - 1
tests/Controllers/ReportController.php

@@ -24,7 +24,6 @@ class ReportController extends Controller
         $grid->disableActions();
         $grid->disableBatchDelete();
         $grid->disableCreateButton();
-        $grid->disableQuickCreateButton();
 
         $grid->setRowSelectorOptions(['style' => 'success', 'clicktr' => true]);