简体   繁体   English

Knockoutjs div 绑定无法按预期工作

[英]Knockoutjs div binding not working as expected

I'm trying to bind some dynamic values to a div but for some reason the div doesn't fetch the data properly.我正在尝试将一些动态值绑定到 div,但由于某种原因,div 无法正确获取数据。

This is what I have:这就是我所拥有的:

HTML: HTML:

            <div class="section-content tabcontent sc7" id="gridEventLimits" style="padding: 0; background-color: #fff; display: none">
                <div style="clear: both">
                </div>
                <table>
                    <tr>
                        <td></td>
                        <td></td>
                        <td colspan="4"></td>
                    </tr>
                    <tr>

                      <table class="sgrid" data-bind="visible: skills && skills.length > 0"
                            style="width: 100%; border-collapse: collapse; border: solid 1px #aaa">
                            <thead>
                                <tr style="background: rgb(242, 242, 225); color: #333">
                                    <td>Event Skills
                                    </td>
                                </tr>
                            </thead>
                            <tbody data-bind="foreach: skills">
                                <tr>
                                    <td>
                                        <ul class="collapsible" data-bind="attr: { id: 'collapsible' + Id }">
                                            <li><span data-bind="text: Name" style="color: #365474"></span>

                                            </li>
                                        </ul>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                        <p data-bind="visible: !skills || skills.length == 0" style="text-align: center">
                            -- No People Found --
                        </p>

                    </tr>
                </table>
            </div>

Then I have js function which is called on page load event:然后我有在页面加载事件上调用的 js 函数:

var skillPeopleList;
function ApplyKOBindingsToSkillPeopleDetails() {
    if (eventId > 0) {
        var element = $('#gridEventLimits')[0];
        skillPeopleList = new SkillPeopleListModel(eventId);
        ko.applyBindings(skillPeopleList, element);
    }
}

function SkillPeopleListModel(id) {
    var self = this;
    self.Id = id;
    self.skills = ko.observableArray([]);

    //initialize
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/webservices/EventScheduleService.asmx/GetEventSkills",
        data: "{'eventId':" + self.Id + "}",
        dataType: "json",
        async: true,
        success: function (data) {
            result = data.d;

            if (result) {
                //if (result.skills) {
                //  result.skills.forEach(function (entry) {
                    result.forEach(function (entry) {
                        self.skills.push(entry);
                    });
                //}
            }
        },
        error: function (data, status, error) {
        }
    });
}

The content of the web service response (result object) is this one: Web 服务响应(结果对象)的内容是这样的:

在此处输入图片说明

Any idea what am I doing wrong?知道我做错了什么吗? I'm new with Knockoutjs and I'm still learning the framework.我是 Knockoutjs 的新手,我仍在学习该框架。

Changing the bindings to将绑定更改为

skills && skills().length > 0

And

!skills() || skills().length == 0

Will fix it.会修好。 Skills is an observableArray, so skills.length will cause an error and break the other bindings. Skills 是一个 observableArray,所以 Skills.length 会导致错误并破坏其他绑定。 Unwrapping the observable and then checking length will fix it.展开 observable 然后检查长度将修复它。

As a side note, this kind of logic would be better placed inside the view-model, so you can keep your model-view and view-model separate.作为旁注,这种逻辑最好放在视图模型中,这样您就可以将模型视图和视图模型分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM