victorybiz / laravel-simple-select

Laravel Simple Select inputs component for Blade and Livewire.
MIT License
121 stars 18 forks source link

show selected element #3

Closed dcblogdev closed 2 years ago

dcblogdev commented 2 years ago

How do you show a standard selected value?

I have this usage:

<label>Supplier {{ $supplierId }}</label><br>
<x-simple-select name="supplierId" id="supplierId" wire:model="supplierId" :options="$suppliers" value-field='id' text-field='name' class="form-select" />

In my class I have mounted supplierId to an id I can see this if I render {{ $supplierId }} to the blade but the select does not pre-select the entry on load. The select menu remains blank until I manually select an item in the menu.

Screenshot 2021-09-27 at 10 10 01
victorybiz commented 2 years ago

In the component's mount method, ensure to assign the options first before assigning the model (selected value).

public function mount($id)
{  
    $product = Product::findOrFail($id);

    $this->suppliers = Supply::get()->toArray(); // first, get list of options

    $this->supplierId = $product->supplier_id; // second, assign the selected suppier

}
dcblogdev commented 2 years ago

thank you, turns out to be a problem with how I was selecting the data I was using pluck changing how the data was loaded solved the issue in selecting an initial value

Supplier::select('id', 'name')->orderBy('name')->get()->toArray()

Thank you! this package is great.