Old value in multiple select option in laravel blade

前端 未结 9 1368
感动是毒
感动是毒 2021-02-13 12:08

Here is my select option


                        
    
提交评论

  • 2021-02-13 12:13

    This is the way I do, very dynamic.

    <select id="gender" name="gender">
        <option>Select</option>
            <option value="M">Male</option>
            <option value="F">Female</option>
    </select>
    
    <script>
        var currentGender = null;
        for(var i=0; i!=document.querySelector("#gender").querySelectorAll("option").length; i++)
        {
            currentGender = document.querySelector("#gender").querySelectorAll("option")[i];
            if(currentGender.getAttribute("value") == "{{ old("gender") }}")
            {
                currentGender.setAttribute("selected","selected");
            }
        }
    </script>
    
    0 讨论(0)
  • 2021-02-13 12:22

    The question was asked for multiple chosen.

    Assuming you have a field called designation and you need to choose multiple designation for adding 1 record and the field name is forWhom

    During Add

    You need to add this {{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} peace of code to your option tag

    which will look like

    <select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
        <option value="">--- Select ---</option>
        @foreach ($desgInfo as $key => $value)
            <option value="{{ $key }}" 
               {{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }}  />
               {{ $value }}
            </option>
        @endforeach
    </select>
    

    During Edit

    You need to add

    {{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} 
    
    {{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}
    

    which will look like

    <select id="forWhom" name="forWhom[]" multiple class="form-control chosen">
            <option value="">--- Select ---</option>
            @foreach ($desgInfo as $key => $value)
                <option value="{{ $key }}" 
                   {{ (collect(old('forWhom'))->contains($key)) ? 'selected':'' }} 
                   {{ (in_array($key,$info->forWhom)) ? 'selected' : ''}}  
                   />
                   {{ $value }}
                </option>
            @endforeach
        </select>
    

    Select all selected id's in a multiselect dropdown in laravel 5.4 with harvest chosen

    Thanks

    0 讨论(0)
  • 2021-02-13 12:29

    For me Jilson Thomas's answer did not work for multiple select so I changed it to:

    {{ in_array($food, old("recommended_food")) ? "selected":"") }}
    
    0 讨论(0)
  • 2021-02-13 12:33

    Best way:

    <label for="name">Tags</label>
        <select name="tags[]" class="form-control select-tag" multiple>
          @foreach($tags as $tag)
            <option value="{{$tag->id}}" {{in_array($tag->id, old("tags") ?: []) ? "selected": ""}}>{{$tag->name}}</option>
          @endforeach
        </select>
    
    0 讨论(0)
  • 2021-02-13 12:36

    I was parsing JSON data, and was faced with repopulating the multi-select as an array, and only wanted one table, and wasn't using keys since this array was being passed back from the show method in the controller, so had to supply the values directly on the template, in my case as a crud resource on the edit.blade.php template (also on the create template).

    This worked for me, and was the cleanest I could get it on the view.

    <select id="recommended_food"   multiple="multiple" size=3 style='height: 100%;' name="recommended_food[]">
        @if ($food->recomemded_food)
          {{ ($val = 'Fries')
            && $chosen = in_array($val, $food->recommended_food)
            ? 'selected':null}}
          <option value="{{$val}}" {{$chosen}}>{{$val}}</option>
          {{ ($val = 'Hot Dogs')
            && $chosen = in_array($val, $food->recommended_food)
            ? 'selected':null}}
          <option value="{{$val}}" {{$chosen}}>{{$val}}</option>
          {{ ($val = 'Hamburgers')
            && $chosen = in_array($val, $food->recommended_food)
            ? 'selected':null}}
          <option value="{{$val}}" {{$chosen}}>{{$val}}</option>
        @endif
      </select>
    
    0 讨论(0)
  • 提交回复
    热议问题