i use jquery to append option to select
it's worked but i want add
<option value="0">choose category</option>
after any selected
when i add it in html it's hidden after select option apeend
<script type="text/javascript">
$(document).ready(function() {
$('select[name="cat"]').on('change', function() {
$('select[name="subcat"]').removeClass('hidden');
$('select[name="subcat2"]').addClass('hidden');
//$('select[name="subcat2"]').find('option').remove().end();
var stateID = $(this).val();
if(stateID) {
$.ajax({
url: '{{ url('getCat') }}/'+stateID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="subcat"]').empty();
$.each(data, function(key, value) {
$('select[name="subcat"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('select[name="subcat"]').empty();
}
});
});
That's because you are calling empty function before appending. So if you have hard coded option <option value="0">choose category</option> it will be removed before new options are added.
success:function(data) {
$('select[name="subcat"]').empty();
$('select[name="subcat"]').append('<option value="0">choose category</option>');
$.each(data, function(key, value) {
$('select[name="subcat"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
I made a simulation of your problem, see if that's what you want?
Link jsfiddle
html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select name="cat">
<option value="0">choose category</option>
<option value="1">Cat 1</option>
<option value="2">Cat 2</option>
</select>
<select name="subcat" style="display:none">
</select>
JS
$(document).ready(function() {
$('select[name="cat"]').on('change', function() {
//$('select[name="subcat"]').removeClass('hidden');
//$('select[name="subcat2"]').addClass('hidden');
//$('select[name="subcat2"]').find('option').remove().end();
var stateID = $(this).val();
if(stateID) {
$.ajax({
data: {
json: JSON.stringify({
object: {
1: 'Sub cat 1',
2: 'Sub cat 2',
3: 'Sub cat 3',
}
}),
delay: 1
},
url: '/echo/json/',
type: "POST",
dataType: "json",
success:function(data) {
$('select[name="subcat"]').empty();
$('select[name="subcat"]').append('<option value="0">choose category</option>');
$.each(data.object, function(key, value) {
$('select[name="subcat"]').append('<option value="'+ key +'">'+ value +'</option>');
});
$('select[name=subcat]').show();
}
});
}else{
$('select[name="subcat"]').empty();
}
});
});
Related
I have two selects, the 1st filled by ouvrages and other by chantiers there is between them belongsTo relation, I want when I choose chantier in 1st select the 2nd filled by ouvrages corresponds, but in my case it gives me empty data.
mois.blade.php
//select chantiers
<select class="form-control" id="chantier_id">
<option selected disabled>Select Location</option>
#foreach($chantiers as $chantier)
<option value="{{ $chantier->id }}">{{ $chantier->chantier}}</option>
#endforeach
</select>
//select ouvrages
<select class="form-control" id="ouvrage_id">
<option value="0" selected disabled>- Select -</option>
</select>
jQuery
<script type="text/javascript">
$(document).ready(function() {
$('#example').DataTable();
});
$(document).ready(function(){
$("#chantier_id").change(function(){
var chantier = $(this).val();
$.ajax({
url: "{{ route('salarie.select') }}",
type: 'post',
data: {
chantier:chantier,
"_token": "{{ csrf_token() }}"
},
dataType: 'json',
success:function(response){
console.log(response);
var len = response.length;
$("#ouvrage_id").empty();
for( var i = 0; i<len; i++){
var id = response[i]['id'];
var ouvrage = response[i]['ouvrage'];
$("#ouvrage_id").append("<option value='"+id+"'>"+ouvrage+"</option>");
}
}
});
});
});
</script>
SalarieController.php
public function select(request $request){
$ouvrages = Ouvrage::where('chantier_id',$request->chantier_id);
return response()->json(['options'=>$ouvrages]);
}
web.php
Route::post('/select','SalarieController#select')->name('salarie.select');
Route::get('/mois','SalarieController#mois');
You need jQuery.parse(response) to parse json
and change loop to $.each to make it easy and less complex.
<script type="text/javascript">
$(document).ready(function() {
$('#example').DataTable();
$("#chantier_id").change(function(){
var chantier = $(this).val();
$.ajax({
url: "{{ route('salarie.select') }}",
type: 'post',
data: {
chantier:chantier,
"_token": "{{ csrf_token() }}"
},
dataType: 'json',
success:function(response){
console.log(jQuery.parse(response));
$("#ouvrage_id").empty();
$.each( jQuery.parse(response), function( key, value ) {
$("#ouvrage_id").append("<option value='"+key+"'>"+value+"</option>");
});
}
});
});
});
</script>
I have three tables like tbl_customers, tbl_items, tbl_customersOrders. I inserted the tbl_customers ID and tbl_items ID into the tbl_customersOrders which as a foreign key. Now think is based on first drop-down lists (here the tbl_customersOrders ID) I can fetch another two fields on other drop-down lists but .... I need the name of the customers which is id matched...also items name which is id matched Like:
what I tried:
Controller Code:
$data['gets_order'] = $this->box_property_model->getCustDataOrder();
Model Code:
public function getCustDataOrder()
{
$this->db->select('*');
$this->db->order_by('customer_orders_id', 'DESC');
$query = $this->db->get('tbl_customer_orders');
return $query->result();
}
View code:
<select name="nestedOrder" class="form-control" >
<option value="">--- Select Order ---</option>
<?php if(!empty($gets_order)) {
foreach($gets_order as $nst){
?>
<option value="<?php echo $nst->customer_orders_id; ?>"><?php echo $nst->new_id_generated; ?></option>';
<?php } } ?>
</select>
<label for="custNested">Customer Name-</label>
<select name="custNested" id="custNested" class="form-control" >
<option value="">Select ITEMS ID</option>
</select>
<label for="ItemsName">Items Name</label>
<select name="items_ids" id="getsitemsName" class="form-control" >
<option value="">Select ITEMS</option>
</select>
<label for="testing">Final ITEM Name</label>
<select name="org_itemsNameValue" id="org_itemsNameValue" class="form-control" >
<option value="">Select ITEMS Name</option>
</select>
JS CODE:
<script type="text/javascript">
$(document).ready(function() {
$('select[name="nestedOrder"]').on('change', function() {
var customID = $(this).val();
alert(customID);
$.ajax({
url: '<?php echo base_url();?>/Box_property/DropdownsAjaxNested/'+customID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="custNested"]').empty();
$('select[name="custNested"]').append('<option value="0">--Select CUSTTT--</option>');
$.each(data, function(key, value) {
$('select[name="custNested"]').append('<option value="'+value.customer_id + '">'+ value.customer_name +'</option>');
});
}
});
$.ajax({
url: '<?php echo base_url();?>/Box_property/DropdownsAjaxNestedItems/'+customID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="items_ids"]').empty();
$('select[name="items_ids"]').append('<option value="0">--Select --</option>');
$.each(data, function(key, value) {
$('select[name="items_ids"]').append('<option value="'+value.items_id +'">'+ value.items_id +'</option>');
});
}
});
});
$('select[name="items_ids"]').on('change', function() {
var items_ids = $(this).val();
alert(items_ids);
$.ajax({
url: '<?php echo base_url();?>/Box_property/itemsOrdersIDfetch/'+items_ids,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="org_itemsNameValue"]').empty();
$('select[name="org_itemsNameValue"]').append('<option value="0">--Select ITEMS Name--</option>');
$.each(data, function(key, value) {
$('select[name="org_itemsNameValue"]').append('<option value="'+value.items_id + '">'+ value.items_name +'</option>');
});
}
});
});
});
</script>
In Controller:
public function DropdownsAjaxNested($id) {
$result = $this->db->where("customer_id",$id)->get("tbl_customers")->result();
echo json_encode($result);
}
public function DropdownsAjaxNestedItems($id) {
$result = $this->db->where("customer_orders_id",$id)->get("tbl_items_order")->result();
echo json_encode($result);
}
public function itemsOrdersIDfetch($id){
$result = $this->db->where("items_id",$id)->get("tbl_items_master")->result();
echo json_encode($result);
}
how can i use a set_select option on ajax part for a dynamic dependent dropdown list, this list is clearing after validation errors, so i want to use set_select option here. kindly see below code:
<script type="text/javascript">
$(document).ready(function() {
$('select[name="relegion"]').on('change', function() {
var regID = $(this).val();
if(regID) {
document.write("ok");
$.ajax({
url: '/demo/main/selectcaste/'+regID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="caste"]').empty();
$('select[name="caste"]').append('<option value=1>'+ "Not Interested to specify" +'</option>');
$('select[name="caste"]').append('<option value=2>'+ "InterCaste" +'</option>');
$.each(data, function(key, value) {
$('select[name="caste"]').append('<option value="'+ value.id +'">'+ value.name +'</option>');
});
}
});
}else{
$('select[name="caste"]').empty();
}
});
});
Maybe you need something like this,
View code.
<select name="category" id="category">
<option>Pilih Kategori Kelas</option>
<option value="1">category 1</option>
<option value="2">category 2</option>
</select>
<select name="sub_category" id="sub_category">
<option>Pilih category Kelas</option>
</select>
AJAX code.
$(document).ready(function(){
$('#category').on('change',function(){
var category_id = $(this).val();
if(category_id){
$.ajax({
type:'POST',
url:'<?php echo base_url()."profile_kelas/get_sub_category";?>',
data: {
category_id : category_id
},
success:function(html){
$('#sub_category').html(html);
}
});
}else{
$('#sub_category').html('<option>Pilih Sub category Kelas</option>');
}
});
});
My Controller code.
public function get_sub_category(){
$data = $this->model_profile->get_sub_category($_POST['category_id']);
foreach ($data as $row) {
echo '<option value="'.$row->id.'">'.$row->sub_category.'</option>';
}
}
You can make your parent category dynamically.
I have this code select state -> show state cities :
<div class="form-group">
{{ Form::label('state', 'State') }}
<select name="state" id="state" class="form-control">
<option value="">{{ __('Select Province') }}</option>
#foreach ($state->data as $info)
<option value="{{$info->province_id}}">{{$info->province}}</option>
#endforeach
</select>
</div>
<div class="form-group">
{{ Form::label('city', 'City') }}
<select class="form-control" name="city" id="city">
<option value="">{{ __('Select City') }}</option>
</select>
</div>
and this javascript:
<!-- find cities -->
<script type="text/javascript">
$(document).ready(function() {
$('select[name="state"]').on('change', function() {
var provinceID = $(this).val();
if(provinceID) {
$.ajax({
url: '{{ url('admin/getcitylistshipping') }}/'+encodeURI(provinceID),
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="city"]').empty().append("<option value='' selected>Select</option>");
$.each(data.data, function(key, value) {
$('select[name="city"]').append('<option value="'+ value['city_name'] +'">'+ value['type'] + ' - ' + value['city_name'] +'</option>');
});
}
});
}else{
$('select[name="city"]').empty().append("<option value='' selected>Select</option>");
}
});
});
</script>
What I want is to save options text in my database instead of their
value (which is ID).
what I did
As my cities list comes by json and nothing depended on them I changed my values in my script from value="'+ value['city_id'] + to value="'+ value['city_name'] + and I get my city names instead of their id's.
But my issue is with my states (provinces) part, in that part I need to have state id as value in order to return city names but i don't want to save that id in my database, what i want is to save option name.
Here is my store method:
$this->validate($request, array(
'state' => 'nullable',
));
$shipping->state = $request->input('state');
how can I save my selected state text instead of their id in my
controller?
ideas?
SOLVED
I added extra hidden input to my blade and changed my script to:
html
<div name="statehidden" id="statehidden"></div>
script
<script type="text/javascript">
$(document).ready(function() {
$('select[name="state"]').on('change', function() {
var provinceID = $(this).val();
var statename = $("select[name='state'] option:selected").text(); //add this
if(provinceID) {
$.ajax({
url: '{{ url('admin/getcitylistshipping') }}/'+encodeURI(provinceID),
type: "GET",
dataType: "json",
success:function(data) {
$('#statehidden').empty(); //add this
$('#statehidden').append("<input type='text' name='statehidden' id='statehidden' value='"+statename+"'>"); //add this
$('select[name="city"]').empty().append("<option value='' selected>Select</option>");
$.each(data.data, function(key, value) {
$('select[name="city"]').append('<option value="'+ value['city_name'] +'">'+ value['type'] + ' - ' + value['city_name'] +'</option>');
});
}
});
}else{
$('#statehidden').empty(); //add this
$('select[name="city"]').empty().append("<option value='' selected>Select</option>");
}
});
});
</script>
I have scenario form like this
if option selected it will come out a new field i named "hah" i call it using ajax script like this
$('#bahan').change(function() {
var id = $('#bahan').val()
var datana = 'id=' + id
var urlna = "<?=base_url()?>controller/food/getdetailstok";
$.ajax({
type: 'POST',
url: urlna,
data: datana,
success: function(data) {
$('#hah').html(data);
}
})
return false;
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<select class="form-control select2" name="bahan" id="bahan">
<option value="">-- Choose One --</option>
<option value="1"> 1 </option>
<option value="2"> 2 </option>
</select>
</div>
<div id="hah"></div>
How to reset "hah" if i click button save. hah in reset because the value field options again become null
$('#hah').empty() will do the trick.
.empty() will remove every element from the DOM. In your case every element in your #hah div.
$('#bahan').change(function() {
$('#hah').html("");
var id = $('#bahan').val()
var datana = 'id=' + id
var urlna = "<?=base_url()?>controller/food/getdetailstok";
$.ajax({
type: 'POST',
url: urlna,
data: datana,
success: function(data) {
$('#hah').html(data);
}
})
return false;
})