I'm using KnockoutJS and durandalJS 2.0. I'm dynamically adding two drop-downs based on the database table entries and populating them with data form another table. Each drop-down also has a check-box. How do I get the selected drop-down values?
models
var dataToAdd = {
mydata_Id: ko.observable(),
mydata_Name: ko.observable(),
mydata_data: ko.observableArray([dataTask])
};
var dataTask = {
taskId: ko.observable(),
dropdownId: ko.observable()
};
var taskList=ko.observableArray([]);//get data from database table. Consider there are two entries.
var dropdownData=ko.observableArray([]); //get the dropdown data
View
//Since *taskList* has two entries, two dropdowns with their respective checkbox will get generated.
<div data-bind="foreach:taskList">
<label><input type="checkbox" data-bind="checked: true" />
<span data-bind="text:Name"></span></label>
<select data-bind="options: $root.DropdownData, optionsValue: 'Id', optionsText: 'Name', optionsCaption: 'Select...', value: $root.dataTask.dropdownId</select>
</div>
First: How do I get the selected value for each drop-down when I hit the ADD
button? When I use the value: $root.dataTask.dropdownId
, both of the drop-downs get changed to the same selected value. When I check the checkbox, dropdown should be enabled and after selecting, I should be able to update a observable
Array like below:
{taskId:44,dropdownId:10},{taskId:55,dropdownId:11}
Second: Also, when I uncheck the check-box, the respective drop-down should get disabled and the entry should be reomoved from the observable
Array.