Ver Fonte

修复radio表单和快捷创建select表单会默认选中key为0的选项问题

jqh há 5 anos atrás
pai
commit
4501ae8b02

+ 1 - 1
resources/views/filter/select.blade.php

@@ -8,7 +8,7 @@
             <option value="">{{trans('admin.all')}}</option>
         @endif
         @foreach($options as $select => $option)
-            <option value="{{$select}}" {{ (string)$select === (string)request($name, $value) ?'selected':'' }}>{{$option}}</option>
+            <option value="{{$select}}" {{ Dcat\Admin\Support\Helper::equal($select, request($name, $value)) ?'selected':'' }}>{{$option}}</option>
         @endforeach
     </select>
 </div>

+ 1 - 1
resources/views/form/select.blade.php

@@ -23,7 +23,7 @@
              @else
                 <option value=""></option>
                 @foreach($options as $select => $option)
-                    <option value="{{$select}}" {{ (string) $select === (string) old($column, $value) ?'selected':'' }}>{{$option}}</option>
+                    <option value="{{$select}}" {{ Dcat\Admin\Support\Helper::equal($select, old($column, $value)) ?'selected':'' }}>{{$option}}</option>
                 @endforeach
             @endif
         </select>

+ 1 - 1
resources/views/grid/quick-create/select.blade.php

@@ -3,7 +3,7 @@
 
         <option value=""></option>
         @foreach($options as $select => $option)
-            <option value="{{$select}}" {{ $select == old($column, $value) ?'selected':'' }}>{{$option}}</option>
+            <option value="{{$select}}" {{ Dcat\Admin\Support\Helper::equal($select, old($column, $value)) ?'selected':'' }}>{{$option}}</option>
         @endforeach
     </select>
 </div>

+ 1 - 1
resources/views/widgets/radio.blade.php

@@ -4,7 +4,7 @@
 
 @foreach($options as $k => $label)
     <div class="vs-radio-con vs-radio-success{{ $style }}" style="margin-right: {{ $right }}">
-        <input {!! in_array($k, $disabled) ? 'disabled' : '' !!} value="{{$k}}" {!! $attributes !!} {!! ($checked == $k && $checked !== null) ? 'checked' : '' !!}>
+        <input {!! in_array($k, $disabled) ? 'disabled' : '' !!} value="{{$k}}" {!! $attributes !!} {!! \Dcat\Admin\Support\Helper::equal($checked, $k) ? 'checked' : '' !!}>
         <span class="vs-radio vs-radio-{{ $size }}">
           <span class="vs-radio--border"></span>
           <span class="vs-radio--circle"></span>

+ 21 - 0
src/Support/Helper.php

@@ -620,4 +620,25 @@ class Helper
             ? Process::fromShellCommandline(...$parameters)
             : new Process(...$parameters);
     }
+
+    /**
+     * 判断两个值是否相等.
+     *
+     * @param $value1
+     * @param $value2
+     *
+     * @return bool
+     */
+    public static function equal($value1, $value2)
+    {
+        if ($value1 === null || $value2 === null) {
+            return false;
+        }
+
+        if (! is_scalar($value1) || ! is_scalar($value2)) {
+            return $value1 === $value2;
+        }
+
+        return (string) $value1 === (string) $value2;
+    }
 }