소스 검색

radio以及checkbox表单增加load以及loads方法

jqh 4 년 전
부모
커밋
39342b3fea

+ 6 - 7
resources/assets/dcat/js/extensions/Helpers.js

@@ -318,13 +318,12 @@ export default class Helpers {
             $.ajax(url).then(function(data) {
                 Dcat.loading(false);
                 target.find("option").remove();
-                $(target).select2({
-                    data: $.map(data, function (d) {
-                        d.id = d[options.idField];
-                        d.text = d[options.textField];
-                        return d;
-                    })
-                }).val(String(target.data('value')).split(',')).trigger('change');
+
+                $.map(data, function (d) {
+                    target.append(new Option(d[options.textField], d[options.idField], false, false));
+                });
+
+                $(target).val(String(target.data('value')).split(',')).trigger('change');
             });
         };
 

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
resources/dist/dcat/extra/select-table.js.map


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
resources/dist/dcat/js/dcat-app.js


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
resources/dist/dcat/js/dcat-app.js.map


+ 56 - 0
resources/views/form/checkbox.blade.php

@@ -26,4 +26,60 @@
         $(this).parents('.form-field').find('input[type="checkbox"]:not(:first)').prop('checked', this.checked).trigger('change');
     });
 </script>
+@endif
+
+@if(! empty($load))
+<script once>
+    var selector = '{!! $selector !!}';
+
+    $(document).off('change', selector);
+    $(document).on('change', selector, function () {
+        var values = [];
+
+        $(selector+':checked').each(function () {
+            if (String(this.value) === '0' || this.value) {
+                values.push(this.value)
+            }
+        });
+
+        Dcat.helpers.loadField(this, {
+            group: '{{ $load['group'] ?? '.fields-group' }}',
+            class: '.{{ $load['class'] }}',
+            url: "{!! $load['url'].(strpos($load['url'],'?')?'&':'?') !!}q=",
+            textField: "{{ $load['textField'] }}",
+            idField: "{{ $load['idField'] }}",
+            values: values,
+        });
+    });
+    $(selector+':checked').trigger('change')
+</script>
+@endif
+
+@if(! empty($loads))
+<script once>
+    var selector = '{!! $selector !!}',
+        fields = '{!! $loads['fields'] !!}'.split('^'),
+        urls = '{!! $loads['urls'] !!}'.split('^');
+
+    $(document).off('change', selector);
+    $(document).on('change', selector, function () {
+        var values = [];
+
+        $(selector+':checked').each(function () {
+            if (String(this.value) === '0' || this.value) {
+                values.push(this.value)
+            }
+        });
+
+        Dcat.helpers.loadFields(this, {
+            group: '.fields-group',
+            urls: urls,
+            fields: fields,
+            textField: "{{ $loads['textField'] }}",
+            idField: "{{ $loads['idField'] }}",
+            values: values,
+        });
+    });
+    $(selector+':checked').trigger('change')
+</script>
 @endif

+ 44 - 0
resources/views/form/radio.blade.php

@@ -14,3 +14,47 @@
 
     </div>
 </div>
+
+@if(! empty($load))
+<script once>
+    var selector = '{!! $selector !!}';
+
+    $(document).off('change', selector);
+    $(document).on('change', selector, function () {
+        var values = this.value;
+
+        Dcat.helpers.loadField(this, {
+            group: '{{ $load['group'] ?? '.fields-group' }}',
+            class: '.{{ $load['class'] }}',
+            url: "{!! $load['url'].(strpos($load['url'],'?')?'&':'?') !!}q=",
+            textField: "{{ $load['textField'] }}",
+            idField: "{{ $load['idField'] }}",
+            values: values,
+        });
+    });
+    $(selector+':checked').trigger('change')
+</script>
+@endif
+
+@if(! empty($loads))
+<script once>
+    var selector = '{!! $selector !!}',
+        fields = '{!! $loads['fields'] !!}'.split('^'),
+        urls = '{!! $loads['urls'] !!}'.split('^');
+
+    $(document).off('change', selector);
+    $(document).on('change', selector, function () {
+        var values = this.value;
+
+        Dcat.helpers.loadFields(this, {
+            group: '.fields-group',
+            urls: urls,
+            fields: fields,
+            textField: "{{ $loads['textField'] }}",
+            idField: "{{ $loads['idField'] }}",
+            values: values,
+        });
+    });
+    $(selector+':checked').trigger('change')
+</script>
+@endif

+ 62 - 0
src/Form/Field/CanLoadFields.php

@@ -0,0 +1,62 @@
+<?php
+
+namespace Dcat\Admin\Form\Field;
+
+use Illuminate\Support\Str;
+
+trait CanLoadFields
+{
+    /**
+     * 联动加载.
+     *
+     * @param string $field
+     * @param string $sourceUrl
+     * @param string $idField
+     * @param string $textField
+     *
+     * @return $this
+     */
+    public function load($field, $sourceUrl, string $idField = 'id', string $textField = 'text')
+    {
+        if (Str::contains($field, '.')) {
+            $field = $this->formatName($field);
+        }
+
+        $class = $this->normalizeElementClass($field);
+
+        $url = admin_url($sourceUrl);
+
+        return $this->addVariables(['load' => compact('url', 'class', 'idField', 'textField')]);
+    }
+
+    /**
+     * 联动加载多个字段.
+     *
+     * @param string $fields
+     * @param string $sourceUrls
+     * @param string $idField
+     * @param string $textField
+     *
+     * @return $this
+     */
+    public function loads($fields = [], $sourceUrls = [], string $idField = 'id', string $textField = 'text')
+    {
+        $fieldsStr = implode('^', array_map(function ($field) {
+            if (Str::contains($field, '.')) {
+                return $this->normalizeElementClass($field).'_';
+            }
+
+            return $this->normalizeElementClass($field);
+        }, (array) $fields));
+        $urlsStr = implode('^', array_map(function ($url) {
+            return admin_url($url);
+        }, (array) $sourceUrls));
+
+        return $this->addVariables(['loads' => [
+            'fields'    => $fieldsStr,
+            'urls'      => $urlsStr,
+            'idField'   => $idField,
+            'textField' => $textField,
+        ]]);
+    }
+}

+ 1 - 0
src/Form/Field/Checkbox.php

@@ -8,6 +8,7 @@ use Dcat\Admin\Widgets\Checkbox as WidgetCheckbox;
 class Checkbox extends MultipleSelect
 {
     use CanCascadeFields;
+    use CanLoadFields;
 
     protected $style = 'primary';
 

+ 1 - 0
src/Form/Field/Radio.php

@@ -9,6 +9,7 @@ use Dcat\Admin\Widgets\Radio as WidgetRadio;
 class Radio extends Field
 {
     use CanCascadeFields;
+    use CanLoadFields;
 
     protected $style = 'primary';
 

+ 1 - 55
src/Form/Field/Select.php

@@ -7,11 +7,11 @@ use Dcat\Admin\Form\Field;
 use Dcat\Admin\Support\Helper;
 use Illuminate\Database\Eloquent\Model;
 use Illuminate\Support\Arr;
-use Illuminate\Support\Str;
 
 class Select extends Field
 {
     use CanCascadeFields;
+    use CanLoadFields;
 
     protected $cascadeEvent = 'change';
 
@@ -84,60 +84,6 @@ class Select extends Field
         return $this;
     }
 
-    /**
-     * Load options for other select on change.
-     *
-     * @param string $field
-     * @param string $sourceUrl
-     * @param string $idField
-     * @param string $textField
-     *
-     * @return $this
-     */
-    public function load($field, $sourceUrl, string $idField = 'id', string $textField = 'text')
-    {
-        if (Str::contains($field, '.')) {
-            $field = $this->formatName($field);
-        }
-
-        $class = $this->normalizeElementClass($field);
-
-        $url = admin_url($sourceUrl);
-
-        return $this->addVariables(['load' => compact('url', 'class', 'idField', 'textField')]);
-    }
-
-    /**
-     * Load options for other selects on change.
-     *
-     * @param string $fields
-     * @param string $sourceUrls
-     * @param string $idField
-     * @param string $textField
-     *
-     * @return $this
-     */
-    public function loads($fields = [], $sourceUrls = [], string $idField = 'id', string $textField = 'text')
-    {
-        $fieldsStr = implode('^', array_map(function ($field) {
-            if (Str::contains($field, '.')) {
-                return $this->normalizeElementClass($field).'_';
-            }
-
-            return $this->normalizeElementClass($field);
-        }, (array) $fields));
-        $urlsStr = implode('^', array_map(function ($url) {
-            return admin_url($url);
-        }, (array) $sourceUrls));
-
-        return $this->addVariables(['loads' => [
-            'fields'    => $fieldsStr,
-            'urls'      => $urlsStr,
-            'idField'   => $idField,
-            'textField' => $textField,
-        ]]);
-    }
-
     /**
      * Load options from current selected resource(s).
      *

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

@@ -6,11 +6,11 @@ use Dcat\Admin\Form\Field;
 use Dcat\Admin\Grid\LazyRenderable;
 use Dcat\Admin\Support\Helper;
 use Dcat\Admin\Widgets\DialogTable;
-use Illuminate\Support\Str;
 
 class SelectTable extends Field
 {
     use PlainInput;
+    use CanLoadFields;
 
     /**
      * @var DialogTable
@@ -92,60 +92,6 @@ class SelectTable extends Field
         return $this;
     }
 
-    /**
-     * 联动加载.
-     *
-     * @param string $field
-     * @param string $sourceUrl
-     * @param string $idField
-     * @param string $textField
-     *
-     * @return $this
-     */
-    public function load($field, $sourceUrl, string $idField = 'id', string $textField = 'text')
-    {
-        if (Str::contains($field, '.')) {
-            $field = $this->formatName($field);
-        }
-
-        $class = $this->normalizeElementClass($field);
-
-        $url = admin_url($sourceUrl);
-
-        return $this->addVariables(['load' => compact('url', 'class', 'idField', 'textField')]);
-    }
-
-    /**
-     * 联动加载多个字段.
-     *
-     * @param string $fields
-     * @param string $sourceUrls
-     * @param string $idField
-     * @param string $textField
-     *
-     * @return $this
-     */
-    public function loads($fields = [], $sourceUrls = [], string $idField = 'id', string $textField = 'text')
-    {
-        $fieldsStr = implode('^', array_map(function ($field) {
-            if (Str::contains($field, '.')) {
-                return $this->normalizeElementClass($field).'_';
-            }
-
-            return $this->normalizeElementClass($field);
-        }, (array) $fields));
-        $urlsStr = implode('^', array_map(function ($url) {
-            return admin_url($url);
-        }, (array) $sourceUrls));
-
-        return $this->addVariables(['loads' => [
-            'fields'    => $fieldsStr,
-            'urls'      => $urlsStr,
-            'idField'   => $idField,
-            'textField' => $textField,
-        ]]);
-    }
-
     /**
      * @param array $options
      *

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.