I am trying to list the options for a select tag from server with the help of knockout options binding. I have a PHP page which returns the JSON data which is pushed to a knockout observable array which is binded to the select tag. But somehow it is not working, please refer to the following code for reference:
HTML:
<div class="form-group">
<select class="form-control">
<option data-bind="options: Country_Names, optionsText: 'country_name'"></option>
</select>
</div>
JavaScript:
$(document).ready(function(){
function appModel(session_info){
/* Session Info related bindings are commented as they are working fine */
var self = this;
this.Country_Names = ko.observableArray();
// Bindings related to the batch processing
$(function(){
$.ajax({
url:"../api/master_list.php",
type:"get",
data:{mastertype: '1'},
cache:false,
success:function(country_list){
ko.mapping.fromJSON(country_list, {}, self.Country_Names);
}
});
});
};
$.ajax({
url:"../api/sessions.php",
type:"get",
data: {rqtype: '1'},
cache:false,
success:function(session_info){
var data = $.parseJSON(session_info);
if (data.status == 'Invalid_id'){
window.location.replace('../files/main.html');
} else {
ko.applyBindings(new appModel(session_info));
}
}
});
});
Sample JSON:
[{"country_name":"Albania"},{"country_name":"Chile"},{"country_name":"Cuba"}]
Question, Why are the options not listed in the select tag? Am i missing something obvious?
You are data-binding the option element, whereas you want to put the bindings on the select, like this:
<select class="form-control" data-bind="options: Country_Names, optionsText: 'country_name'">
</select>
The <option...> element is gone; the options will be generated by Knockout.
Note that the relevant documentation is very clear on how all this works. If you haven't already, I suggest (re-)reading it.
Or, see this full demo:
var data = [{"country_name":"Albania"},{"country_name":"Chile"},{"country_name":"Cuba"}];
ko.applyBindings({ Country_Names: data });
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout.mapping/2.4.1/knockout.mapping.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<select data-bind="options: Country_Names, optionsText: 'country_name'"></select>
Related
Not sure what I am doing wrong here but the on change function is not hitting for some reason. I hope someone here can point me in the right direction.
I have a view that has some bootstrap tabs, inside one of those tabs I have a dropdown list. What I want to happen is, when the user selects a year, it make an ajax call to controller, gets the json data and populates a table with the data. Here is my code:
In the View:
<div id="StatementsTab" class="tab-pane fade">
<div class="text-left dash-left-padding dash-right-col-content-billing">
<select class="form-control edi-blue" id="ddlStatements">
<option value="0">VIEW STATEMENTS</option>
<option value="#DateTime.Now.Year">#DateTime.Now.Year STATEMENTS</option>
<option value="#DateTime.Now.AddYears(-1).Year">#DateTime.Now.AddYears(-1).Year STATEMENTS</option>
<option value="#DateTime.Now.AddYears(-2).Year">#DateTime.Now.AddYears(-2).Year STATEMENTS</option>
<option value="#DateTime.Now.AddYears(-3).Year">#DateTime.Now.AddYears(-3).Year STATEMENTS</option>
<option value="#DateTime.Now.AddYears(-4).Year">#DateTime.Now.AddYears(-4).Year STATEMENTS</option>
<option value="#DateTime.Now.AddYears(-5).Year">#DateTime.Now.AddYears(-5).Year STATEMENTS</option>
</select>
<br />
<table id="statementtbl">
<tr>
<td></td>
</tr>
</table>
and the jquery:
$("#ddlStatements").change(function () {
console.log("change function yes");
var yr = $("#ddlStatements").val();
if (yr > 0){
$.ajax({
//var url = '../LabOrder/DeletePatientNoteAttachment?PatientNotes=' + JSON.stringify(yr);
url: '..Members/GetStatements',
data: 'Year=' + yr, // Send value of the drop down change of option
dataType: 'json', // Choosing a JSON datatype
success: function (data) {
console.log("success");
// Variable data contains the data you get from the action method
},
error: function (ex) {
console.log(ex);
}
});
}
});
I am not sure why but the console log is not being hit. Please help...
I had to display data on textbox on change of select box but it will not work & also control not going to ajax url.
HTMl code:
<select class="form-control" name="product_title" id="product_title">
#foreach($product_title as $id => $p_title )
<option value="{{ $id }}">{{ $p_title }}</option>
#endforeach
</select>
<input type="text" class="form-control" name="product_price" id="product_price">
<input type="text" class="form-control" name="product_quantity" id="product_quantity">
Js:
<script>
$(document).ready(function(){
$("#product_title").change(function(){
e.preventDefault();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
jQuery.ajax({
url: "{{ url('/productdetails') }}",
method: 'get',
data: {
id: jQuery('#product_title').val()
},
success: function(result){
jQuery('#product_price').html(result.product_price);
jQuery('#product_quantity').html(result.product_quantity);
}});
});
});
</script>
route:
Route::get('/productdetails', 'ProductController#getProduct');
public function getProduct(Request $req)
{
$product = product::find($req->id)->first(); // Product is the model
//dd($product);
if ($product)
{
return response()->json(['product_price' => $product->product_price,'product_quantity' => $product->product_quantity]);
}
}
Database table name is product with attribute id,product_title,product_price,quantity.When i select title value from select box related value product_price,quantity from database will display in textbox.
Looks like issue is on success function lines. html is not useful for assign value to text. it is used to echo html. instead you can use val() like following.
jQuery('#product_price').val(result.product_price);
jQuery('#product_quantity').val(result.product_quantity);
Hope it helps. happy coding.
you can use simple without jquery like as follow
<select>
foreach($result as $key => $value)
{
echo "<option value=".$your_id_variable.">".$your_display_variable."</option>";
}
So in my 'edit records' view I have this two dropdrown the first one contains the main category and the second one should contain values based from the main category chosen is the first dropdown. So I implemented an ajax call for this in my view, here is how the dropdown code looks like
<select name="category" id="category" class="dds">
#foreach($mCategories as $lists)
<option value="{{$lists->maincategoryid}}">{{$lists->maincategoryname}}</option>
#endforeach
</select>
<h6>Doc SubClass 1</h6>
<select name="subcategory" id="subcategory" class="dds">
<option value=""></option>
</select>
then I have this in my script to call the ajax function
<script>
$('#category').on('change' , function(e){
console.log(e);
var cat_id = e.target.value;
alert(cat_id);
//ajax
$.getJSON('ajax-subcat?cat_id=' + cat_id , function(data){
console.log(data);
$('#subcategory').empty();
$.each(data, function(index, subcatObj)
{
$('#subcategory').append('<option value="' + subcatObj.Object.subcategoryid +'">' + subcatObj.subcategoryname +'</option>');
});
});
});
</script>
and the code for the ajax call is in my routes and here is how it looks
Route::get('ajax-subcat' ,function()
{
$cat_id = Input::get('cat_id');
$subcategories = DB::table('nsa_subcategory')
->where('maincategoryid' , '=' , $cat_id)
->get();
return Response::json($subcategories);
});
as you can see, in my script I tried to log the data after the $getJSON to check whether or not it went in but I am not getting anything. The thing is I also used this codes in my creating of records so i don't know what my error is now. any ideas? thanks in advance!
We have two drop downs. Based on the first dropdown event changed the second drop down values are getting populated.
How to change appearance of the 2nd type drop down such that it should be different for both enabled and disabled state?
In both drop down they look like enabled but until we select the country the states not loaded any way .. The select CSS was behaving as excepted in IE but not in Chrome. It was looking as enabled in Chrome.
//***********//
button,
input,
optgroup,
select,
textarea {
margin: 0;
font: inherit;
color: inherit;
}
When I removed the Color inherit from boot strap It makes the difference.How to override the style sheet which effecs the color to the state dropdown in the customized CSS
<div class="item1" id="countries">
<div class="selectbox">
<select data-bind="options: Countries,
optionsText: $data,
optionsValue: $data,
value: SelectedCountry,
event: { change: $parent.CountryActionChanged }">
</select>
</div>
</div>
<div class="item2" id="states">
<div class="selectbox">
<select data-bind="options: States,
optionsText: 'Name',
optionsValue: 'Id',
value: SelectedStateType,
event: { change: $parent.StateActionChanged },
enable: $parent.IsStateType"
>
</select>
</div>
</div>
You haven't provided a full repro. When I tried to stub out the things you omitted, I ran in to various problems. So I've decided to tell you how you could do things slightly different, in a way that will also solve the enable binding issue. Here it is:
var ViewModel = function() {
var self = this;
self.countries = [
{ name: "US", states: ["NY", "CA", "etc"] },
{ name: "Monaco", states: [] },
{ name: "Vatican", states: [] }
// etc
];
self.selectedCountry = ko.observable();
self.selectedState = ko.observable();
self.availableStates = ko.computed(function() {
if (!!self.selectedCountry()) {
return self.selectedCountry().states;
}
return [];
});
self.selectedCountryHasStates = ko.computed(function() {
return self.availableStates().length > 0;
});
};
ko.applyBindings(new ViewModel());
select { min-width: 15em; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div class="item1" id="countries">
<div class="selectbox">
<select data-bind="
options: countries,
optionsText: 'name',
value: selectedCountry">
</select>
</div>
</div>
<div class="item2" id="states">
<div class="selectbox">
<select data-bind="
options: availableStates,
value: selectedState,
enable: selectedCountryHasStates">
</select>
</div>
</div>
Things to note:
Don't use the event binding unless you really, really have to. Instead, use computed functions so your view models change dynamically based on the selections made by users.
(Subjective) Reserve PascalCase for constructor functions, and use camelCase for members and variables.
Base the enable status on whether there are states or not. Do this in a seperate computed so you can unit test its logic.
Try to bind dropdowns to complex object (e.g. countries themselves), this makes your view models use the OO features of constructor functions / javascript in a way that makes your code a lot more readable. It also slims down your data-bind attributes a lot.
I load values for select2 like the following way.
Declare the Type
var AdjustmentType = Backbone.Model.extend({
url : Hexgen.getContextPath("/referencedata/adjustmenttype")
});
create instance for the Type
var adjustmentTypes = new AdjustmentType();
load the values to select2 box
adjustmentTypes.fetch({
success : function() {
for(var count in adjustmentTypes.attributes) {
$("#adjustment-type").append("<option>" + adjustmentTypes.attributes[count] + "</option>");
}
}
});
$("#adjustment-type").select2({
placeholder: "Select Adjustment Type",
allowClear: true
});
My HTML Code
<div class="span4">
<div>ADJUSTMENT TYPE</div>
<select id="adjustment-type" tabindex="5" style="width:200px;">
<option value=""></option>
</select>
</div>
when i load this for the first it is not giving any exception but if i Refresh or navigate to different URL i get the following exception:
Uncaught query function not defined for Select2 adjustment-type
"Query" refers to the list by which to check your search terms against. You need to make sure that your data property is a proper array of objects (ie. your options elements).