Meteor Blaze迭代动态创建的对象

时间:2018-01-18 10:37:11

标签: node.js meteor meteor-blaze

我正在尝试使用Blaze迭代动态创建的对象。我有集合和对象,但我无法在表格中显示数据,我不明白为什么。

这是我检索对象的代码:

在我的模板助手中:

fields: ()=> {
    var collectionSelected = FlowRouter.current().params.collectionName;

    // var data = Mongo.Collection.get(collectionSelected).find().count();


    var collectionObject = Collections.findOne({name: collectionSelected});

    console.log(Collections.findOne({name: collectionSelected}));

    return Collections.findOne({name: collectionSelected});
},

values: ()=> {

    var collectionSelected = FlowRouter.getParam('collectionName');
    var collectionObject = Collections.findOne({name: collectionSelected});
    var dataArray = [];

    var data = Meteor.call('findElmt', collectionSelected, function(err, res){
        console.log('VALUES : ');
        console.log(res);
        return res;
    });

},

findElmt方法:

findElmt(collectionName){
    if (global.hasOwnProperty(collectionName)){
        var res = Mongo.Collection.get(collectionName).find().fetch();

        console.log(res);
        return res;
    }

    else {
        global[collectionName] = new Meteor.Collection(collectionName);

        var res = Mongo.Collection.get(collectionName).find().fetch();
        console.log(res);
        return res;
    }

}

最后我试图用Blaze显示它的方式:

<table data-toggle="table" data-show-columns="true" data-search="true" data-pagination="true">
    {{#with fields}}
        {{#if Template.subscriptionsReady}}
            <thead>
                <tr>
                {{#each fields.fieldsName}}
                    <th data-sortable="true">{{this}}</th>
                {{/each}}
                </tr>
            </thead>
        {{else}}
            <p>Loading...</p>
        {{/if}}
    {{/with}}
    <tbody>
        {{#each values}}
            <tr>
            {{#each valueString in values.items}}
                <td>{{valueString}}</td>
            {{/each}}
            </tr>
        {{/each}}
    </tbody>
</table>

如果您对我做错了什么有任何建议,谢谢!

编辑1:我添加了字段助手!

2 个答案:

答案 0 :(得分:0)

假设values是一个带有字段items的JsonObject数组,那么您正在以错误的方式迭代。 使用{{#each}}而不是{{#each in}}时,您可以使用itemsthis.items直接访问您的媒体资源。

如果values是JsonObject的数组:

<tbody>
    {{#each values}}
        <tr>
        {{#each valueString in this.items}} // values.items -> this.items
            <td>{{valueString}}</td>
        {{/each}}
        </tr>
    {{/each}}
</tbody>

如果values是JsonObject,则可以使用with

<tbody>
    {{#with values}}
        <tr>
        {{#each valueString in this.items}} // values.items -> this.items
            <td>{{valueString}}</td>
        {{/each}}
        </tr>
    {{/with}}
</tbody>

然后,要使用Method调用获得响应返回值,您可以使用以下包: https://atmospherejs.com/simple/reactive-method

答案 1 :(得分:0)

非常感谢你的帮助,我终于把它钉了。

这里是像我这样未来迷失灵魂的最终代码!

模板助手:

fields: ()=> {
    var collectionSelected = FlowRouter.current().params.collectionName;

    // var data = Mongo.Collection.get(collectionSelected).find().count();


    var collectionObject = Collections.findOne({name: collectionSelected});

    console.log(Collections.findOne({name: collectionSelected}));

    return Collections.findOne({name: collectionSelected});
},

values: ()=> {

    var collectionSelected = FlowRouter.getParam('collectionName');
    var collectionObject = Collections.findOne({name: collectionSelected});
    var dataArray = [];

    console.log(ReactiveMethod.call("findElmt", collectionSelected));

    return ReactiveMethod.call("findElmt", collectionSelected);
}

服务器端FindElmt方法没有改变, 最后是Blaze代码:

<table data-toggle="table" data-show-columns="true" data-search="true" data-pagination="true">
                    {{#with fields}}
                        {{#if Template.subscriptionsReady}}
                            <thead>
                                <tr>
                                {{#each fields.fieldsName}}
                                    <th data-sortable="true">{{this}}</th>
                                {{else}}
                                    {{! will this fix this issue? }}
                                {{/each}}
                                </tr>
                            </thead>
                        {{else}}
                            <p>Loading...</p>
                        {{/if}}
                    {{else}}
                        {{! will this fix this issue? }}
                    {{/with}}

                    {{#with values}}
                        <tbody>
                        {{#each values}}
                            <tr>
                            {{#each valueString in this.items}}
                                <td>{{valueString}}</td>
                            {{else}}
                                {{! will this fix this issue? }}
                            {{/each}}
                            </tr>
                        {{else}}
                            {{! will this fix this issue? }}
                        {{/each}}
                        </tbody>
                    {{else}}
                        {{! will this fix this issue? }}
                    {{/with}}

                </table>