I am trying get a list of checkboxes get checked based on two observablearrays. The first one has a few programids and the second one will have all the records of programids. Both the observablearray's data come from the database.
Basically I am trying to get the list of programid already assigned for a user which is in ProgramIDs array and compare it with the array of AllPartnerPrograms array and show the ones that match as checked.
Then I want to be able to check from the new list and send it back to the server to update the user's data with the new list of programid.
I am not sure why the checkedValue binding is not work or I do not understand how to make it work. I created a fiddle with the same code here.
I am assuming that $root.AllPartnerPrograms
will get show the checked items based on the self.ProgramIDs
array but that does not happen.
If I put the code like this, it gets checked but does not show me the other records.
<input type="checkbox" data-bind="checkedValue: ProgramID, checked: ProgramID" />
If I change the foreach to <!-- ko foreach: AllPartnerPrograms -->
then I get the other records but still not check based on the first list.
What am I doing wrong here?
My code
<div id="programs">
<!-- ko foreach: ProgramIDs -->
<input type="checkbox" data-bind="checkedValue: ProgramID, checked: $root.AllPartnerPrograms" />
<span data-bind="text: ProgramName"></span>
<!-- /ko -->
View Model
var objPartnerPrograms;
vmPartnerProgramsModel = function () {
var self = this;
self.ProgramIDs = ko.observableArray(
[{ProgramID: 16002,ProgramName: "Program1"},
{ProgramID: 16003,ProgramName: "Program2"},
{ProgramID: 16005,ProgramName: "Program3"},
{ProgramID: 16006,ProgramName: "Program4"},
{ProgramID: 16011,ProgramName: "Program5"
}]);
self.AllPartnerPrograms = ko.observableArray(
[{ProgramID: 16002,ProgramName: "Program1"},
{ProgramID: 16003,ProgramName: "Program2"},
{ProgramID: 16005,ProgramName: "Program3"},
{ProgramID: 16006,ProgramName: "Program4"},
{ProgramID: 16011,ProgramName: "Program5"},
{ProgramID: 16102,ProgramName: "Program6"},
{ProgramID: 16104,ProgramName: "Program7"
}]);
};
$(document).ready(function () {
objPartnerPrograms = new vmPartnerProgramsModel()
ko.applyBindings(objPartnerPrograms, $("#programs")[0]);
});