2

我正在尝试按如下方式构建表,但出现“错误:找不到要匹配的结束注释标记: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();
        }
    })
};
4

2 回答 2

2

而不是像下面这样向你的 js 添加格式化逻辑:

// 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;
        }

我建议为数据行创建一个单独的视图模型。由于您没有提供任何 json 数据,我无法 100% 解决它,但希望这能让您朝着正确的方向前进。这是我正在使用的 jsfiddle:http: //jsfiddle.net/JasonMore/GcSAn/2/

看法

<table>
    <tbody data-bind="foreach: someArray">
            <tr class="docsRow" data-bind="foreach:docRows">
                <td>
                    <div data-bind="attr:  {id: objectId}">
                       <a data-bind="attr: {href: someUrl}">
                          <img data-bind="attr: {src: IconPath, alt: Tooltip}"/>
                       </a> 
                       <br/>
                       <textarea runat="server" readonly="readonly" data-bind="html: DisplayName"></textarea>

                    </div>
                </td> 
            </ tr>

   </tbody>
</table>​​​​​

Javascript

HpDocs.RowVM = function(data) {
    ko.mapping.fromJS(data, {}, this);

}

HpDocs.DocsVM = function(data) {
    ko.mapping.fromJS(data, {}, this);

    this.docRows = ko.observableArray();

    // 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) {
            // create new RowVM and start adding DocsVM to it
        } else if (i % 5 == 5) {
            // push the RowVM to this.docRows populated with cells
        } else {
            // add the myDoc to the current RowVM you are working with
        }
    }
};

更新-关于如何创建正确的 mvvm 的淘汰赛讨论的链接

幻灯片和示例:http ://bit.ly/FamilyFeudSlides

代码:https ://github.com/jasonmore/familyfeud

于 2012-04-18T16:46:32.287 回答
2

问题出在这里:

<!-- ko if: cellIsStartOfRow --> 
   <tr class="docsRow">
<!-- /ko -->  

你必须在 if 中有打开和关闭标签,所以..

<!-- ko if: cellIsStartOfRow --> 
   <tr class="docsRow">
   ... 
   </tr>
<!-- /ko -->  
于 2014-04-30T15:37:03.217 回答