Bladeren bron

Column::limit

jqh 5 jaren geleden
bovenliggende
commit
d1b1a3c785
3 gewijzigde bestanden met toevoegingen van 64 en 33 verwijderingen
  1. 3 0
      src/Grid/Column.php
  2. 0 33
      src/Grid/Column/HasDisplayers.php
  3. 61 0
      src/Grid/Displayers/Limit.php

+ 3 - 0
src/Grid/Column.php

@@ -5,6 +5,7 @@ namespace Dcat\Admin\Grid;
 use Closure;
 use Dcat\Admin\Grid;
 use Dcat\Admin\Grid\Displayers\AbstractDisplayer;
+use Dcat\Admin\Grid\Displayers\Orderable;
 use Dcat\Admin\Traits\HasBuilderEvents;
 use Dcat\Admin\Traits\HasDefinitions;
 use Illuminate\Contracts\Support\Arrayable;
@@ -35,6 +36,7 @@ use Illuminate\Support\Traits\Macroable;
  * @method $this downloadable($server = '', $disk = null)
  * @method $this copyable()
  * @method $this orderable()
+ * @method $this limit(int $limit = 100, string $end = '...')
  * @method $this ascii()
  * @method $this camel()
  * @method $this finish($cap)
@@ -87,6 +89,7 @@ class Column
         'downloadable'     => Displayers\Downloadable::class,
         'copyable'         => Displayers\Copyable::class,
         'orderable'        => Displayers\Orderable::class,
+        'limit'            => Displayers\Limit::class,
     ];
 
     /**

+ 0 - 33
src/Grid/Column/HasDisplayers.php

@@ -218,39 +218,6 @@ trait HasDisplayers
         });
     }
 
-    /**
-     * Limit the number of characters in a string, or the number of element in a array.
-     *
-     * @param int    $limit
-     * @param string $end
-     *
-     * @return $this
-     */
-    public function limit($limit = 100, $end = '...')
-    {
-        return $this->display(function ($value) use ($limit, $end) {
-            if ($value !== null && ! is_scalar($value)) {
-                $value = Helper::array($value);
-
-                if (count($value) <= $limit) {
-                    return $value;
-                }
-
-                $value = array_slice($value, 0, $limit);
-
-                array_push($value, $end);
-
-                return $value;
-            }
-
-            if (mb_strlen($value, 'UTF-8') <= $limit) {
-                return $value;
-            }
-
-            return mb_substr($value, 0, $limit).$end;
-        });
-    }
-
     /**
      * @return $this
      */

+ 61 - 0
src/Grid/Displayers/Limit.php

@@ -0,0 +1,61 @@
+<?php
+
+namespace Dcat\Admin\Grid\Displayers;
+
+use Dcat\Admin\Admin;
+use Dcat\Admin\Support\Helper;
+use Illuminate\Support\Str;
+
+class Limit extends AbstractDisplayer
+{
+    protected function addScript()
+    {
+        $script = <<<'JS'
+$('.limit-more').click(function () {
+    $(this).parent('.limit-text').toggleClass('d-none').siblings().toggleClass('d-none');
+});
+JS;
+
+        Admin::script($script);
+    }
+
+    public function display($limit = 100, $end = '...')
+    {
+        // 数组
+        if ($this->value !== null && ! is_scalar($this->value)) {
+            $value = Helper::array($this->value);
+
+            if (count($value) <= $limit) {
+                return $value;
+            }
+
+            $value = array_slice($value, 0, $limit);
+
+            array_push($value, $end);
+
+            return $value;
+        }
+
+        // 字符串
+        $this->addScript();
+
+        $value = Str::limit($this->value, $limit, $end);
+
+        $original = $this->column->getOriginal();
+
+        if ($value == $original) {
+            return $value;
+        }
+
+        return <<<HTML
+<div class="limit-text">
+    <span class="text">{$value}</span>
+    &nbsp;<a href="javascript:void(0);" class="limit-more">&nbsp;<i class="fa fa-angle-double-down"></i></a>
+</div>
+<div class="limit-text d-none">
+    <span class="text">{$original}</span>
+    &nbsp;<a href="javascript:void(0);" class="limit-more">&nbsp;<i class="fa fa-angle-double-up"></i></a>
+</div>
+HTML;
+    }
+}