skip to Main Content

I have a selectpicker inside a table. I am passing the selected value into it but it shows the first value. why?

<td>
    <select name="select[]" id="select" data-selected="224515"
        class="selectpicker form-control select"  data-live-search="true"
        data-show-subtext="true" data-size="5" 
        data-container="body" title="Search Parts..">
        @foreach ($list as $p)
            <option data-id="{{ $p->id }}"
                data-subtext="{{ $p->code }}"
                data-tokens="{{ $p->id }}"
                data-name="{{ $p->name }}" data-price="{{ $p->price }}"
                data-code="{{ $p->code }}">
                {{ $p->name }}</option>
        @endforeach
    </select>
</td>

2

Answers


  1. <td>
      <select name="select[]" id="select"
      class="selectpicker form-control select"  data-live-search="true" data-show- 
      subtext="true" data-size="5" data-container="body" title="Search Parts..">
        @foreach ($list as $p)
          <option data-id="{{ $p->id }}" 
            value="{{ $p->value }}" "{{ ( $p->value == "224515") ? 'selected' : '' }}"
            data-subtext="{{ $p->code }}"
            data-tokens="{{ $p->id }}" 
            data-name="{{ $p->name }}" 
            data-price="{{ $p->price }}" 
            data-code="{{ $p->code }}">{{ $p->name }}
          </option>
        @endforeach
    </select></td> 
    
    Login or Signup to reply.
  2. The selectpicker you’re using does not support setting a value like that (documentation here)
    You’re setting the data-selected attribute which is not supported.

    You can use the @selected helper from Blade. Documentation on additional attributes

    <td>
        <select name="select[]" id="select" data-selected="224515"
            class="selectpicker form-control select"  data-live-search="true"
            data-show-subtext="true" data-size="5" 
            data-container="body" title="Search Parts..">
            @foreach ($list as $p)
                <option data-id="{{ $p->id }}"
    
                    @selected($p->id === 224515)
    
                    data-subtext="{{ $p->code }}"
                    data-tokens="{{ $p->id }}"
                    data-name="{{ $p->name }}" 
                    data-price="{{ $p->price }}"
                    data-code="{{ $p->code }}">
                    {{ $p->name }}
                </option>
            @endforeach
        </select>
    </td>
    

    I assumed the data-selected attribute is filled dynamically, so replace the id in the @selected with the dynamic stuff.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search