I build a dynamic dropdown in select option but when i implement a select2 package it not work. Here is my code. The value of type not passing to the court select.
here is my Blade.php
{{$selectedType}}
<div class="row g-3 mb-3">
<div class="col-md">
<div class="form-floating" wire:ignore>
<select class="form-select" id="type" wire:model="selectedType" required>
<option value="">Select Court Type</option>
#foreach($types as $type)
<option value="{{$type->id}}">{{$type->court_type_name}}</option>
#endforeach
</select>
<label for="floatingInputGrid">Type</label>
</div>
</div>
#if (!is_null($courts))
<div class="col-md">
<div class="form-floating" wire:ignore>
<select class="form-select" wire:model="selectedCourt" required>
<option value="">Select Court Location</option>
#foreach($courts as $court)
<option value="{{$court->id}}">{{$court->court_name}}, {{$court->province}}</option>
#endforeach
</select>
<label for="floatingInputGrid">Court Location</label>
</div>
</div>
#endif
</div>
The livewire components works perfectly but in select2 it not work
public function render()
{
$types = Type::All();
$periods = Period::All();
return view('livewire.collection.add', compact('types', 'periods'));
}
public function updatedSelectedType($court_id)
{
$this->courts = Court::where('court_type', $court_id)->orderBy('court_name')->get();
}
public function updatedSelectedCourt($clerk_id)
{
$this->clerks = Clerk::where('court', $clerk_id)->get();
}
Select2
<script>
$(document).ready(function () {
$('.form-select').select2();
$('#type').on('change', function (e) {
var data = $('#type').select2("val");
#this.set('selectedType', data);
});
$('#court').on('change', function (e) {
var data = $('#court').select2("val");
#this.set('selectedCourt', data);
});
});
</script>
Nothing happen when clicked.
I solve select2 element in Livewire like this.
<div class="form-floating">
<select class="form-select" wire:model="selectedCourt">
<option value="">Select Court Location</option>
#foreach($courts as $court)
<option value="{{$court->id}}">{{$court->court_name}}</option>
#endforeach
</select>
<label for="floatingInputGrid">Court Location</label>
</div>
//
<script>
$(document).ready(function() {
window.initSelectDrop=()=>{
$('.form-select').select2({
placeholder: '{{ __('locale.Select') }}',
allowClear: true
});
initSelectDrop();
$('.form-select').on('change', function (e) {
livewire.emit('selectedItemCourt', e.target.value)
});
window.livewire.on('select2',()=>{
initSelectDrop();
});
});
</script>
in component
public function hydrate()
{
$this->emit('select2');
}
public $listeners = ['selectedItemCourt'];
//.....
public function selectedItemCourt($value)
{
dd($value);
$this->selectedCourt = $value;
}
Related
I just want to show the spinner during option selection
<div wire:ignore class="mt-0 ">
<select class="form-control form-control-sm custom__select " id="select2" wire:model="check">
<option value="10">-- Select region --</option>
#foreach($regions as $region)
<option value="{{ $region->region_id }}">{{ $region->title_ru }}</option>
#endforeach
</select>
</div>
#if($loadState)
<div class="position-relative">
<div class="spinner-border spinner-border-sm text-light " role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
#endif
#push('scripts')
<script>
$(document).ready(function () {
$('#select2').select2({
placeholder: 'Select an option',
});
$(document).on('change', '#select2', function (e) {
#this.set('selRegion', e.target.value);
});
});
</script>
#endpush
controller:
public function updatedCheck()
{
$this->loadState = true;
}
this works without wire:ignore, but select2 itself doesn't work without it. i saw a similar question, but can't figure out how to apply it to my example
I don't know how to populate the edit function my select2 form. Here's my attempt on doing it. Controller:
public function edit($id)
{
$company = Company::find($id);
$categories = MainCategory::all();
$categories2 = array();
foreach ($categories as $category) {
$categories2[$category->main_cat_id] = $category->main_cat_name;
}
return view('company/edit_company')->with('company', $company)->with('categories', $categories2);
}
here's my view:
<select class="form-control select2-multi" name="company_main_cat[]" multiple="multiple" style="width: 50%" data-placeholder="select main category">
<option value='{{$category->main_cat_id}}'>{{$category->main_cat_name}}</option>
</select>
here's my JS:
<script type="text/javascript">
$('.select2-multi').select2();
$('.select2-multi').select2().val({!! json_encode($company->maincategories()->allRelatedIds()) !!}).trigger('change');
</script>
if you get your category data correctly to your view; Which seems to be an array from the code. You can foreach them inside your select element.
<select class="form-control select2-multi" name="company_main_cat[]" multiple="multiple" style="width: 50%" data-placeholder="select main category">
#foreach($category as $category_single)
<option value='{{$category_single->main_cat_id}}'>{{$category_single->main_cat_name}}</option>
#endforeach
</select>
This is my view code.
#using (Html.BeginForm())
{
#Html.LabelFor(m=>m.GroupID)
#Html.DropDownListFor(m => m.GroupID, Model.GroupList, "Please select", new { id = "ddlgrp" })
if (ViewBag.selectedperms!=null)
{
foreach(var permission in Model.Permissions)
{
<label>
#Html.RadioButtonFor(m=>m.perm_id,permission.perm_id)
<span>#permission.perm_levelname</span>
</label>
}
}
else
{
foreach(var permission in Model.Permissions)
{
<label>
#if (Model.perm_id.Equals(ViewBag.selectedperms))
{
#Html.RadioButtonFor(m=>m.perm_id,permission.perm_id,true)
<span>#permission.perm_levelname</span>
}
else
{
#Html.RadioButtonFor(m=>m.perm_id,permission.perm_id)
<span>#permission.perm_levelname</span>
}
</label>
}
}
<p><input type="submit" value="Submit" /></p>
This is my jquery code.
$(document).ready(function () {
$("#ddlgrp").change(function () {
$("#log").ajaxError(function (event, jqxhr, settings, exception) {
alert(exception);
});
var grpselected = $("select option:selected").val();
alert(grpselected);
$.get('#Url.Action("CheckPermissions")',
{ id: grpselected }, function (data) {
});
});
});
This is actionmethod
public ActionResult CheckPermissions(int id)
{
tblperm od = new tblperm();
var selectedperms = (from c in db.tblperm where c.grp_id==id select c.perm_id).SingleOrDefault();
ViewBag.selectedperms = selectedperms;
return View(od);
}
Assume for groupip id 1 there is a permisson with permid 1. So when i select 1 from dropdwon corresponding dropdown with id 1 should be checked. I have tried with above code but its not working. Can anybody suggest me where i am going wrong?
I think you need something like following snippet.
$("#GroupID").change(function () {
$('input[name="perm_id"][value="' + this.value + '"]').prop('checked', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="GroupID">
<option value="">Select group</option>
<option value="1">Group 1</option>
<option value="2">Group 2</option>
<option value="3">Group 3</option>
<option value="4">Group 4</option>
</select>
<input type="radio" value="1" name="perm_id" /><label>Permission 1</label>
<input type="radio" value="2" name="perm_id" /><label>Permission 2</label>
<input type="radio" value="3" name="perm_id" /><label>Permission 3</label>
<input type="radio" value="4" name="perm_id" /><label>Permission 4</label>
Update
Action method
public ActionResult CheckPermissions(int id)
{
tblperm od = new tblperm();
var selectedperms = (from c in db.tblperm where c.grp_id == id select c.perm_id).SingleOrDefault();
return Json(selectedperms, JsonRequestBehavior.AllowGet);
}
jQuery
$("#ddlgrp").change(function () {
var grpselected = $(this).val();
$.getJSON('#Url.Action("CheckPermissions")', { id: grpselected }, function (data) {
$('input[name="perm_id"][value="' + data + '"]').prop('checked', true);
});
});
I have this bit of code in one of my views.
Instead of this hardcoded for testing,
selectedProduct=" + "Bonds"
I need
selectedProduct=" + SelectedValueFromTheDropDownList
How can I get the selected value?
Tks in advance
<div class="Left Width50Perc" style="display: inline">
<select id="searchForBondsSelect">
<option value="Bonds">Bonds</option>
<option value="Stocks">Stocks</option>
<option value="Funds">Funds</option>
<option value="ETFS">ETFs</option>
</select>
</div>
<div class="Right Width50Perc">
<div class="Left FilterTooltip" style="display: inline">
#CommonHelpers.AutoCompleteTextBox("/st/searchlist?partialString={0}**&selectedProduct=" + "Bonds"**, InputClass: "TextBox2 Data10 AutoCompleteTextBox", Watermark: "Enter a value")
</div>
</div>*
I managed with the help of Thereader.
I tried a different approach with new code which was not working.
<div class="Left Width50Perc" style="display: inline">
<select id="searchForBondsSelect" class="selectedValue">
<option value="Bonds">Bonds</option>
<option value="Stocks">Stocks</option>
<option value="Funds">Funds</option>
<option value="ETFS">ETFs</option>
</select>
</div>
<script>
$(".selectedValue").select(
{
select: function (event, ui) {
$(".selectedValue").val(ui.select.val);
$.ajax({
url: "/searchtool/st/GetProductType",
data: { productType: ui.select.val },
async: true,
type: "POST",
})
}
});
</script>
Then the TheReader suggested this. Thanks once again.
<script>
$(".selectedValue").change(function() {
$.ajax({
url: "/searchtool/st/GetProductType",
data: { productType: $(this).val() },
async: true,
type: "POST",
})
});
</script>
I have the following code segment but can not seem to capture the flipped value.
Java segment in the Head and HTML in Body:
<script>
$('#pauseslider').bind('change', function(event, data) {
if ($(this).slider().val() == 'off') {
alert($('No');
if ($(this).slider().val() == 'on') {
alert($('ON');
});
//**********************************
</script>
<div data-role="fieldcontain" data-theme="a">
<label for="pauseslider" data-theme="e">Mode</label>
<select name="pauseslider" id="pauseslider" data-role="slider" data-theme="e">
<option value="off" data-theme="a">OFF</option>
<option value="on" data-theme="a">ON</option>
</select>
</div>