Browse Source

Filter selectTable

jqh 4 years ago
parent
commit
af8a60d328

+ 4 - 0
resources/assets/dcat/extra/select-table.js

@@ -163,6 +163,10 @@
                 placeholder.removeClass('d-none');
                 placeholder.removeClass('d-none');
                 option.addClass('d-none');
                 option.addClass('d-none');
 
 
+                if (options.multiple) {
+                    box.addClass('form-control');
+                }
+
                 return;
                 return;
             }
             }
 
 

File diff suppressed because it is too large
+ 0 - 0
resources/dist/adminlte/adminlte.js.map


File diff suppressed because it is too large
+ 0 - 0
resources/dist/dcat/extra/action.js.map


File diff suppressed because it is too large
+ 0 - 0
resources/dist/dcat/extra/resource-selector.js.map


File diff suppressed because it is too large
+ 0 - 0
resources/dist/dcat/extra/select-table.js


File diff suppressed because it is too large
+ 0 - 0
resources/dist/dcat/extra/select-table.js.map


File diff suppressed because it is too large
+ 0 - 0
resources/dist/dcat/js/dcat-app.js.map


+ 19 - 0
resources/views/filter/selecttable.blade.php

@@ -0,0 +1,19 @@
+<div class="input-group input-group-sm">
+    <div class="input-group-prepend">
+        <span class="input-group-text bg-white"><b>{!! $label !!}</b></span>
+    </div>
+
+    <div id="{{ $id }}" class="form-control">
+        <span class="default-text" style="opacity:0.75">{{ $placeholder }}</span>
+        <span class="option d-none"></span>
+    </div>
+
+    <input name="{{ $name }}" type="hidden" id="hidden-{{ $id }}" value="{{ implode(',', \Dcat\Admin\Support\Helper::array($value)) }}" />
+    <div class="input-group-append">
+        <div class="btn btn-primary btn-sm" data-toggle="modal" data-target="#modal-{{ $id }}">
+            &nbsp;<i class="feather icon-arrow-up"></i>&nbsp;
+        </div>
+    </div>
+
+    {!! $modal !!}
+</div>

+ 4 - 10
src/Controllers/LogController.php

@@ -70,16 +70,10 @@ class LogController extends Controller
             $grid->setActionClass(Grid\Displayers\Actions::class);
             $grid->setActionClass(Grid\Displayers\Actions::class);
 
 
             $grid->filter(function (Grid\Filter $filter) {
             $grid->filter(function (Grid\Filter $filter) {
-                $filter->equal('user_id', trans('admin.user'))
-                    ->selectResource('auth/users')
-                    ->options(function ($v) {
-                        if (! $v) {
-                            return $v;
-                        }
-                        $userModel = config('admin.database.users_model');
-
-                        return $userModel::find((array) $v)->pluck('name', 'id');
-                    });
+                $userModel = config('admin.database.users_model');
+
+                $filter->in('user_id', trans('admin.user'))
+                    ->multipleSelect($userModel::pluck('name', 'id'));
 
 
                 $filter->equal('method', trans('admin.method'))
                 $filter->equal('method', trans('admin.method'))
                     ->select(
                     ->select(

+ 4 - 9
src/Form/Field/MultipleSelectTable.php

@@ -2,10 +2,12 @@
 
 
 namespace Dcat\Admin\Form\Field;
 namespace Dcat\Admin\Form\Field;
 
 
-use Dcat\Admin\Admin;
-
 class MultipleSelectTable extends SelectTable
 class MultipleSelectTable extends SelectTable
 {
 {
+    public static $css = [
+        '@select2',
+    ];
+
     protected $view = 'admin::form.selecttable';
     protected $view = 'admin::form.selecttable';
 
 
     /**
     /**
@@ -27,13 +29,6 @@ class MultipleSelectTable extends SelectTable
         return $this;
         return $this;
     }
     }
 
 
-    public function render()
-    {
-        Admin::css('@select2');
-
-        return parent::render();
-    }
-
     protected function addScript()
     protected function addScript()
     {
     {
         $this->script .= <<<JS
         $this->script .= <<<JS

+ 31 - 0
src/Grid/Filter/AbstractFilter.php

@@ -10,6 +10,7 @@ use Dcat\Admin\Grid\Filter\Presenter\Presenter;
 use Dcat\Admin\Grid\Filter\Presenter\Radio;
 use Dcat\Admin\Grid\Filter\Presenter\Radio;
 use Dcat\Admin\Grid\Filter\Presenter\Select;
 use Dcat\Admin\Grid\Filter\Presenter\Select;
 use Dcat\Admin\Grid\Filter\Presenter\Text;
 use Dcat\Admin\Grid\Filter\Presenter\Text;
+use Dcat\Admin\Grid\LazyRenderable;
 use Dcat\Laravel\Database\WhereHasInServiceProvider;
 use Dcat\Laravel\Database\WhereHasInServiceProvider;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Arr;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Collection;
@@ -299,12 +300,34 @@ abstract class AbstractFilter
      * @param mixed $source
      * @param mixed $source
      *
      *
      * @return Filter\Presenter\SelectResource
      * @return Filter\Presenter\SelectResource
+     *
+     * @deprecated 即将在2.0版本中废弃,请使用 selectTable 和 multipleSelectTable 代替
      */
      */
     public function selectResource($source = null)
     public function selectResource($source = null)
     {
     {
         return $this->setPresenter(new Filter\Presenter\SelectResource($source));
         return $this->setPresenter(new Filter\Presenter\SelectResource($source));
     }
     }
 
 
+    /**
+     * @param LazyRenderable $table
+     *
+     * @return mixed
+     */
+    public function selectTable(LazyRenderable $table)
+    {
+        return $this->setPresenter(new Filter\Presenter\SelectTable($table));
+    }
+
+    /**
+     * @param LazyRenderable $table
+     *
+     * @return mixed
+     */
+    public function multipleSelectTable(LazyRenderable $table)
+    {
+        return $this->setPresenter(new Filter\Presenter\MultipleSelectTable($table));
+    }
+
     /**
     /**
      * @param array $options
      * @param array $options
      *
      *
@@ -474,6 +497,14 @@ abstract class AbstractFilter
         return $parenName ? "{$parenName}_{$this->column}" : $this->column;
         return $parenName ? "{$parenName}_{$this->column}" : $this->column;
     }
     }
 
 
+    /**
+     * @return string
+     */
+    public function getLabel()
+    {
+        return $this->label;
+    }
+
     /**
     /**
      * Get value of current filter.
      * Get value of current filter.
      *
      *

+ 54 - 0
src/Grid/Filter/Presenter/MultipleSelectTable.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace Dcat\Admin\Grid\Filter\Presenter;
+
+use Dcat\Admin\Admin;
+use Dcat\Admin\Grid\LazyRenderable;
+use Dcat\Admin\Support\Helper;
+use Dcat\Admin\Widgets\TableModal;
+use Illuminate\Support\Str;
+
+class MultipleSelectTable extends SelectTable
+{
+    public static $css = [
+        '@select2',
+    ];
+
+    protected $view = 'admin::filter.selecttable';
+
+    /**
+     * @var int
+     */
+    protected $max = 0;
+
+    /**
+     * 设置最大选择数量.
+     *
+     * @param int $max
+     *
+     * @return $this
+     */
+    public function max(int $max)
+    {
+        $this->max = $max;
+
+        return $this;
+    }
+
+    protected function addScript()
+    {
+        Admin::script(
+            <<<JS
+Dcat.grid.SelectTable({
+    modal: replaceNestedFormIndex('#{$this->modal->id()}'),
+    container: replaceNestedFormIndex('#{$this->id}'),
+    input: replaceNestedFormIndex('#hidden-{$this->id}'),
+    button: replaceNestedFormIndex('#{$this->getButtonId()}'),
+    multiple: true,
+    max: {$this->max},
+    values: {$this->options},
+});
+JS
+        );
+    }
+}

+ 16 - 0
src/Grid/Filter/Presenter/Presenter.php

@@ -80,6 +80,22 @@ abstract class Presenter
         return $this;
         return $this;
     }
     }
 
 
+    /**
+     * Get filter value.
+     *
+     * @return array|string
+     */
+    public function value()
+    {
+        $value = $this->filter->getValue();
+
+        if ($value === null || $value === '') {
+            return $this->filter->getDefault();
+        }
+
+        return $value;
+    }
+
     /**
     /**
      * Blade template variables for this presenter.
      * Blade template variables for this presenter.
      *
      *

+ 181 - 0
src/Grid/Filter/Presenter/SelectTable.php

@@ -0,0 +1,181 @@
+<?php
+
+namespace Dcat\Admin\Grid\Filter\Presenter;
+
+use Dcat\Admin\Admin;
+use Dcat\Admin\Grid\LazyRenderable;
+use Dcat\Admin\Support\Helper;
+use Dcat\Admin\Widgets\TableModal;
+use Illuminate\Support\Str;
+
+class SelectTable extends Presenter
+{
+    public static $js = [
+        '@select-table',
+    ];
+
+    /**
+     * @var TableModal
+     */
+    protected $modal;
+
+    protected $title;
+
+    protected $id;
+
+    protected $options;
+
+    protected $placeholder;
+
+    public function __construct(LazyRenderable $table)
+    {
+        $this->modal = TableModal::make($table);
+        $this->id = 'select-table-filter-'.Str::random(8);
+    }
+
+    /**
+     * 设置选中的选项.
+     *
+     * @param \Closure $options
+     *
+     * @return $this
+     */
+    public function options(\Closure $options)
+    {
+        $this->options = $options;
+
+        return $this;
+    }
+
+    /**
+     * 设置尺寸为 xl.
+     *
+     * @return $this
+     */
+    public function xl()
+    {
+        $this->modal->xl();
+
+        return $this;
+    }
+
+    /**
+     * 设置弹窗标题.
+     *
+     * @param string $title
+     *
+     * @return $this
+     */
+    public function title($title)
+    {
+        $this->title = $title;
+
+        return $this;
+    }
+
+    /**
+     * @param string $placeholder
+     *
+     * @return $this|string
+     */
+    public function placeholder(string $placeholder = null)
+    {
+        if ($placeholder === null) {
+            return $this->placeholder ?: __('admin.choose');
+        }
+
+        $this->placeholder = $placeholder;
+
+        return $this;
+    }
+
+    protected function setUpModal()
+    {
+        $table = $this->modal->getTable();
+
+        $table->id('table-card-'.$this->id);
+
+        $this->modal
+            ->id('modal-'.$this->id)
+            ->title($this->title ?: $this->filter->getLabel())
+            ->footer($this->renderFooter());
+    }
+
+    protected function formatOptions()
+    {
+        $value = Helper::array($this->value());
+
+        if ($this->options instanceof \Closure) {
+            $this->options = call_user_func($this->options, $value, $this);
+        }
+
+        $values = [];
+
+        foreach (Helper::array($this->options) as $id => $label) {
+            foreach ($value as $v) {
+                if ($v == $id && $v !== null) {
+                    $values[] = ['id' => $v, 'label' => $label];
+                }
+            }
+        }
+
+        $this->options = json_encode($values);
+    }
+
+    protected function addScript()
+    {
+        Admin::script(
+            <<<JS
+Dcat.grid.SelectTable({
+    modal: replaceNestedFormIndex('#{$this->modal->id()}'),
+    container: replaceNestedFormIndex('#{$this->id}'),
+    input: replaceNestedFormIndex('#hidden-{$this->id}'),
+    button: replaceNestedFormIndex('#{$this->getButtonId()}'),
+    values: {$this->options},
+});
+JS
+        );
+    }
+
+    /**
+     * @return array
+     */
+    public function variables(): array
+    {
+        $this->formatOptions();
+        $this->setUpModal();
+        $this->addScript();
+
+        return [
+            'id'          => $this->id,
+            'modal'       => $this->modal,
+            'placeholder' => $this->placeholder(),
+        ];
+    }
+
+    /**
+     * 弹窗底部内容构建.
+     *
+     * @return string
+     */
+    protected function renderFooter()
+    {
+        $submit = trans('admin.submit');
+        $cancel = trans('admin.cancel');
+
+        return <<<HTML
+<a id="{$this->getButtonId()}" class="btn btn-primary" style="color: #fff">&nbsp;{$submit}&nbsp;</a>&nbsp;
+<a onclick="$(this).parents('.modal').modal('toggle')" class="btn btn-white">&nbsp;{$cancel}&nbsp;</a>
+HTML;
+    }
+
+    /**
+     * 提交按钮ID.
+     *
+     * @return string
+     */
+    public function getButtonId()
+    {
+        return 'submit-'.$this->id;
+    }
+}

+ 1 - 2
src/Grid/LazyRenderable.php

@@ -54,9 +54,8 @@ abstract class LazyRenderable extends Renderable
 
 
         if ($this->allowSimpleMode()) {
         if ($this->allowSimpleMode()) {
             $grid->disableCreateButton();
             $grid->disableCreateButton();
-            $grid->disableActions();
             $grid->disablePerPages();
             $grid->disablePerPages();
-            $grid->disableBatchActions();
+            $grid->disableBatchDelete();
             $grid->disableRefreshButton();
             $grid->disableRefreshButton();
 
 
             $grid->filter()
             $grid->filter()

+ 9 - 1
src/Grid/Tools/FilterButton.php

@@ -84,7 +84,15 @@ class FilterButton extends AbstractTool
         return false
         return false
     });
     });
     
     
-    $('.wrapper').on('click', function () {
+    $('.wrapper').on('click', '.modal', function (e) {
+        if (typeof e.cancelBubble != "undefined") {
+            e.cancelBubble = true;
+        }
+        if (typeof e.stopPropagation != "undefined") {
+            e.stopPropagation();
+        }
+    });
+    $(document).on('click', '.wrapper', function (e) {
         if (slider && slider.close) {
         if (slider && slider.close) {
             slider.close();
             slider.close();
         }
         }

+ 9 - 4
src/Widgets/TableModal.php

@@ -59,16 +59,21 @@ class TableModal extends Widget
      * TableModal constructor.
      * TableModal constructor.
      *
      *
      * @param null $title
      * @param null $title
-     * @param \Dcat\Admin\Grid\LazyRenderable|null $renderable
+     * @param \Dcat\Admin\Grid\LazyRenderable|null $table
      */
      */
-    public function __construct($title = null, LazyRenderable $renderable = null)
+    public function __construct($title = null, LazyRenderable $table = null)
     {
     {
         $this->modal = Modal::make()
         $this->modal = Modal::make()
             ->lg()
             ->lg()
-            ->title($title)
             ->class('grid-modal', true);
             ->class('grid-modal', true);
 
 
-        $this->body($renderable);
+        if ($title instanceof LazyRenderable) {
+            $table = $title;
+            $title = null;
+        }
+
+        $this->title($title);
+        $this->body($table);
     }
     }
 
 
     /**
     /**

Some files were not shown because too many files changed in this diff