您没有存储复选框的值,因此每次都会清除它。下面的小提琴能解决你的问题吗?
http://jsfiddle.net/unklefolk/RYqTT/
<table>
<thead>
<th class="checkbox_th"/>
<th class="name_th" align="left">Sample</th>
<th class="status_th" align="left">Status</th>
</thead>
<tbody data-bind="template: {name: 'sampleRowTemplate', foreach: samples}"></tbody>
<script type="text/x-jquery-tmpl" id="sampleRowTemplate">
<tr>
<td><input type="checkbox" name="select_sample" data-bind="checked: is_checked"/></td>
<td><span data-bind='text: sample_name'/> The checkbox =<span data-bind="text:is_checked"/></td>
<td><span data-bind='text: ""'/><td/>
</tr>
{{each sample_runs()}}
<tr>
<td/>
<td><span data-bind='text: $value.name'/></td>
<td><span data-bind='text: $value.status'/></td>
</tr>
{{/each}}
<!--
<script type="text/x-jquery-tmpl" id="sampleRunTemplate">
<td/>
<td>${name}</td>
<td>${status}</td>
</script>
<tr data-bind="template: {name: 'sampleRunTemplate', foreach: sample_runs }"></tr>
-->
</script>
</table>
var mapping = {
'samples': {
key: function(item) {
return ko.utils.unwrapObservable(item.id);
},
'sample_runs': {
key: function(item) {
return ko.utils.unwrapObservable(item.id);
}
}
}
};
var data = {};
data.samples = [{
id: "s1",
sample_name: "AR008",
is_checked: false,
sample_runs: [
{
id: "rs1",
name: "run1",
status: "done"}]}];
var viewModel = ko.mapping.fromJS(data, mapping);
ko.applyBindings(viewModel);
// update the data every 2 seconds
var i = 0;
setInterval(function() {
var data = {};
data.samples = [{
id: "s1",
sample_name: "AR008" + i,
sample_runs: [
{
id: "rs1",
name: "run" + i,
status: "done" + i}]}];
// is this right? updateFromJS is deprecated
viewModel = ko.mapping.fromJS(data, viewModel);
i++;
}, 2000);