Browse Source

Update batch action

jqh 5 years ago
parent
commit
3a5af0e759

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

@@ -6,11 +6,7 @@
     </button>
     <ul class="dropdown-menu" role="menu">
         @foreach($actions as $action)
-            @if($action instanceof \Dcat\Admin\Grid\Tools\BatchDelete)
-                {!! $action->render() !!}
-            @else
-                <li><a href="#" class="{{ $action->getElementClass(false) }}">{{ $action->getTitle() }}</a></li>
-            @endif
+            {!! $action->render() !!}
         @endforeach
     </ul>
 </div>

+ 23 - 5
src/Grid/Tools/BatchAction.php

@@ -3,8 +3,9 @@
 namespace Dcat\Admin\Grid\Tools;
 
 use Dcat\Admin\Grid;
+use Illuminate\Contracts\Support\Renderable;
 
-abstract class BatchAction
+abstract class BatchAction implements Renderable
 {
     /**
      * @var int
@@ -72,18 +73,35 @@ abstract class BatchAction
      *
      * @return string
      */
-    public function getElementClass($dotPrefix = true)
+    public function getElementClass()
     {
         return sprintf(
-            '%s%s-%s',
-            $dotPrefix ? '.' : '',
+            '%s-%s',
             $this->grid->getGridBatchName(),
             $this->id
         );
     }
 
     /**
-     * @return mixed
+     * @return string
+     */
+    public function getElementSelector()
+    {
+        return '.'.$this->getElementClass();
+    }
+
+    /**
+     * Script of batch action.
+     *
+     * @return string
      */
     abstract public function script();
+
+    public function render()
+    {
+        return <<<HTML
+<li><a href="#" class="{{$this->getElementClass()}}}">{$this->getTitle()}</a></li>
+HTML;
+
+    }
 }

+ 3 - 10
src/Grid/Tools/BatchActions.php

@@ -52,22 +52,15 @@ class BatchActions extends AbstractTool
     /**
      * Add a batch action.
      *
-     * @param $title
-     * @param BatchAction|null $action
+     * @param BatchAction $action
      *
      * @return $this
      */
-    public function add($title, BatchAction $action = null)
+    public function add(BatchAction $action)
     {
         $id = $this->actions->count();
 
-        if (func_num_args() == 1) {
-            $action = $title;
-            $action->setId($id);
-        } elseif (func_num_args() == 2) {
-            $action->setId($id);
-            $action->setTitle($title);
-        }
+        $action->setId($id);
 
         $this->actions->push($action);