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"
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>
CodePudding user response:
<td>
<select name="select[]" id="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>
CodePudding user response:
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"
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.