我在我的 JavaScript Metro 应用程序中绑定表格时遇到了问题。与使用模板提供的 HTML 绑定不同,它会添加许多 div 并将 JSON 渲染为字符串。以下是我的代码:
<tr id="tableRow" data-win-control="WinJS.Binding.Template">
<td data-win-bind="innerText: label"></td>
<td data-win-bind="innerText: value"></td>
<td></td>
</tr>
<table>
<thead>
<tr>
<th>col1</th>
<th>col2</th>
<th>col3</th>
</tr>
</thead>
<tbody class="topContent" data-win-control="WinJS.UI.ListView" data-win-options="{ selectionMode: 'none' }"></tbody>
</table>
我正在使用以下javascript代码来绑定数据(topContent是一个包含{ label, value }对象的列表):
function bindContent() {
var list = new WinJS.Binding.List();
topContent.forEach(function (item) {
list.push(item);
});
var listView = document.querySelector(".topContent").winControl;
var template = document.getElementById("tableRow");
listView.layout = new ui.ListLayout();
listView.itemTemplate = template;
listView.itemDataSource = list.dataSource;
}