嵌套模板

发布于 2024-12-01 15:39:26 字数 1069 浏览 0 评论 0原文

我有以下代码:

function Session(name, index) {
    this.Messages = [];
    this.Name = name;
    this.Index = index;
}

var vm = {};
vm.Sessions = ko.observableArray([new Session("Foo", 0), new Session("Bar", 1)]);

ko.applyBindings();

vm.Sessions()[0].Messages.push("Hello foo");

沿着这个视图:

<div data-bind="template: {name: 'TopTemplate', foreach: Sessions}">
</div>
<script type="text/html" id="TopTemplate">
    <p>
        ${$data.Name}
    <ul data-bind="template: {name: 'NestedTemplate', foreach: Sessions[${$data.Index}]}"></ul>
    </p>
</script>
<script type="text/html" id="NestedTemplate">
    <li>
    ${$data}
    </li>
</script>

正如您所看到的,有一个包含数组的对象。因此,我创建了可观察的会话数组,它变得可观察,包括内部属性。我在这里想要的是显示嵌套的“中继器”。

昨天我以某种方式成功执行了这个脚本。有趣的是,没有显示属性名称,即 Sessions[${$data.Index}].Messages。不幸的是,我删除了那个测试脚本。

现在我尝试重新创建,但没有成功。

附言。问题是我不想在没有显示相关属性的情况下让它工作。我只想让嵌套模板发挥作用。

I have the following code:

function Session(name, index) {
    this.Messages = [];
    this.Name = name;
    this.Index = index;
}

var vm = {};
vm.Sessions = ko.observableArray([new Session("Foo", 0), new Session("Bar", 1)]);

ko.applyBindings();

vm.Sessions()[0].Messages.push("Hello foo");

Along this view:

<div data-bind="template: {name: 'TopTemplate', foreach: Sessions}">
</div>
<script type="text/html" id="TopTemplate">
    <p>
        ${$data.Name}
    <ul data-bind="template: {name: 'NestedTemplate', foreach: Sessions[${$data.Index}]}"></ul>
    </p>
</script>
<script type="text/html" id="NestedTemplate">
    <li>
    ${$data}
    </li>
</script>

As you can see there is an object with the containing array. So I make the observable array of sessions and it becomes observable including the internal properties. What I want here is to display nested 'repeaters'.

Yesterday I somehow suceeded to execute this script. And what is interesting without showing property name, i.e. Sessions[${$data.Index}].Messages. Unfortunately, I deleted that test script.

Now I've tried to recreate and it doesn't work.

PS. The thing is I don't want to make it work witout showing the relevant property. I just want to make work the nested template.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随遇而安 2024-12-08 15:39:26

这似乎更接近您想要完成的任务:

<ul data-bind="template: {name: 'TopTemplate' , foreach: Sessions}"></ul> 
<script type="text/html" id="TopTemplate">
    <li >    
        <p>${name}</p>
        <ul data-bind=" template: {name:  'NestedTemplate' , foreach: $data} " style="list-style-type:circle;margin-left:15px">
        </ul>
    </li> 

</script>
<script type="text/html" id="NestedTemplate">         
{{each(prop, val) $data}}
    {{if $.isArray( val ) }}
        <li>
            <b>${prop}</b>
            <ul  style="list-style-type:square;margin-left:15px" >  
        {{each(index, arrayVal) val}}
            {{each(i, mVal) arrayVal }}
                <li> 
                    <b>${i}</b>  ${mVal}
                </li>
            {{/each}}
        {{/each}}
    {{else}}
    <li><b>${prop}</b> : ${val}</li>
    {{/if}}
{{/each}}     
</script>

代码:

var viewModel = {
    Sessions : ko.observableArray([
        {name:"foo",index: 0, messages:[{body:"Hello foo 1"},{body:"Hello foo 2"}]},
        {name:"bar",index: 1, messages:[{body:"Hello foo 3"},{body:"Hello foo 4"}]}
    ])
};

// ko magic...
ko.applyBindings(viewModel);

另请参阅 this fiddle

This seems closer to what you want to accomplish:

<ul data-bind="template: {name: 'TopTemplate' , foreach: Sessions}"></ul> 
<script type="text/html" id="TopTemplate">
    <li >    
        <p>${name}</p>
        <ul data-bind=" template: {name:  'NestedTemplate' , foreach: $data} " style="list-style-type:circle;margin-left:15px">
        </ul>
    </li> 

</script>
<script type="text/html" id="NestedTemplate">         
{{each(prop, val) $data}}
    {{if $.isArray( val ) }}
        <li>
            <b>${prop}</b>
            <ul  style="list-style-type:square;margin-left:15px" >  
        {{each(index, arrayVal) val}}
            {{each(i, mVal) arrayVal }}
                <li> 
                    <b>${i}</b>  ${mVal}
                </li>
            {{/each}}
        {{/each}}
    {{else}}
    <li><b>${prop}</b> : ${val}</li>
    {{/if}}
{{/each}}     
</script>

And code:

var viewModel = {
    Sessions : ko.observableArray([
        {name:"foo",index: 0, messages:[{body:"Hello foo 1"},{body:"Hello foo 2"}]},
        {name:"bar",index: 1, messages:[{body:"Hello foo 3"},{body:"Hello foo 4"}]}
    ])
};

// ko magic...
ko.applyBindings(viewModel);

See also this fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文