Ver código fonte

行选择器js代码优化

jqh 5 anos atrás
pai
commit
59567e8ae4

+ 23 - 5
resources/assets/dcat-admin/main.js

@@ -33,6 +33,25 @@ window.require = window.define = window.exports = window.module = undefined;
             setTimeout(NP.done, 200);
         };
 
+        LA.grid = {
+            _defaultName: '_def_',
+            _selectors: {},
+
+            addSelector: function (selector, name) {
+                this._selectors[name || this._defaultName] = selector;
+            },
+
+            // 获取行选择器选中的ID字符串
+            selected: function (name) {
+                return this._selectors[name || this._defaultName].getIds()
+            },
+
+            // 获取行选择器选中的行
+            selectedRows: function (name) {
+                return this._selectors[name || this._defaultName].getRows()
+            },
+        };
+
         $.pjax.defaults.timeout = 5000;
         $.pjax.defaults.maxCacheLength = 0;
 
@@ -77,8 +96,8 @@ window.require = window.define = window.exports = window.module = undefined;
         batchDeleteAction: function () {
             $('[data-action="batch-delete"]').off('click').on('click', function() {
                 var url = $(this).data('url'),
-                    method = $(this).data('method'),
-                    id = win[method]().join();
+                    name = $(this).data('name'),
+                    id = LA.grid.selected(name).join();
                 if (!id) {
                     return;
                 }
@@ -437,7 +456,6 @@ window.require = window.define = window.exports = window.module = undefined;
                 checkbox: '', // checkbox css选择器
                 selectAll: '', // 全选checkbox css选择器
                 bg: 'rgba(255, 255,213,0.4)', // 选中效果颜色
-                getSelectedRowsMethod: 'getSelectRows',
                 clickTr: false, // 点击行事件
             }, opts);
 
@@ -478,7 +496,7 @@ window.require = window.define = window.exports = window.module = undefined;
                 }
             });
 
-            this.getIds = window[opts.getSelectedRowsMethod] = function () {
+            this.getIds = function () {
                 var selected = [];
                 $(checkboxSelector+':checked').each(function() {
                     selected.push($(this).data('id'));
@@ -486,7 +504,7 @@ window.require = window.define = window.exports = window.module = undefined;
 
                 return selected;
             };
-            this.getItems = window[opts.getSelectedRowsMethod + 'Options'] = function () {
+            this.getRows = function () {
                 var selected = [];
                 $(checkboxSelector+':checked').each(function(){
                     selected.push({'id': $(this).data('id'), 'label': $(this).data('label')})

Diferenças do arquivo suprimidas por serem muito extensas
+ 0 - 0
resources/assets/dcat-admin/main.min.js


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

@@ -8,7 +8,6 @@ LA.RowSelector = function RowSelector(opts) {
         checkbox: '', // checkbox css选择器
         selectAll: '', // 全选checkbox css选择器
         bg: 'rgba(255, 255,213,0.4)', // 选中效果颜色
-        getSelectedRowsMethod: 'getSelectRows',
         clickTr: false, // 点击行事件
     }, opts);
 
@@ -49,7 +48,7 @@ LA.RowSelector = function RowSelector(opts) {
         }
     });
 
-    this.getIds = window[opts.getSelectedRowsMethod] = function () {
+    this.getIds = function () {
         var selected = [];
         $(checkboxSelector+':checked').each(function() {
             selected.push($(this).data('id'));
@@ -57,7 +56,7 @@ LA.RowSelector = function RowSelector(opts) {
 
         return selected;
     };
-    this.getItems = window[opts.getSelectedRowsMethod + 'Options'] = function () {
+    this.getRows = function () {
         var selected = [];
         $(checkboxSelector+':checked').each(function(){
             selected.push({'id': $(this).data('id'), 'label': $(this).data('label')})

+ 0 - 15
src/Grid/Concerns/HasElementNames.php

@@ -27,7 +27,6 @@ trait HasElementNames
         'grid_per_page'   => 'grid-per-pager',
         'grid_batch'      => 'grid-batch',
         'export_selected' => 'export-selected',
-        'selected_rows'   => 'selectedRows',
     ];
 
     /**
@@ -106,20 +105,6 @@ trait HasElementNames
         return $this->elementNameWithPrefix('export_selected');
     }
 
-    /**
-     * @return string
-     */
-    public function selectedRowsName()
-    {
-        $elementName = $this->elementNames['selected_rows'];
-
-        if ($this->__name) {
-            return sprintf('%s%s', $this->__name, ucfirst($elementName));
-        }
-
-        return $elementName;
-    }
-
     /**
      * @return string
      */

+ 2 - 2
src/Grid/Displayers/RowSelector.php

@@ -29,13 +29,13 @@ EOT;
 
         Admin::script(
             <<<JS
-LA.RowSelector({
+var selector = LA.RowSelector({
     checkbox: '.{$this->grid->rowName()}-checkbox',
     selectAll: '.{$this->grid->selectAllName()}', 
-    getSelectedRowsMethod: '{$this->grid->selectedRowsName()}',
     clickTr: {$clickTr},
     bg: '{$background}',
 });
+LA.grid.addSelector(selector, '{$this->grid->getName()}');
 JS
         );
     }

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

@@ -12,7 +12,7 @@ class BatchDelete extends BatchAction
     public function render()
     {
         return <<<HTML
-<li><a href="#" data-method="{$this->grid->selectedRowsName()}" data-action="batch-delete" data-url="{$this->resource}">{$this->title}</a></li>
+<li><a href="#" data-name="{$this->grid->getName()}" data-action="batch-delete" data-url="{$this->resource}">{$this->title}</a></li>
 HTML;
     }
 

+ 2 - 2
src/Grid/Tools/ExportButton.php

@@ -31,8 +31,8 @@ class ExportButton extends AbstractTool
 $('.{$this->grid->exportSelectedName()}').click(function (e) {
     e.preventDefault();
     
-    var rows = {$this->grid->selectedRowsName()}().join(',');
-    if (!rows) {
+    var rows = LA.grid.selected('{$this->grid->getName()}').join(',');
+    if (! rows) {
         return false;
     }
     

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff