Skip to content

Instantly share code, notes, and snippets.

@rachyharkov
Last active May 13, 2023 07:59
Show Gist options
  • Save rachyharkov/c169d42bc28449db98f67428c09cd413 to your computer and use it in GitHub Desktop.
Save rachyharkov/c169d42bc28449db98f67428c09cd413 to your computer and use it in GitHub Desktop.
Using ChoiceJS on Livewire component V.1
class ItemChartExample extends Component
{
public $singleChoiceValue;
public $multipleChoiceValue;
//just example method to triggered
public function testSubmitPreview() {
// it's should be work
dd($singleChoiceValue, $multipleChoiceValue);
}
}
<div class="mb-3" wire:ignore>
<label for="multi_select" class="form-label">Select Which is to be selected</label>
<select
id="multi_select"
class="form-control"
x-data
x-ref="multi_select_ref"
x-init="
new Choices($refs.multi_select_ref,{
//optional, if you want to add something like removeButton
removeItemButton: true
})
$refs.multi_select_ref.addEventListener('change', (event) => {
// you might need conditional in future using event.target.hasAttribute('multiple'), but that's optional
var model = Array.from(event.target.selectedOptions).map(option => option.value)
$wire.multipleChoiceValue = model
})
"
>
@foreach ($items as $item)
<option value="{{ $item->id }}">{{ $item->name }}</option>
@endforeach
</select>
<button type="button" class="btn btn-success" wire:click.prevent="testSubmitPreview">Preview</button>
</div>
<form>
<div class="mb-3" wire:ignore>
<label for="single_select" class="form-label">Select your single Choice</label>
<select name="single_select" id="single_select" class="form-control"
x-data
x-ref="single_select_ref"
x-init="
new Choices($refs.single_select_ref)
$refs.ujian_id.addEventListener('change', (event) => {
var model = event.target.value
$wire.singleChoiceValue = model
})
"
>
<option value="">Pilih Ujian</option>
@foreach ($items as $item)
<option value="{{ $item->id }}">{{ $item->name }}</option>
@endforeach
</select>
</div>
<button type="button" class="btn btn-success" wire:click.prevent="testSubmitPreview">Preview</button>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment