Browse Source

快捷搜索表单增加auto方法

jqh 5 years ago
parent
commit
f18b88fbb5

+ 1 - 1
resources/views/grid/quick-search.blade.php

@@ -9,7 +9,7 @@
                     placeholder="{{ $placeholder }}"
                     name="{{ $key }}"
                     value="{{ $value }}"
-                    aria-controls="DataTables_Table_0"
+                    auto="{{ $auto ? '1' : '0' }}"
             >
 
         </label>

+ 2 - 2
src/Controllers/UserController.php

@@ -2,7 +2,6 @@
 
 namespace Dcat\Admin\Controllers;
 
-use App\Admin\Actions\Copy;
 use Dcat\Admin\Auth\Permission;
 use Dcat\Admin\Form;
 use Dcat\Admin\Grid;
@@ -60,10 +59,11 @@ class UserController extends AdminController
             $grid->created_at;
             $grid->updated_at->sortable();
 
+            $grid->quickSearch(['id', 'name', 'username']);
+
             $grid->disableBatchDelete();
             $grid->showQuickEditButton();
             $grid->disableFilterButton();
-            $grid->quickSearch(['id', 'name', 'username']);
             $grid->enableDialogCreate();
 
             $grid->actions(function (Grid\Displayers\Actions $actions) {

+ 29 - 6
src/Grid/Tools/QuickSearch.php

@@ -28,6 +28,11 @@ class QuickSearch extends AbstractTool
      */
     protected $width = 19;
 
+    /**
+     * @var bool
+     */
+    protected $autoSubmit = true;
+
     /**
      * @param string|null $name
      *
@@ -101,6 +106,18 @@ class QuickSearch extends AbstractTool
         ]);
     }
 
+    /**
+     * @param bool $value
+     *
+     * @return $this
+     */
+    public function auto(bool $value = true)
+    {
+        $this->autoSubmit = $value;
+
+        return $this;
+    }
+
     /**
      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
      */
@@ -114,6 +131,7 @@ class QuickSearch extends AbstractTool
             'value'       => $this->value(),
             'placeholder' => $this->placeholder ?: trans('admin.search'),
             'width'       => $this->width,
+            'auto'        => $this->autoSubmit,
         ];
 
         return view($this->view, $data);
@@ -126,7 +144,8 @@ class QuickSearch extends AbstractTool
     var inputting = false,
         $ipt = $('input.quick-search-input'), 
         val = $ipt.val(),
-        ignoreKeys = [16, 17, 18, 20, 35, 36, 37, 38, 39, 40, 45, 144];
+        ignoreKeys = [16, 17, 18, 20, 35, 36, 37, 38, 39, 40, 45, 144],
+        auto = $ipt.attr('auto');
     
     var submit = Dcat.helpers.debounce(function (input) {
         inputting || $(input).parents('form').submit()
@@ -145,11 +164,6 @@ class QuickSearch extends AbstractTool
     }
     
     $ipt.on('focus', toggleBtn)
-        .on('keyup', function (e) {
-            toggleBtn.apply(this);
-            
-            ignoreKeys.indexOf(e.keyCode) == -1 && submit(this)
-        })
         .on('mousemove', toggleBtn)
         .on('mouseout', toggleBtn)
         .on('compositionstart', function(){
@@ -158,6 +172,15 @@ class QuickSearch extends AbstractTool
         .on('compositionend', function() {
             inputting = false
         });
+
+    if (auto > 0) {
+        $ipt.on('keyup', function (e) {
+            toggleBtn.apply(this);
+            
+            ignoreKeys.indexOf(e.keyCode) == -1 && submit(this)
+        })
+    }
+    
     val !== '' && $ipt.val('').focus().val(val);
     
     $('.quick-search-clear').on('click', function () {