我正在尝试按如下方式构建表,但出现“错误:找不到要匹配的结束注释标记:ko if: cellIsStartOfRow”。
我做错了吗?
<table>
<tbody data-bind="foreach: MyDocs">
<!-- ko if: cellIsStartOfRow -->
<tr class="docsRow">
<!-- /ko -->
<td>
<!-- There is more databinding in here - a div containing a textarea and also containing a hyperlink surrounding an image. I think the contents are irrelevant to my question, but I can post if someone disagrees.-->
</td>
<!-- ko if: cellIsEndOfRow -->
</ tr>
<!-- /ko -->
</tbody>
</table>
这是视图模型的 JS。上面 td 的内容有些简化,因为我认为里面的内容完全不相关。我从页面上的其他 js 调用函数。视图模型本身被分配给在页面上声明的变量。
Type.registerNamespace("HpDocs");
HpDocs.DocsVM = function (data) {
ko.mapping.fromJS(data, {}, this);
// add additional properties to each document for presentation
// purposes
for (i = 0; i < this.MyDocs().length; i++) {
var myDoc = this.MyDocs()[i];
myDoc.docObjectId = "docObject" + myDoc.Id();
myDoc.textareaId = "ucHpDocs" + "_txta";
if (i % 5 == 0) {
myDoc.cellIsStartOfRow = true;
myDoc.cellIsEndOfRow = false;
} else if (i % 5 == 5) {
myDoc.cellIsStartOfRow = false;
myDoc.cellIsEndOfRow = true;
} else {
myDoc.cellIsStartOfRow = false;
myDoc.cellIsEndOfRow = false;
}
}
};
HpDocs.DocsVM.prototype = {
// cellIsStartOfRow: function(){
// return true;
// },
getDocs: function (filter) {
var self = this;
$.ajax({
url: getMethodUrl("GetDocs"),
data: "{'filter': " + filter + "}",
success: function (response) {
ko.mapping.fromJS(response.d, {}, self.MyDocs);
}
})
}
};
HpDocs.dbGetDocs = function (filter) {
$.ajax({
url: getMethodUrl("DbGetDocs"),
data: "{'filter': " + filter + "}",
success: function (response) {
myDocsViewModel = new HpDocs.DocsVM({
MyDocs: ko.mapping.fromJS(response.d)
});
var bindingScope = $("#divMyDocs")[0];
ko.applyBindings(myDocsViewModel, bindingScope);
$(".DocsUpdateProgress").addClass("invisible");
}
})
};
HpDocs.getPreferredTab = function () {
var tabPref = $("[id$='hidDocTabPreference']").html();
return tabPref;
};
HpDocs.showProgress = function () {
$(".DocsUpdateProgress").removeClass("invisible");
};
HpDocs.hideProgress = function () {
$(".DocsUpdateProgress").addClass("invisible");
};
//register the class
HpDocs.DocsVM.registerClass('HpDocs.DocsVM', null, Sys.IDisposable);
// notify ajax that the script is now loaded.
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
回答
我重构了我的模型:不是让 MyDocs 包含对象列表,而是让它包含一个名为 Rows 的属性,该属性又包含一个名为 Documents 的属性。然后,我可以执行以下操作:
<table id="tblMyDocs">
<tbody data-bind="foreach: MyDocs.Rows">
<tr data-bind="foreach: Documents">
<td>
<!-- in here i present each document by databinding to the Model's properties -->
<td>
</tr>
</tbody>
</table>
当然,视图模型要容易得多,因为模型现在按行组织:
HpDocs.DocsVM = function (data) {
ko.mapping.fromJS(data, {}, this);
};
HpDocs.DocsVM.prototype = {
getDocs: function (filter) {
var self = this;
$.ajax({
url: getMethodUrl("GetDocs"),
data: "{'filter': " + filter + "}",
success: function (response) {
ko.mapping.fromJS(response.d, {}, self.MyDocs);
}
})
}
};
HpDocs.dbGetDocs = function (filter) {
$.ajax({
url: getMethodUrl("DbGetDocs"),
data: "{'filter': " + filter + "}",
success: function (response) {
myDocsViewModel = new HpDocs.DocsVM({
MyDocs: ko.mapping.fromJS(response.d)
});
var bindingScope = $("#divMyDocs")[0];
ko.applyBindings(myDocsViewModel, bindingScope);
HpDocs.hideProgress();
}
})
};