Bläddra i källkod

优化SelectTable设置显示字段功能

jqh 4 år sedan
förälder
incheckning
c0bca3b107

+ 9 - 6
resources/assets/dcat/extra/select-table.js

@@ -80,8 +80,9 @@
                 self.resetSelected();
 
                 checkbox.on('change', function () {
-                    let id = $(this).data('id'),
-                        label = $(this).data('label');
+                    let $this = $(this),
+                        id = $this.data('id'),
+                        label = $this.data('label');
 
                     if (this.checked) {
                         if (! options.multiple) {
@@ -91,7 +92,7 @@
 
                         // 多选
                         if (options.max && (self.getSelectedRows()[0].length > options.max)) {
-                            $(this).prop('checked', false);
+                            $this.prop('checked', false);
                             delete self.selected[id];
 
                             return Dcat.warning(self.options.lang.exceed_max_item);
@@ -104,9 +105,11 @@
                         if (this.checked) {
                             // 单选效果
                             checkbox.each(function () {
-                                if ($(this).data('id') != id) {
-                                    $(this).prop('checked', false);
-                                    $(this).parents('tr').css('background-color', '');
+                                let $this = $(this);
+
+                                if ($this.data('id') != id) {
+                                    $this.prop('checked', false);
+                                    $this.parents('tr').css('background-color', '');
                                 }
                             });
                         }

+ 18 - 2
src/Admin.php

@@ -30,7 +30,7 @@ class Admin
     use HasAssets;
     use HasHtml;
 
-    const VERSION = '2.0.19-beta';
+    const VERSION = '2.0.20-beta';
 
     const SECTION = [
         // 往 <head> 标签内输入内容
@@ -158,15 +158,31 @@ class Admin
         return $navbar;
     }
 
+    /**
+     * 启用或禁用Pjax
+     *
+     * @param bool $value
+     *
+     * @return void
+     */
+    public function pjax(bool $value = true)
+    {
+        static::context()->pjaxContainerId = $value ? static::$defaultPjaxContainerId : false;
+    }
+
     /**
      * 禁用pjax.
+     *
+     * @return void
      */
     public static function disablePjax()
     {
-        static::context()->pjaxContainerId = false;
+        static::pjax(false);
     }
 
     /**
+     * 获取pjax ID.
+     *
      * @return string|void
      */
     public static function getPjaxContainerId()

+ 29 - 1
src/Form/Field/SelectTable.php

@@ -18,6 +18,10 @@ class SelectTable extends Field
 
     protected $style = 'primary';
 
+    protected $visibleColumn;
+
+    protected $key;
+
     public function __construct($column, $arguments = [])
     {
         parent::__construct($column, $arguments);
@@ -71,6 +75,22 @@ class SelectTable extends Field
         return $this;
     }
 
+    /**
+     * 设置选中的key以及标题字段.
+     *
+     * @param $visibleColumn
+     * @param $key
+     *
+     * @return $this
+     */
+    public function pluck(?string $visibleColumn, ?string $key = 'id')
+    {
+        $this->visibleColumn = $visibleColumn;
+        $this->key = $key;
+
+        return $this;
+    }
+
     /**
      * @param array $options
      *
@@ -94,7 +114,7 @@ class SelectTable extends Field
      */
     public function model(string $model, string $id = 'id', string $text = 'title')
     {
-        return $this->options(function ($v) use ($model, $id, $text) {
+        return $this->pluck($text, $id)->options(function ($v) use ($model, $id, $text) {
             if (! $v) {
                 return [];
             }
@@ -129,6 +149,14 @@ class SelectTable extends Field
         $this->dialog
             ->footer($this->renderFooter())
             ->button($this->renderButton());
+
+        // 设置选中的字段和待显示的标题字段
+        $this->dialog
+            ->getTable()
+            ->getRenderable()
+            ->payload([
+                LazyRenderable::ROW_SELECTOR_COLUMN_NAME => [$this->key, $this->visibleColumn],
+            ]);
     }
 
     public function render()

+ 1 - 1
src/Form/Field/Tags.php

@@ -57,7 +57,7 @@ class Tags extends Field
      *
      * @return $this
      */
-    public function pluck($visibleColumn, $key)
+    public function pluck($visibleColumn, $key = 'id')
     {
         if (! empty($visibleColumn) && ! empty($key)) {
             $this->keyAsValue = true;

+ 29 - 1
src/Grid/Filter/Presenter/SelectTable.php

@@ -27,6 +27,10 @@ class SelectTable extends Presenter
 
     protected $placeholder;
 
+    protected $visibleColumn;
+
+    protected $key;
+
     public function __construct(LazyRenderable $table)
     {
         $this->dialog = DialogTable::make($table);
@@ -58,7 +62,7 @@ class SelectTable extends Presenter
      */
     public function model(string $model, string $id = 'id', string $text = 'title')
     {
-        return $this->options(function ($v) use ($model, $id, $text) {
+        return $this->pluck($text, $id)->options(function ($v) use ($model, $id, $text) {
             if (! $v) {
                 return [];
             }
@@ -67,6 +71,22 @@ class SelectTable extends Presenter
         });
     }
 
+    /**
+     * 设置选中的key以及标题字段.
+     *
+     * @param $visibleColumn
+     * @param $key
+     *
+     * @return $this
+     */
+    public function pluck(?string $visibleColumn, ?string $key = 'id')
+    {
+        $this->visibleColumn = $visibleColumn;
+        $this->key = $key;
+
+        return $this;
+    }
+
     /**
      * 设置弹窗宽度.
      *
@@ -120,6 +140,14 @@ class SelectTable extends Presenter
         $this->dialog
             ->footer($this->renderFooter())
             ->button($this->renderButton());
+
+        // 设置选中的字段和待显示的标题字段
+        $this->dialog
+            ->getTable()
+            ->getRenderable()
+            ->payload([
+                LazyRenderable::ROW_SELECTOR_COLUMN_NAME => [$this->key, $this->visibleColumn],
+            ]);
     }
 
     protected function formatOptions()

+ 12 - 1
src/Grid/LazyRenderable.php

@@ -8,6 +8,9 @@ use Dcat\Admin\Support\LazyRenderable as Renderable;
 
 abstract class LazyRenderable extends Renderable
 {
+    const SIMPLE_NAME = '_simple_';
+    const ROW_SELECTOR_COLUMN_NAME = '_row_columns_';
+
     /**
      * 是否启用简化模式.
      *
@@ -50,7 +53,7 @@ HTML;
      */
     public function simple(bool $value = true)
     {
-        return $this->payload(['_simple_' => $value]);
+        return $this->payload([static::SIMPLE_NAME => $value]);
     }
 
     /**
@@ -77,6 +80,14 @@ HTML;
             $grid->rowSelector()->click();
         }
 
+        if (! empty($this->payload[static::ROW_SELECTOR_COLUMN_NAME])) {
+            [$key, $visibleColumn] = $this->payload[static::ROW_SELECTOR_COLUMN_NAME];
+
+            $key && $grid->rowSelector()->idColumn($key);
+
+            $visibleColumn && $grid->rowSelector()->titleColumn($visibleColumn);
+        }
+
         return $grid;
     }
 

+ 4 - 4
src/Support/helpers.php

@@ -418,7 +418,7 @@ if (! function_exists('admin_asset')) {
      */
     function admin_asset($path)
     {
-        return Dcat\Admin\Admin::asset()->url($path);
+        return Admin::asset()->url($path);
     }
 }
 
@@ -434,7 +434,7 @@ if (! function_exists('admin_route')) {
      */
     function admin_route(?string $route, array $params = [], $absolute = true)
     {
-        return Dcat\Admin\Admin::app()->getRoute($route, $params, $absolute);
+        return Admin::app()->getRoute($route, $params, $absolute);
     }
 }
 
@@ -448,7 +448,7 @@ if (! function_exists('admin_route_name')) {
      */
     function admin_route_name(?string $route)
     {
-        return Dcat\Admin\Admin::app()->getRoutePrefix().$route;
+        return Admin::app()->getRoutePrefix().$route;
     }
 }
 
@@ -462,7 +462,7 @@ if (! function_exists('admin_api_route_name')) {
      */
     function admin_api_route_name(?string $route = '')
     {
-        return Dcat\Admin\Admin::app()->getCurrentApiRoutePrefix().$route;
+        return Admin::app()->getCurrentApiRoutePrefix().$route;
     }
 }
 

+ 8 - 0
src/Widgets/LazyTable.php

@@ -61,6 +61,14 @@ class LazyTable extends Widget
         return $this;
     }
 
+    /**
+     * @return LazyRenderable
+     */
+    public function getRenderable()
+    {
+        return $this->renderable;
+    }
+
     /**
      * 设置是否自动加载.
      *