我在敲除可观察阵列中显示项目时遇到了问题。
下面的代码显示长度为3(所以我知道有元素),但foreach不会显示任何行。
<label data-bind="text: Data().length"></label>
<table>
<tbody data-bind="foreach: Data">
<tr>
<td >woot</td>
</tr>
</tbody>
</table>
的结果
<label data-bind="text: ko.toJSON(Data)"></label>
是:
[ { "Description" : null,
"DeviceId" : "fake1",
"DeviceType" : null,
"Policy" : null
},
{ "Description" : null,
"DeviceId" : "fake2",
"DeviceType" : null,
"Policy" : null
},
{ "Description" : null,
"DeviceId" : "fake3",
"DeviceType" : null,
"Policy" : null
}
]
任何建议非常感谢
答案 0 :(得分:2)
<label data-bind="text: Data().length"></label>
<table>
<tbody data-bind="foreach: Data">
<tr>
<td data-bind="text: DeviceId"></td>
<td>woot</td>
</tr>
</tbody>
</table>
<label data-bind="text: ko.toJSON(Data)"></label>
<script type="text/javascript">
var JSONdata = [ { "Description" : null,
"DeviceId" : "fake1",
"DeviceType" : null,
"Policy" : null
},
{ "Description" : null,
"DeviceId" : "fake2",
"DeviceType" : null,
"Policy" : null
},
{ "Description" : null,
"DeviceId" : "fake3",
"DeviceType" : null,
"Policy" : null
}
];
function ViewModel() {
var self = this;
self.Data = ko.observableArray(JSONdata);
}
ko.applyBindings(new ViewModel());
</script>
答案 1 :(得分:1)
您是否在问题中显示所有您的javascript代码?
无论如何,这是一个简单的工作示例,其中包括用于显示DeviceId的表格单元格;
希望它有所帮助。
http://jsbin.com/UZIDira/2/edit?html,js,output
HTML
<label data-bind="text: Data().length"></label>
<table>
<tbody data-bind="foreach: Data">
<tr>
<td data-bind="text: DeviceId"></td>
<td>woot</td>
</tr>
</tbody>
</table>
<label data-bind="text: ko.toJSON(Data)"></label>
<强>的Javascript 强>
var myJSON = [ { "Description" : null,
"DeviceId" : "fake1",
"DeviceType" : null,
"Policy" : null
},
{ "Description" : null,
"DeviceId" : "fake2",
"DeviceType" : null,
"Policy" : null
},
{ "Description" : null,
"DeviceId" : "fake3",
"DeviceType" : null,
"Policy" : null
}
];
function ViewModel() {
var self = this;
self.Data = ko.observableArray(myJSON);
}
// Activates knockout.js
ko.applyBindings(new ViewModel());