Assign "select" attribute to jquery select2 dropdown dynamically - php

I am working on jquery select2 dropdown. I have to display the multiple selected option in select area. For this purpose i loop through each of select2. If condition is met i will assign 'selected' attribute property to option so that it may appears in the select area.
But somehow the select2 fails to display the selected ''.
HTML CODE:
<div class="form-group col-md-12">
<label for="inputsskill">Skill<span class="required-field">*</span></label>
<fieldset>
<select class="custom-select w-100" name="skill_name[]" id="skill_name" multiple="multiple" style="width: 100%;">
#if(!$skills->isEmpty())
#foreach($skills as $skill)
<option value="{{$skill->id}}">{{$skill->name}}</option>
#endforeach
#endif
</select>
</fieldset>
</div>
JQUERY CODE:
// parse skills
var parseData = JSON.parse(skill);
// parseData is an array which contain 1,2,3 values like ['1','2','3']
$("#skill_name > option").each(function() {
// if select2 and array values matches assign selected attribute to that option
if( $.inArray($(this).val(), parseData) !== -1 ) {
$(this).attr('selected', 'selected');
}
});
Select2 Jquery:
$( "#skill_name" ).select2({
});
I dont know where i am going wrong, i would appreciate if someone guide me in this.
Thanks,

you can achieve this functionality with PHP easily.
E.g : fetch skills from the database which you want to preselect and create an array of skills. Then match this in your loop.Like
<div class="form-group col-md-12">
<label for="inputsskill">Skill<span class="required-field">*</span></label>
<fieldset>
<select class="custom-select w-100" name="skill_name[]" id="skill_name" multiple="multiple" style="width: 100%;">
#if(!$skills->isEmpty())
#foreach($skills as $skill)
<option value="{{$skill->id}}" <?php
if(in_array($skill->name, $selectedskillsArray)){
echo $selected = 'selected';
}
?> >{{$skill->name}}</option>
#endforeach
#endif
</select>
</fieldset>
</div>
Try this one.
Jquery solution
$( "#skill_name" ).select2({
});
$('#skill_name').val(["1","2","3"]).trigger('change');

Related

laravel fetch only the previously selected option submitted previously on a edit form

I've got a form that submits correctly, but it has a value on a question appended to a name, now to edit i'm almost done in the edit form, but the blade php with the foreach argument fetches all options from the select, i want to show and be able to edit the previously selected option. how do i do this on blade php.
resuming i want to submit it first then in dashboard of the form results have a button of edit and be able to edit the previously submited forms.
here's the code:
<select name="quilometragem" id="mySelect" class="chosen-select" onchange="myFunction()">
#foreach($viaturas as $item)
<option value="{{ $item->matricula }}:{{ $item->quilometragem }}">
{{ $item->matricula }}
</option>
#endforeach
</select>
<p id="demo"></p>
<div class="form-group mb-3">
<label for="">Chegada Kms</label>
<input type="number" name="kmfim" class="form-control">
</div>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "Matricula:QuilómetrosCorrentes: " + x;
}
</script>

Multi select dropdown select all

Here is my Controller function and view of multi select dropdown.How can i include 'all' condition to select all data that are in multselect dropdown .Please Help me
Controller Function
$where2 = array('departments.isDeleted =' => 0);
$data['dept'] = $this->general_model->get('departments',$where2);
View:
<div class="form-group">
<label class="col-sm-3 control-label">Departments</label
<div class="col-sm-6">
<select name="departmentId[]" class="form-control" multiple="multiple" id="departmentId" >
<?php if(is_array($dept)){ foreach($dept as $depts){ ?>
<option value="<?php echo $depts['departmentId'];?>">
<?php echo $depts['departmentName'];?>
</option>
<?php }} ?>
</select>
</div>
</div><!--/form-group-->
Before <?php if(is_array($dept)){ foreach($dept as $depts){ ?>
add <option value="all">All</option>
Sometimes the easiest solutions are the least apparent ;)
Note: you could make it so that (with js) when the "all" option is selected the rest are deselected for better ui but it really doesn't matter. On the backend you can just have a condition checking for all as selected and disregard the rest that are selected (because you have multiple enabled).

Select box need Focus in first element of the search field (multiselect)

This is my select Box Now its working fine, But I want the first element in search to be focus, Any idea will appreciate
$('.newmultiselect').multiselect({
enableFiltering: true,
filterPlaceholder: 'Search',
enableCaseInsensitiveFiltering : true,
includeSelectAllOption: false,
onChange: function(element, checked) {
var selectID = $(element).parent().attr('id');
$("#"+selectID).valid();
}
});
searchMultiselect();
My state field Div
<div class="col-12 col-sm-6 col-md-6 col-lg-4">
<div class="form-group">
<p>State<span class="notice-txt">*</span></p>
<select class="form-control validrmvcls newmultiselect" id="state" name="state">
<option value="">--Select--</option>
#foreach($states as $st)
<option value="{{$st['state_prefix']}}">{{$st['state_prefix']}}</option>
#endforeach
</select>
</div>
</div>
You can use css first child property, and for selecting default you can pass the value of first option in your result dataSet.
Krzysztof Janiszewski please have a look
Just add onDropdownShown callback and focus on your input.
EDIT
If you don't want to focus on input but first element, just do it like this:
onDropdownShown : function() {
$('li:visible:not(.multiselect-filter)').eq(0).focus();
}

Populate multi select with selected values in laravel 5.2

I have stored multiselect values in database as string
Now while trying to edit the values in need to fetch this highlighted string as array and send it to edit view , so that i can populate multi select with this selected values.
My code in view for multi select
<div class="form-group">
<label class="control-label col-md-3">News For
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
{!! Form::select('news_todisplay[]',['users'=>'Users','staff'=>'Staff','cinemahall'=>'Cinemahall'],$todisplayarray,array('class'=>'form-control','multiple'=>'multiple')) !!}
</select>
</div>
</div>
Where $todisplayarray is the array fetched from database i.e they are users,staff, cinemahall
My controller code
public function edit($id)
{
$newsdetails=General_news::findOrFail($id);
$todisplayarray=explode(',',$newsdetails->news_todisplay);
return view('admin.editnews',compact('newsdetails','todisplayarray'));
}
Page view-source shows this
<select class="form-control" multiple="multiple" name="news_todisplay[]"><option value="users">Users</option><option value="staff">Staff</option><option value="cinemahall" selected="selected">Cinemahall</option></select>
This only shows last value as selected, where as it should display 3 of the values as selected.
Kindly let me know where I am going wrong.
Find the below answer
<?php
//get the old values from form
$old = old('category');
//get data from database table field
$cate= explode(',',$data->category_ids);
//stay the values after form submission
if($old){ $cate=$old; }
?>
<select id="categories" name="category[]" multiple>
#foreach ($categories as $val)
<option value="{{ $val->id }}" <?php echo in_array($val->id,$cate)?"selected" :"" ;?> >{{ ucfirst($val->category_name) }}</option>
#endforeach
</select>
I've used loop the categories data with id's and match the data stored in the table

PHP jQuery Data-Attribute populate dropdown

I have an HREF with various data-attributes (about 8) that come from a PHP query and loaded into a table. When you click the button, it takes the data-attributes via jQuery and sends them to a modal window, where I can use them in a form.
I can open the window and place the attributes into an INPUT field with no problem.
What I would like to do is place the attribute in the first OPTION in a SELECT dropdown.
Here is the button with the attributes:
<tr><td><a href='' class='modAccount'
data-modcode=".$row[partner_code]."
data-modname=\"".$row[partner_name]."\"
data-boiler=\"".$row[boilerplate]."\"
data-surcharges=\"".$row[no_new_surcharges]."\"
data-ccalog=\"".$row[cca_logistics]."\"
data-toggle='modal'>Click</a></td></tr>
I have quite a few data-attributes. I had to eliminate a few for this question.
Here is the jQuery used to retrieve the data-attributes and send them to a modal window, called modifyModal.
Here is the jQuery:
$(function()
{
$('.modAccount').click(function(e)
{
e.preventDefault();
$partnerCode = $(this).attr('data-modcode');
$partnerName = $(this).attr('data-modname');
$boiler = $(this).attr('data-boiler');
$surcharges = $(this).attr('data-surcharges');
$ccalog = $(this).attr('data-ccalog');
// the 5 below currently populate the INPUT fields for the ID's listed
$('#modpartnerCode').val($partnerCode);
$('#modpartnerName').val($partnerName);
$('#modplatform').val($platform); // <- should go to dropdown
$('#modboiler').val($boiler); // <- should go to dropdown
$('#modsurcharges').val($surcharges); // <- should go to dropdown
});
});
$(document).on("click", ".modAccount", function ()
{
$('#modifyModal').modal('show');
});
As you've probably read in the note above, I can send the attributes to the INPUT fields with their respective IDs.
I need to be able to populate the first OPTION of a dropdown SELECT field.
I don't think I need to show the MODAL window code. As stated, I can populate the INPUT fields using the ID's I listed.
How can I get the IDs to populate the dropdown SELECT first OPTION value?
Here is the code for the modal:
<div class="modal fade" id="modifyModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
</div>
<div class="modal-body">
<form role="form" action="api/modifyAcct.php" method="get" id="modAcctForm" name="modAcctForm">
<input readonly type="text" class="form-control" id="modpartnerName" name="modpartnerName" />
<input readonly type="text" class="form-control" id="modpartnerCode" name="modpartnerCode" />
<select class="form-control" id="modboiler" name="modboiler" />
<option value=""></option> // <- #modboiler goes here
<option value="Y">Yes</option>
<option value="N">No<option>
</select>
<select class="form-control" id="modsurcharges" name="modsurcharges" />
<option value=""></option> // <- #modsurcharges goes here
<option value="Y">Yes</option>
<option value="N">No</option>
</select>
<select class="form-control" id="modplatform" name="modplatform" />
<option value=""></option> // <- #modplatform goes here
<option value="Y"></option>
<option value="N"></option>
</select>
</div>
</div>
</div>
</div>
Use jquery to select the first option and set the value.
$('#modplatform option:first').val($platform);
$('#modboiler option:first').val($boiler);
$('#modsurcharges option:first').val($surcharges);

Categories