i have an issue with the bootstrap-multiselect binder for knockout. The initial values of my array are not set as selected in dropdown.
Bellow is my index how i use the data-bind:
<table class="table">
<thead>
<tr>
<th>@Resource.Name</th>
<th>@Resource.Group</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody data-bind="foreach: ProjectsCollection">
<tr>
<td><input type='text' class='form-control' data-bind="value: ProjectCenterName"></td>
<td><select class="form-control" multiple="multiple" data-bind="options: $root.CompanyStructures, optionsText: 'CompanyStructureName', optionsValue: 'CompanyStructureId', selectedOptions: $data.ProjectCompanyStructures, multiselect: $root.multiSelectInitOptions"></select></td>
<td class="important">
<i class="fa fa-save" data-bind="click: $root.save"></i>
</td>
<td class="important">
<i class="fa fa-archive"></i>
</td>
<td class="important">
<i class="fa fa-trash"></i>
</td>
</tr>
</tbody>
</table>
I use the binding handler created by David Stutz and my view model looks like this:
function GetProjects() {
// Ajax Call Get All ProjectCenter Records for a specific Company in database
$.ajax({
type: "GET",
url: "/api/ProjectCenterApi/GetProjectsForCompany",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
ko.mapping.fromJS(data.ProjectsCenterModels, {}, self.ProjectsCollection);
self.CompanyStructures(data.CompanyStructures); // Put the response in ObservableArray
},
error: function (error) {
alert(error.status + "<--and--> " + error.statusText);
}
});
// Ends Here
}
I read that 'selectedOptions' need's to be an observable array, this is checked but even so the values are not selected.
The response from the ajax call is the following:
{"ProjectsCenterModels":
[{"ProjectCenterId":1,
"ProjectCenterName":
"PnAweb",
"ProjectCompanyStructures":
[3]},
{"ProjectCenterId":2,
"ProjectCenterName":"Pna old",
"ProjectCompanyStructures":
[3,4]},
{"ProjectCenterId":3,
"ProjectCenterName":"Flint bugs",
"ProjectCompanyStructures":
[]}
],
"CompanyStructures":
[{"CompanyStructureId":3,
"CompanyStructureName":"Not assigned"},
{"CompanyStructureId":4,
"CompanyStructureName":"Home"}
]
}
I have the the projects in a foreach data-bind table and i need to link the projects to company structures. On save the selected company structures are updated and this ok, only the initial values are not displayed! Anyone encountered this situation? Tnx