【问题标题】:Displaying nested data with blaze用 blaze 显示嵌套数据
【发布时间】:2016-07-24 04:52:16
【问题描述】:

我已经在我的 minimongo 中插入了这个嵌套数据

db.orders.insert({ 
    _id: ObjectId().str,
    name: "admin",
    status: "online",catalog : [{
        "objectid" : ObjectId().str,
        "message" : "sold",
        "status" : "open"
    }]
});

我正在尝试使用此代码显示它

<template name="Listed">
    <div class="row">
        {{#each list}}
            <article class="post">
                <a href="{{pathFor route='edit'}}"><h3>{{_id}}</h3></a>
                <a href="{{pathFor route='edit'}}"><h3>{{name}}</h3></a>
                <br>
                <a href="{{pathFor route='create'}}"><h3>{{status}}</h3></a>
                <br>
                {{#each ../catalog}}
                    <a href="{{pathFor route='create'}}"><h3></h3></a>
                    <a href="{{pathFor route='create'}}"><h3>{{status}}</h3></a>
                {{/each}}
                <div class="well"></div>
                <br/>    
            </article>
            <br/><br/>
        {{/each}}
    </div>
</template>

但未显示嵌套数据。如何显示嵌套数据?

这是我的数据助手

/*****************************************************************************/
/* Listed: Helpers */
/*****************************************************************************/
Template.Listed.helpers({
    'list': function(){
        return Orders.find();
    }
});

【问题讨论】:

  • 如何返回数据?你为什么写../catalog?
  • 我已经更新了问题。

标签: meteor handlebars.js meteor-blaze


【解决方案1】:

你需要删除../

<template name="Listed">
    <div class="row">
        {{#each list}}
            <article class="post">
                <a href="{{pathFor route='edit'}}"><h3>{{_id}}</h3></a>
                <a href="{{pathFor route='edit'}}"><h3>{{name}}</h3></a>
                <br>
                <a href="{{pathFor route='create'}}"><h3>{{status}}</h3></a>
                <br>
                {{#each catalog  }}
                    <a href="{{pathFor route='create'}}"><h3></h3></a>
                    <a href="{{pathFor route='create'}}"><h3>{{status}}</h3></a>
                {{/each}}
                <div class="well"></div>
                <br/>    
            </article>
            <br/><br/>
        {{/each}}
    </div>
</template>

```

【讨论】:

  • 这是我拥有的嵌套对象{ "objectid" : ObjectId().str, "message" : "sold", "status" : "open" }。有没有办法可以访问嵌套对象的键名和值,即我会显示键“状态”和值“打开”。我可以通过 {{ status}} 显示状态值,但我如何显示密钥。我这样说是因为,有时,我不知道钥匙是什么。
  • @LeQs 尝试使用{{@index}}访问对象的键
  • @rdk1992 一个小例子会有所帮助。
  • @LeQs JBoulhous 提供的示例很好,您刚刚提到您缺少键:尝试{{#each catalog}} Key: {{@index}} Status: {{status}} {{/each}} 只需将 {{@index}} 放在您要显示对象键的位置。
  • @rdk1992 感谢您的意见。更清楚一点,catalog 对象就是我所拥有的。我想打印出catalog 对象下的键/值对。在真正的应用程序中,我不会知道键/值对。
猜你喜欢
  • 1970-01-01
  • 2019-01-06
  • 2023-01-28
  • 2013-01-27
  • 2018-11-28
  • 2022-08-03
  • 1970-01-01
  • 2021-12-30
  • 2019-09-24
相关资源
最近更新 更多