Просмотр исходного кода

行内编辑优化;重构checkbox以及radio行内编辑

Jiang qinghua 3 лет назад
Родитель
Сommit
4a32a9788d

+ 17 - 0
resources/views/grid/displayer/editinline/checkbox.blade.php

@@ -0,0 +1,17 @@
+@extends('admin::grid.displayer.editinline.template')
+
+@section('field')
+    {!! $checkbox !!}
+@endsection
+
+<script>
+@section('popover-content')
+    $template.find('input[type=checkbox]').each(function (index, checkbox) {
+        if($.inArray($(checkbox).attr('value'), $trigger.data('value')) >= 0) {
+            $(checkbox).attr('checked', true);
+        }
+    });
+@endsection
+</script>
+
+

+ 1 - 2
resources/views/grid/displayer/editinline/input.blade.php

@@ -10,9 +10,8 @@
 @endsection
 
 @section('popover-shown')
-    $popover.find('.ie-input').focus();
     @if(! empty($mask))
-    $popover.find('.ie-input').inputmask(@json($mask));
+    $popover.find('.ie-input').inputmask({!! admin_javascript_json($mask) !!});
     @endif
 @endsection
 </script>

+ 17 - 0
resources/views/grid/displayer/editinline/radio.blade.php

@@ -0,0 +1,17 @@
+@extends('admin::grid.displayer.editinline.template')
+
+@section('field')
+    {!! $radio !!}
+@endsection
+
+<script>
+@section('popover-content')
+    $template.find('input[type=radio]').each(function (index, checkbox) {
+        if(String($(checkbox).attr('value')) === String($trigger.data('value'))) {
+            $(checkbox).attr('checked', true);
+        }
+    });
+@endsection
+</script>
+
+

+ 30 - 3
resources/views/grid/displayer/editinline/template.blade.php

@@ -11,7 +11,12 @@
         data-url="{!! $url !!}"
         data-refresh="{{ $refresh }}"
     >
-        <span class="ie-display">{{ $display }}</span>
+        <span class="ie-display">
+            {{ $display }}
+            @if(! $display)
+                <i class="feather icon-edit-2"></i>
+            @endif
+        </span>
     </a>
 </span>
 
@@ -100,6 +105,7 @@
     $(document).off('click', '.ie-content .ie-submit').on('click', '.ie-content .ie-submit', function () {
         var $popover = $(this).closest('.ie-content'),
             $trigger = $popover.data('trigger'),
+            name = $trigger.data('name'),
             original = $trigger.data('original'),
             refresh = $trigger.data('refresh'),
             val,
@@ -111,6 +117,19 @@
                 val = $popover.find('.ie-input').val();
                 label = val;
                 break;
+            case 'checkbox':
+                val = [];
+                label = [];
+                $popover.find('.ie-input:checked').each(function(){
+                    val.push($(this).val());
+                    label.push($(this).parent().text());
+                });
+                label = label.join(';');
+                break;
+            case 'radio':
+                val = $popover.find('.ie-input:checked').val();
+                label = $popover.find('.ie-input:checked').parent().text();
+                break;
         }
 
         if (val == original) {
@@ -121,7 +140,15 @@
         Dcat.NP.start();
 
         var data = {};
-        data[$trigger.data('name')] = val;
+
+        if (name.indexOf('.') === -1) {
+            data[name] = val;
+        } else {
+            name = name.split('.');
+
+            data[name[0]] = {};
+            data[name[0]][name[1]] = val;
+        }
         data['_inline_edit_'] = 1;
 
         $.put({
@@ -135,7 +162,7 @@
             var data = res.data;
             if (res.status === true) {
                 Dcat.success(data.message);
-                $popover.data('display').html(label);
+                $popover.data('display').html(label || '<i class="feather icon-edit-2"></i>');
                 $trigger.data('value', val).data('original', val);
                 hide();
                 refresh && Dcat.reload();

+ 0 - 6
resources/views/grid/displayer/editinline/textarea.blade.php

@@ -8,10 +8,4 @@
 @section('popover-content')
     $template.find('textarea').text($trigger.data('value'));
 @endsection
-
-@section('popover-shown')
-    $popover.find('.ie-input').focus();
-@endsection
 </script>
-
-

+ 29 - 19
src/Grid/Displayers/Checkbox.php

@@ -2,32 +2,42 @@
 
 namespace Dcat\Admin\Grid\Displayers;
 
-use Dcat\Admin\Admin;
 use Dcat\Admin\Support\Helper;
+use Illuminate\Support\Arr;
 
-class Checkbox extends AbstractDisplayer
+class Checkbox extends Editable
 {
+    protected $type = 'checkbox';
+
+    protected $view = 'admin::grid.displayer.editinline.checkbox';
+
     public function display($options = [], $refresh = false)
     {
-        if ($options instanceof \Closure) {
-            $options = $options->call($this, $this->row);
-        }
-
-        $this->value = Helper::array($this->value);
-
-        return Admin::view('admin::grid.displayer.checkbox', [
-            'options'  => $options,
-            'key'      => $this->getKey(),
-            'column'   => $this->column->getName(),
-            'value'    => $this->value,
-            'class'    => $this->getElementClass(),
-            'resource' => $this->resource(),
-            'refresh'  => $refresh,
-        ]);
+        $options['options'] = $options;
+        $options['refresh'] = $refresh;
+        $options['checkbox'] = $this->renderCheckbox($options['options']);
+
+        return parent::display($options);
+    }
+
+    protected function renderCheckbox($options)
+    {
+        $checkbox = \Dcat\Admin\Widgets\Checkbox::make($this->getName().'[]');
+        $checkbox->options($options);
+        $checkbox->class('ie-input');
+
+        return $checkbox;
+    }
+
+    protected function getValue()
+    {
+        return implode('; ', Arr::only($this->options['options'], Helper::array($this->value, false)));
     }
 
-    protected function getElementClass()
+    protected function getOriginal()
     {
-        return 'grid-checkbox-'.$this->column->getName();
+        return json_encode(array_map(function ($value) {
+            return (string) $value;
+        }, Helper::array($this->column->getOriginal(), false)));
     }
 }

+ 18 - 4
src/Grid/Displayers/Editable.php

@@ -2,7 +2,6 @@
 
 namespace Dcat\Admin\Grid\Displayers;
 
-use Dcat\Admin\Admin;
 use Dcat\Admin\Support\Helper;
 
 abstract class Editable extends AbstractDisplayer
@@ -37,14 +36,29 @@ abstract class Editable extends AbstractDisplayer
         return [
             'key'     => $this->getKey(),
             'class'   => $this->getSelector(),
+            'name'    => $this->getName(),
             'type'    => $this->type,
-            'display' => Helper::render($this->value),
-            'value'   => $this->column->getOriginal(),
-            'name'    => $this->column->getName(),
+            'display' => $this->getValue(),
+            'value'   => $this->getOriginal(),
             'url'     => $this->getUrl(),
         ];
     }
 
+    protected function getName()
+    {
+        return $this->column->getName();
+    }
+
+    protected function getValue()
+    {
+        return $this->value;
+    }
+
+    protected function getOriginal()
+    {
+        return $this->column->getOriginal();
+    }
+
     protected function getSelector()
     {
         return 'grid-editable-'.$this->type;

+ 11 - 0
src/Grid/Displayers/Input.php

@@ -2,9 +2,20 @@
 
 namespace Dcat\Admin\Grid\Displayers;
 
+use Dcat\Admin\Admin;
+
 class Input extends Editable
 {
     protected $type = 'input';
 
     protected $view = 'admin::grid.displayer.editinline.input';
+
+    public function display($options = [])
+    {
+        if (! empty($options['mask'])) {
+            Admin::requireAssets('@jquery.inputmask');
+        }
+
+        return parent::display($options);
+    }
 }

+ 22 - 17
src/Grid/Displayers/Radio.php

@@ -2,29 +2,34 @@
 
 namespace Dcat\Admin\Grid\Displayers;
 
-use Dcat\Admin\Admin;
+use Illuminate\Support\Arr;
 
-class Radio extends AbstractDisplayer
+class Radio extends Editable
 {
+    protected $type = 'radio';
+
+    protected $view = 'admin::grid.displayer.editinline.radio';
+
     public function display($options = [], $refresh = false)
     {
-        if ($options instanceof \Closure) {
-            $options = $options->call($this, $this->row);
-        }
-
-        return Admin::view('admin::grid.displayer.radio', [
-            'options'  => $options,
-            'key'      => $this->getKey(),
-            'column'   => $this->column->getName(),
-            'value'    => $this->value,
-            'class'    => $this->getElementClass(),
-            'resource' => $this->resource(),
-            'refresh'  => $refresh,
-        ]);
+        $options['options'] = $options;
+        $options['refresh'] = $refresh;
+        $options['radio'] = $this->renderRadio($options['options']);
+
+        return parent::display($options);
+    }
+
+    protected function renderRadio($options)
+    {
+        $checkbox = \Dcat\Admin\Widgets\Radio::make($this->getName());
+        $checkbox->options($options);
+        $checkbox->class('ie-input');
+
+        return $checkbox;
     }
 
-    protected function getElementClass()
+    protected function getValue()
     {
-        return 'grid-radio-'.$this->column->getName();
+        return Arr::get($this->options['options'], $this->value);
     }
 }