Преглед изворни кода

Grid\Column::select支持一对一关联字段更新

jqh пре 5 година
родитељ
комит
ba0fdb8282
1 измењених фајлова са 39 додато и 29 уклоњено
  1. 39 29
      src/Grid/Displayers/Select.php

+ 39 - 29
src/Grid/Displayers/Select.php

@@ -9,56 +9,66 @@ class Select extends AbstractDisplayer
     public static $js = '@select2';
     public static $css = '@select2';
 
+    protected $selector = 'grid-column-select';
+
     public function display($options = [])
     {
         if ($options instanceof \Closure) {
             $options = $options->call($this, $this->row);
         }
 
-        $name = $this->column->getName();
+        $this->addScript();
 
-        $class = "grid-select-{$name}";
+        $optionsHtml = '';
 
-        $script = <<<JS
+        foreach ($options as $option => $text) {
+            $selected = (string) $option === (string) $this->value ? 'selected' : '';
+            $optionsHtml .= "<option value=\"$option\" $selected>$text</option>";
+        }
+
+        return <<<EOT
+<div class="input-group input-group-sm">
+    <select style="width: 100%;" class="{$this->selector}" data-key="{$this->getKey()}" data-name="{$this->column->getName()}">
+    $optionsHtml
+    </select>
+</div>
+EOT;
+    }
 
-$('.$class').off('change').select2().on('change', function(){
-    var pk = $(this).data('key');
-    var value = $(this).val();
+    protected function addScript()
+    {
+        $script = <<<JS
+$('.{$this->selector}').off('change').select2().on('change', function(){
+    var pk = $(this).data('key'),
+        value = $(this).val(),
+        name = $(this).data('name'),
+        data = {
+            _token: Dcat.token,
+            _method: 'PUT'
+        };
+    
+    if (name.indexOf('.') === -1) {
+        data[name] = value;
+    } else {
+        name = name.split('.');
+        
+        data[name[0]] = {};
+        data[name[0]][name[1]] = value;
+    }
+    
     Dcat.NP.start();
     $.ajax({
         url: "{$this->resource()}/" + pk,
         type: "POST",
-        data: {
-            $name: value,
-            _token: Dcat.token,
-            _method: 'PUT'
-        },
+        data: data,
         success: function (data) {
             Dcat.NP.done();
             Dcat.success(data.message);
         }
     });
 });
-
 JS;
 
         Admin::script($script);
-
-        $key = $this->row->{$this->grid->getKeyName()};
-
-        $optionsHtml = '';
-
-        foreach ($options as $option => $text) {
-            $selected = $option == $this->value ? 'selected' : '';
-            $optionsHtml .= "<option value=\"$option\" $selected>$text</option>";
-        }
-
-        return <<<EOT
-<div class="input-group input-group-sm">
-    <select style="width: 100%;" class="$class" data-key="$key">
-    $optionsHtml
-    </select>
-</div>
-EOT;
     }
 }