Select2 is Not Working In bootstrap 4 and laravel - php

Select2 is not working. I don't see any effect. I need to apply "Multi-select boxes (pillbox)" style From Select2.
I am using bootstrap 4. I think this is the problem.
What I have tried so far is showed below,
PHP
<div class="form-group" >
<label for="exampleFormControlInput">Tags</label>
<select multiple class="form-control tags"> > <!-- Multiple To Select more than one but need press ctrl+select -->
#foreach ($tags as $tagname)
<option>{{$tagname->tag}}</option>
#endforeach
</select>
Script
<script type="text/javascript">
${(function() {
$('.tags').select2();
});
</script>

First make sure you have imported the css and the javascript file
This is my view, I used select2 in my vendor.
<label>Vendor <span style="color: red">*</span></label>
<select class="form-control" name="vendor_id" required="required" id="vendor">
<option value="">Select Vendor</option>
#if($vendors)
#foreach($vendors as $key => $value)
<option value="{{ $value->id }}">{{ $value->name }}</option>
#endforeach
#endif
</select>
As you can see I declared an id because I will use it in my select2. See my code below how I implement the select2 in my vendor.
$('#vendor').select2({
placeholder: "Select Vendor" // you can change this
});
And if you want some event with that select2 here my code.
$(document).on('select2:select', '#vendor', function(e){
var id = $(this).val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
})
$.ajax({
url: '/purchases/purchase-orders/getVendorTerms',
type: "POST",
data: {id:id},
success: function(response) {
if (response != '') {
$('#terms').val(response.term);
}else{
$('#terms').val('');
}
editGetGoodThru();
}
});
})

Select2 depends on the order of js included, try including them in order below.
Like jQuery ->jQuery UI -> Bootstrap -> Select2.

Related

I'm having a weird problem with my Ajax code. How can I solve this?

When I click on all of my edit buttons the first time then it populates into the city field and area field nicely. But when I clicked on the edit button a second time the city field does not populate. After refreshing its populate again with the same problem. I need help.
City field code:
<select class="form-control" id="selectCity">
<option value="0" selected disabled>--- Select City ---</option>
#foreach ($cities as $city)
<option value="{{ $city->id }}">{{ $city->city_name }}</option>
#endforeach
</select>
My Ajax code:
$(document).on('click', '#edit_area', function(e) {
e.preventDefault();
$('#addArea').hide();
$('#updateArea').show();
var area_id = $(this).val();
$.ajax({
type: "GET",
url: "/edit-area/" + area_id,
success: function(response) {
if (response.status == 400) {
//some staff goes here
} else {
$('#area_id').val(area_id);
$('#areaName').val(response.area[0].area_name);
$('#selectCity').find("option:contains(" + response.area[0].city_name + ")").attr('selected', 'selected');//this works more better
}
}
});
});

How to create filter for options value using ajax in php and zf2

How to make filter using ajax i want to get single option value for each selection and when click on filter button it sends data to getproductlistcb controller.
please help
HTML CODE is-
<div class="filter-category">
<form>
<input type="submit" value="filter" class="btn filter-btn">
<div class="user-category-select">
<select required="" name="sort" form="filter-form" class="btn filter-form-select" >
<option value="" disabled="">Sort</option>
<option value="popularity" selected="">Most Popular</option>
<option value="highest-rated">Highest Rated</option>
<option value="newest">Newest</option>
<option id="aesc" value="price-low-to-high">Lowest Price</option>
<option id="desc" value="price-high-to-low">Highest Price</option>
</select>
<label class="filter-category-select"><i class="fa fa-angle-down" aria-hidden="true"></i></label>
</div>
</form>
</div>
AjaX-
<script type="text/javascript">
$(document).ready(function(){
$('.btn filter-form-select').on('change',function(){
var data=$('#search_form');
$.ajax({
type: "POST",
url: "<?php echo $this->url('page', array('controller' => 'page', 'action' => 'getproductlistcb')); ?>",
data:data,
success:function(data){
console.log(data);
alert(data);
$("#").html(data);
}
});
});
});
</script>
Did you try to use :option in selector? For example, in that way: $('select.filter-form-select option:selected').val();?
I think you are looking for DOM-selector like that:
$('.filter-form-select option:selected').
So, in your case, in your code the code below might be helpful:
var $formData = $('#search_form');
var $formFilters = $formData.find('select.filter-form-select');
var $firstFilter = $formFilters.first();
var $option = $firstFilter.find('option:selected');
var optionText = $option.length ? $option.text() : null;
var optionValue = $option.length ? $option.val() : null;
Be aware, that $formFilters is not just one element, it's jQuery object that represents a set of DOM elements.
An additional way, try to debug and check what you have in your this-context and event:
$('.btn filter-form-select').on('change',function(event) {
console.log('clicked: ' + event.target.nodeName);
console.log('Context: ' + this);
});
I hope, something from that will help you a bit.

I want text type to be stored in database instead of the id Ajax Laravel 7

I am new to Ajax. So, excuse my lack of knowledge. When I pass the data in the view, I get IDs in my database. Even though I passed text from a dropdown dependent menu is that possible with explanation!
Please see those 2 tables category
and products
I wanted to get the names from category ('Désignation' => first table) in my view - but instead I got the IDs. Please help.
controller
public function create() {
$prod=ProductCat::all();
return view('Achats.create',compact('prod'));
}
public function findProductName(Request $request) {
$data = Product::select('Désignation','id') -> where('catégorie', $request->id) -> take(100) -> get();
return response()->json($data);
}
public function findRef(Request $request) {
$p = Product::select('Référence')->where('id',$request->id)->first();
return response()->json($p);
}
Routes
Route::get('create','AchatController#create')->name('create.Achat');
Route::get('/findProductName','AchatController#findProductName');
Route::get('/findRef','AchatController#findRef');
View
<form action="{{ route('Achat.store')}}" method="POST" enctype="multipart/form-data">
<span>Product Category: </span>
<select style="width: 200px"class="form-control catégorie" name="catégorie" id="catégorie" >
<option value=" " disabled="true" selected="true">-Select-</option>
#foreach($prod as $cat)
<option value="{{$cat->id}}">{{$cat->Désignation}}</option>
#endforeach
</select>
<span>Product Name: </span>
<select name ="Désignation" class="form-control Désignation" id="Désignation" style="width: 200px" >
<option value="" disabled="true" selected="true">Product Name</option>
</select>
<span>Product ref: </span>
<input name="Référence" id="Référence" class="form-control Référence" type="text" >
<br>
ajax
<script type="text/javascript">
$(document).ready(function(){
$(document).on('change','.catégorie',function(){
// console.log("hmm its change");
var cat_id=$(this).val();
// console.log(cat_id);
var div=$(this).parent();
var op=" ";
$.ajax({
type:'get',
url:'{!!URL::to('findProductName')!!}',
data:{'id':cat_id},
success:function(data){
//console.log('success');
//console.log(data);
//console.log(data.length);
op+='<option value="0" selected disabled>chose product</option>';
for(var i=0;i<data.length;i++){
op+='<option value="'+data[i].id+'">'+data[i].Désignation+'</option>';
}
div.find('.Désignation').html(" ");
div.find('.Désignation').append(op);
},
error:function(){
}
});
});
$(document).on('change','.Désignation',function () {
var prod_id=$(this).val();
var a=$(this).parent();
console.log(prod_id);
var op="";
$.ajax({
type:'get',
url:'{!!URL::to('findRef')!!}',
data:{'id':prod_id},
dataType:'json',//return data will be json
success:function(data){
console.log("Référence");
console.log(data.Référence);
// here price is column name in products table data.coln name
a.find('.Référence').val(data.Référence);
},
error:function(){
}
});
});
});
It looks like you are passing the Id as the value, hence the reason for getting the Id in your database table.
Change
#foreach($prod as $cat)
<option value="{{$cat->id}}">{{$cat->Désignation}}</option>
#endforeach
To
#foreach($prod as $cat)
<option value="{{$cat->Désignation}}">{{$cat->Désignation}}</option>
#endforeach
That should help.

Ajax send same value but different options in dropdown list

I have a users' data table and im trying to change role of them by populating roles from database to datatable. I use ajax to change the role and it works. but problem is value of selecting option from dropdown is not sent to database.
Herewith I attached my controller function view and ajax code.
Please advise me to solve this.
Controller:
public function changeRole(Request $request){
$id = Input::get('id');
$isAdmin = Input::get('isAdmin');
User::findOrFail($id)->update(['isAdmin'=>$isAdmin]);
}
Ajax :
$(document).ready(function(){
$('select').on('change' , function (event){
id = $(this).data('id');
isAdmin = $('#role').val();
//alert(role);
$.ajax({
type: 'POST',
url: SITE_URL + '/changeRole/',
data: {
'_token': $('input[name=_token]').val(),
'id': id,
'isAdmin': isAdmin
},
cache: false,
success: function(data) {
toastr.success('Admin Role Successfully Changed ! ', 'Congratulations', {timeOut: 5000});
//$('#example1').DataTable().ajax.reload();
},
error: function (jqXHR, exception) {
console.log(jqXHR);
toastr.error('Something Error !', 'Status not Changed!')
},
});
});
});
View :
<td>
<form method="post">
{{ csrf_field() }}
<div class="form-group">
<select class="form-control" data-id="{{$user->id}}" id="role" name="role">
<option value="0">Select a Role</option>
#foreach ($roles as $role)
<option #if($user->isAdmin==$role->id) selected #endif value="{{ $role->id }}">{{ $role->name }}</option>
#endforeach
</select>
</div>
</form>
</td>
Please see the user table view
First of all don't use Input::get('id') use $request->id and $request->isAdmin
Try dd($request->all()) at the very first line of you changeRole() method and check if you are getting the correct values every time.
Don't use $('select') give your select some id and use it as $('#myselect')
add var before you variable in jquery like var id=$() to keep it in local scope.
for reference must read this : https://www.tutorialspoint.com/What-is-the-difference-between-global-and-local-Variables-in-JavaScript
Don't use var id = $(this).data('id'); the .data() messes things up sometimes.
Use it as
var id = $(this).attr('data-id');
To get the value of selected roles use following:
var isAdmin = $(this).children("option:selected").val();
Hope this fixes many things.

yii dropdownlist change jquery works only in create

i have form with render partial in yii which make update and create
i write this jquery code to check success only :
$("#AmakenPlaces_place_main_category").change(function()
{
alert("hi");
}
it works good to me in create but in update nothing do
i think this error because i get select value from database
can any one help me .. iam tired to solve this problem
note: i use this code for main categories and sub categories for my site
my html in update
<div class="controls col-sm-6">
<select class="form-control" name="AmakenPlaces[place_main_category]" id="AmakenPlaces_place_main_category">
<option value="">Select main category</option>
<option value="1" selected="selected">cafe and resturants</option>
<option value="2">men and wommen</option>
<option value="3">hoho</option>
</select>
</div>
my code in javascript
<script type="text/javascript">
$(document).ready(function()
{
$("#AmakenPlaces_place_main_category").change(function()
{
var id=$(this).val();
$.ajax
({
type: "POST",
url: '<?php echo yii::app()->createAbsoluteUrl("site/subcategory");?>',
data: "id="+id,
dataType: 'json',
cache: false,
success:function(data,textStatus,jqXHR)
{
$("#AmakenPlaces_place_sub_category").html('');
$("#AmakenPlaces_place_sub_category").append('<option selected="selected">--Select Sub category--</option>');
$.each(data.town,function(i,v)
{
$("#AmakenPlaces_place_sub_category").append('<option value='+v.id+'>'+v.name+'</option>');
});
},
});
});
});
</script>
thanks all for attention
my solve to this problem when i delete
$(document).ready(function()
{
}

Categories