Meteor中的动态循环与嵌套的#each?

时间:2015-10-01 13:41:23

标签: javascript dynamic meteor meteor-blaze spacebars

我有一个由Papaparse从CSV解析为JSON的数据表,我希望在表格中动态显示。数据数组看起来像这样,这是Papaparse新鲜的:

data: {
  0 {
    _id: "",
    Testcol: "cellvalue",
    Testcol2: "cellvalue2"
  },
  1 {
    ...
  }
}

我现在正努力将其变成HTML,因为我不知道这些值,因为它们是动态的,我不能假设任何值的名称。

我尝试了以下内容:

<table class="table table-striped">
    <thead>
        <tr>
            {{#each projectData.meta.fields}}
                <th>{{this}}</th>
            {{/each}}
        </tr>
    </thead>
    <tbody>

        {{#each row in projectData.data}}
            <tr>
                {{#each row}}
                    <td>
                        {{this}}
                    </td>
                {{/each}}
            </tr>
        {{/each}}

    </tbody>
</table>

但是我收到了这个错误:Uncaught Error: {{#each}} currently only accepts arrays, cursors or falsey values.

我该怎么做呢?我希望有人能指出我正确的方向。

1 个答案:

答案 0 :(得分:0)

我实际上现在正在使用它,通过使用帮助器将对象转换为键/值来找到有用的东西。

How to print key and values in Meteor Template?

帮助者:

import SpriteKit  
import GameController  
class GameScene: SKScene, ReactToMotionEvents {  

    override func didMoveToView(view: SKView) {   
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate  
        appDelegate.motionDelegate = self  

    }  

    func motionUpdate(motion: GCMotion) {  
        print("x: \(motion.userAcceleration.x)   y: \(motion.userAcceleration.y)")  
    }  
}  

修改后的#each块:

Template.registerHelper("objectToPairs",function(object){
  return _.map(object, function(value, key) {
    return {
      key: key,
      value: value
    };
  });
});

如果这不是最好的方法,请告知。

相关问题