this is my problem: I have an Array of Array: allAnswers and I want to display its rows in a table. The problem is that I want to pass a value while I'm calling it to set the right row in the right position of the table. I've try this but it still not works.
HTML file in template name= body
<table>
<tr>
<th>Week/Questions</th>
<th>Q1</th>
<th>Q2</th>
<th>Q3</th>
<th>Q4</th>
</tr>
<tr>
<td>Week</td>
<td>{{allAnswers "0"}}</td>
<td>{{allAnswers "1"}}</td>
<td>{{allAnswers "2"}}</td>
<td>{{allAnswers "3"}}</td>
</tr>
</table>
JS File
Template.body.helpers({
allAnswers: function(N) {
var i=N;
for(i=0;i<10;i++){
var allAnswers(i)=[];
for(var j=0; j<4; j++){
allAnswers(i)[j+1] = Quests.find({ userId: this._id, answer: { $exists: true }}).map((el) => el.answer[j]);
alert(allAnswers(i)[j]);
return allAnswer(i)[j];
}
}
}
});
I'd recommend changing tack and using loop helpers in Blaze to build the table like so:
<table>
<tr>
<th>Week/Questions</th>
<th>Q1</th>
<th>Q2</th>
<th>Q3</th>
<th>Q4</th>
</tr>
{{#each week in quests}}
<tr>
<td>Week</td>
{{#each answer in week}}
<td>{{answer}}</td>
{{/each}}
</tr>
{{/each}}
</table>
JS File
Template.body.helpers({
quests() {
return Quests.find({
userId: Meteor.user(),
answer: { $exists: true }
});
}
});
The idea being that your helper returns the data cursor once, and then your template is responsible for structuring that data visually on the page.
The other approach would be to build your two dimensional array in Template.body.onCreated
and then reference the data from there. This would disable any reactivity and so the table won't update when the underlying data changes.
Important to note that helpers are run every time they are referenced in the template as well as every time a reactive data source they use changes (in this case, the Quests collection).
Also a minor note that userId checks should be done in the data publication on the server side. The Meteor tutorial has more information about that.
I also strongly recommend this article if you are getting confused about data flow in Meteor: https://medium.com/meteor-js/data-flow-from-the-database-to-the-ui-three-layers-of-meteor-d5e208b466c3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.