I currently have a working example pulling data from a JSON response and populating a table however, when i add the checkbox to the results table all of the data that once populated the table is missing. for example when i search for the letter "T" in the first name field it populates the table with all results containing the letter T in the first name. however once i add the all data that once populated no longer appears. when i remove the data bind section it works fine and all data populates and a checkbox appears and can be checked and unchecked just fine. i believe the problem is because i have a foreach running it is searching for the data within the json response but because it cant find any it fails. so how do i go about excluding the checked databind from the foreach if that is the issue.
<tbody data-bind="foreach: policies">
<tr>
<td><input type="checkbox" data-bind="checked: queued" /></td>
<td data-bind="text: policy_number"></td>
<td data-bind="text: policy_type"></td>
<td data-bind="text: contact.first"></td>
<td data-bind="text: contact.last"></td>
<td data-bind="text: contact.street"></td>
<td data-bind="text: contact.city"></td>
<td data-bind="text: contact.state"></td>
<td data-bind="text: contact.zipcode"></td>
<td data-bind="text: contact.phonenumber"></td>
</tr>
</tbody>
</table>
<a data-bind="visible: queued" class="btn btn-lg btn-primary btn-block">Generate</a>
<!--class="clickable" data-bind="click: generate"-->
<script type="text/javascript">
/* global ko, $ */
function Policy(data) {
var self = this;
Object.keys(data).forEach(function(prop) {
self[prop] = data[prop];
});
self.generate = function() {
window.open("{{ url_for('genreport') }}/" + qvm.letter() + '/' + self.id);
}
}
function QueryViewModel(){
var self = this;
self.first = ko.observable('');
self.last = ko.observable('');
self.phone = ko.observable('');
self.letter = ko.observable();
self.letters = {{ letters|safe }};
self.policies = ko.observableArray();
self.queued = ko.observable(false);
self.clear = function() {
self.policies.removeAll();
self.first('');
self.last('');
self.phone('');
}
self.search = function() {
// postJson here
var queryObj = {
first: self.first(),
last: self.last(),
phone: self.phone()
}
$.postJSON("{{ url_for('report_search') }}", queryObj, function(result) {
// first empty our policy table
self.policies([]);
// add results
result.policies.forEach(function(p) {
self.policies.push(new Policy(p));
});
});
}
}
var qvm = new QueryViewModel()
ko.applyBindings(qvm);
</script>
self.policies([]);) - what's so surprising that it is empty? - Tomalakforeach:binding sets the context to the items in thepoliciesarray. So it expects thequeuedproperty on those items, butqueuedis a property on theQueryViewModel- "one level up", so to speak. Use$parent.queuedto go to the parent context inside the body offoreach:. - Tomalak