Parcourir la source

非选中状态下隐藏批量操作按钮

jqh il y a 5 ans
Parent
commit
f25175f373

Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
resources/assets/dcat-admin/main.js


Fichier diff supprimé car celui-ci est trop grand
+ 0 - 0
resources/assets/dcat-admin/main.min.js


+ 14 - 3
resources/assets/dcat-admin/row-selector.js

@@ -51,15 +51,26 @@ LA.RowSelector = function RowSelector(opts) {
     this.getIds = function () {
         var selected = [];
         $(checkboxSelector+':checked').each(function() {
-            selected.push($(this).data('id'));
+            var id = $(this).data('id');
+            if (selected.indexOf(id) === -1) {
+                selected.push(id);
+            }
         });
 
         return selected;
     };
     this.getRows = function () {
         var selected = [];
-        $(checkboxSelector+':checked').each(function(){
-            selected.push({'id': $(this).data('id'), 'label': $(this).data('label')})
+        $(checkboxSelector+':checked').each(function() {
+            var id = $(this).data('id'), i, exist;
+
+            for (i in selected) {
+                if (selected[i].id === id) {
+                    exist = true
+                }
+            }
+
+            exist || selected.push({'id': id, 'label': $(this).data('label')})
         });
 
         return selected;

+ 1 - 0
resources/lang/en/admin.php

@@ -186,6 +186,7 @@ return [
     'import_extension_confirm' => 'Are you sure import the extension?',
     'selected_must_less_then'  => 'Only supports maximum :num options.',
     'quick_create'             => 'Quick create',
+    'grid_items_selected'      => '{n} items selected',
     'validation'               => [
         'match'     => 'The :attribute and :other must match.',
         'minlength' => 'The :attribute must be at least :min characters.',

+ 1 - 0
resources/lang/zh-CN/admin.php

@@ -187,6 +187,7 @@ return [
     'import_extension_confirm' => '确认导入拓展?',
     'selected_must_less_then'  => '最多只能选择:num个选项',
     'quick_create'             => '快速创建',
+    'grid_items_selected'      => '已选择 {n} 项',
     'validation'               => [
         'match'     => '与 :attribute 不匹配。',
         'minlength' => ':attribute 字符长度不能少于 :min。',

+ 5 - 3
resources/views/grid/batch-actions.blade.php

@@ -1,6 +1,7 @@
-<div class="btn-group default" style="margin-right:3px">
+@if(! $isHoldSelectAllCheckbox)
+<div class="btn-group default {{$selectAllName}}-btn" style="display:none;margin-right: 3px;">
     <button type="button" class="btn btn-sm btn-default dropdown-toggle btn-mini" data-toggle="dropdown">
-        <span class="hidden-xs">{{ trans('admin.action') }}&nbsp;</span>
+        <span class="hidden-xs selected"></span>
         <span class="caret"></span>
         <span class="sr-only"></span>
     </button>
@@ -9,4 +10,5 @@
             {!! $action->render() !!}
         @endforeach
     </ul>
-</div>
+</div>
+@endif

+ 50 - 1
src/Grid/Tools/BatchActions.php

@@ -2,6 +2,7 @@
 
 namespace Dcat\Admin\Grid\Tools;
 
+use Dcat\Admin\Admin;
 use Dcat\Admin\Grid\BatchAction;
 use Illuminate\Support\Collection;
 
@@ -17,6 +18,11 @@ class BatchActions extends AbstractTool
      */
     protected $enableDelete = true;
 
+    /**
+     * @var bool
+     */
+    protected $isHoldSelectAllCheckbox = false;
+
     /**
      * BatchActions constructor.
      */
@@ -49,6 +55,20 @@ class BatchActions extends AbstractTool
         return $this;
     }
 
+    /**
+     * Disable delete And Hode SelectAll Checkbox.
+     *
+     * @return $this
+     */
+    public function disableDeleteAndHodeSelectAll()
+    {
+        $this->enableDelete = false;
+
+        $this->isHoldSelectAllCheckbox = true;
+
+        return $this;
+    }
+
     /**
      * Add a batch action.
      *
@@ -77,6 +97,32 @@ class BatchActions extends AbstractTool
         }
     }
 
+    /**
+     * Scripts of BatchActions button groups.
+     */
+    protected function setupScript()
+    {
+        $name = $this->parent->getName();
+        $allName = $this->parent->selectAllName();
+        $rowName = $this->parent->rowName();
+
+        $selected = trans('admin.grid_items_selected');
+
+        $script = <<<JS
+$('.{$rowName}-checkbox').on('change', function () {
+    var btn = $('.{$allName}-btn');
+    if (this.checked) {
+        btn.show()
+    } else {
+        btn.hide()
+    }
+    btn.find('.selected').html("{$selected}".replace('{n}', LA.grid.selectedRows('$name').length));
+});
+JS;
+
+        Admin::script($script);
+    }
+
     /**
      * Render BatchActions button groups.
      *
@@ -92,10 +138,13 @@ class BatchActions extends AbstractTool
             return '';
         }
 
+        $this->setupScript();
         $this->prepareActions();
 
         $data = [
-            'actions' => $this->actions,
+            'actions'                 => $this->actions,
+            'selectAllName'           => $this->parent->selectAllName(),
+            'isHoldSelectAllCheckbox' => $this->isHoldSelectAllCheckbox,
         ];
 
         return view('admin::grid.batch-actions', $data)->render();

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff