PROBLEM: I have table with tr that have data from table, When I insert element in Mongo and post it on my page, element added on the bottom of the table
Posts = new Mongo.Collection('posts');
Meteor.publish('allPosts', function(){
return Posts.find({}, { sort: { date: -1 }} );
});
Meteor.subscribe("allPosts");
Posts.insert({
date: 1
})
Posts.insert({
date: 2
})
Posts.insert({
date: 3
})
<table class=" table">
{{#each posts}}
{{> postJobs}}
{{/each}}
</table>
<template name="postJobs">
<tr class="smallInfo">
<td>{{date}}</td>
</tr>
</template>
In DOM I have:
<table>
<tr>
<td>1</td> // must be 3
</tr>
<tr>
<td>2</td> // must be 2
</tr>
<tr>
<td>3</td> // must be 1
</tr>
</table>
All my last inserts must add to the top of my page (table)
EDIT :
My problem with dynamic insert in collection (and i know about -1 gramatik mistake)
EXAMPLE :
Meteor.startup(function(){
Meteor.setTimeout(function(){Test("10");}, 1000);
function Test(x)
{
Posts.insert( {
submitDate: moment().format()
});
}
Meteor.setTimeout(function(){Test("10");}, 10000);
If i sort by submitDate it will show like :
<tr><td>10-10-10</td></tr> // must be 10-10-20
<tr><td>10-10-20</td></tr> // must be 10-10-10
BUT when i refresh my page(F5) all ok
<tr><td>10-10-20</td></tr>
<tr><td>10-10-10</td></tr>
Why do you not sort descending?
{sort: { date: -1 } }
Cheers,
Tom
UPDATE:
You can find a live example at a MeteorPad I prepared:
http://meteorpad.com/pad/Ba5DTe94NjFi3ZTPA/Playground_Flow-Router_Chat
You have to do the sort on the client side not within the publish method.
This is why you get first time sorted but then just as inserted returns.
If you do the sort on client side find() the minimongo will do it on each new document.
Hope this helps for you.
Tom
Related
I have a collection that handles default values for forms. I need to build a UI to update the default values themselves, instead of force updating via mongo backed.
I've used aldeed's update-each functionality. The data is being fetched from the DB and displayed in the table. However, when I try to update by inputting new values in the textbox, it does not persist. In fact, it keeps throwing this error which i'm not aware of.
Exception in template helper: TypeError: Cannot read property 'namedContext' of undefined
at Object.autoFormFieldIsInvalid
As a sample, here is what I'm working with:
Mongo Collection:
meteor:PRIMARY> db.testCollection.find()
{ "_id" : ObjectId("577ccd87f57f43d790c3ec49"), "schemaName" : "test_schema", "label" : "test_lable", "value" : "test_value" }
Schema:
test_schema = new SimpleSchema({
"schemaName": {
type: String,
},
"label": {
type: String,
},
"value": {
type: String,
}
});
testCollection.attachSchema(test_schema);
Template:
<template name = "testTemplate">
<table class="table table-bordered table-condensed">
<thead>
<tr>
<td style="width: 85px">Schema Name</td>
<td style="width: 85px">Label</td>
<td>Default Value</td>
<td style="width: 250px">New Default Value</td>
</tr>
</thead>
<tbody>
{{#each items}}
<tr>
<td>{{this.schemaName}}</td>
<td>{{this.label}}</td>
<td>{{this.value}}</td>
<td>
{{#autoForm id=updateDefaiultsID type="update" collection=testCollection doc=this autosave=true}}
{{> afFormGroup name="value" label=false}}
{{/autoForm}}
</td>
</tr>
{{/each}}
</tbody>
</table>
</template>
Helper
import { Template } from 'meteor/templating';
import '../templates/testTemplate.html';
if (Meteor.isServer) {
Meteor.publish(null, function () {
return testCollection.find();
});
testCollection.allow({
update: function () {
return true;
}
});
}
else if (Meteor.isClient) {
Template["testTemplate"].helpers({
items: function () {
return testCollection.find({}, {sort: {name: 1}});
},
updateDefaiultsID: function () {
return "testTemplate" + this._id;
}
});
}
Change this
from
<td>{{this.schemaName}}</td>
to
<td>{{this.schema_name}}</td>
I am rendering table dynamically.
Templates are defined as below:
<template name="home">
<div class="row">
<div class="tblCSV">
<table class="table table-bordered table-hover table-striped">
<thead>
// Dynamic Header Row - Working
<tr>
{{#each columns}}
<th>
{{columnName}}
</th>
{{/each}}
</tr>
</thead>
// Dynamic Rows - Working
<tbody>
{{#each entries}}
{{> entry}}
{{/each}}
</tbody>
</table>
</div>
</div>
</template>
// if i try to iterate columns to print values per row, it prints field/column name instead it's value
<template name="entry">
<tr>
{{#each columns}}
// columnName is a mongodb field name, Not Working
<td>{{columnName}}</td> // This prints text of Column name itself instead its value
{{/each}}
</tr>
</template>
// If i use static column names, it prints all the column values perfectly
<template name="entry">
<tr>
<td>{{MyColumnName1}}</td> //This prints value of column name i.e. MyColumnValue
<td>{{MyColumnName2}}</td>
<td>{{MyColumnName3}}</td>
<td>{{MyColumnName4}}</td>
</tr>
</template>
Template Script
Template.home.helpers({
entries: function () {
return Entry.find();
},
columns: function () {
return Column.find();
}
});
Template.entry.helpers({
columns: function () {
return Column.find();
}
});
This is what the problem is
This is what i want
Feel free to ask for any information you need.
I'm extremely new to Meteor and haven't been able to find the answer to something that's probably laughably simple.
I have a Meteor collection called Ingredients:
Ingredients = new Mongo.Collections("ingredients");
if (Meteor.isClient) {
Template.body.helpers({
ingredients: function() {
return Ingredients.find({});
});
}
Which is populated with documents like the following:
{ name: Boneless Pork Chop,
tags: [Paleo, Pork, Local] }
Right now I'm rendering the name in a template, as follows:
<template name="ingredient">
<tr>
<td>{{name}}</td>
</tr>
</template>
What I need to figure out now is how to also render the individual elements of the 'tags' array in that template. Preferably, I'd like to render them in such a way that later I could assign a click event to each of them so they could be individually removed or edited... which from my earlier reading means I might want to put the tags in their own collection and join them to the Ingredients documents by an ID, which is perfectly find with me if that's a better pattern.
Little help? Thanks!
you can use #each to print an array in meteor.
<template name="ingredient">
<tr>
<td>{{name}}</td>
<td>
{{#each tags}}
{{this}}
{{/each}}
</td>
</tr>
</template>
Here are the templates in question:
<template name="tables">
<div class="table-area">
{{#each tableList}}
{{> tableBox}}
{{/each}}
</div>
</template>
<template name="tableBox">
<table id="{{name}}" class="table table-condensed table-striped">
<tr>
<td>{{name}}</td>
<td> Min:</td>
<td>{{minBet}}</td>
<td>{{cPlayers}}</td>
</tr>
<tr>
<td>Dice1</td>
<td>Dice2</td>
<td>Dice3</td>
<td>Total</td>
</tr>
{{#each table [{{name}}]}}
{{> tableRow}}
{{/each}}
</table>
</template>
<template name="tableRow">
<tr>
<td>{{Roll.dice1}}</td>
<td>{{Roll.dice2}}</td>
<td>{{Roll.dice3}}</td>
<td>{{Roll.total}}</td>
</tr>
</template>
And here are the Meteor Handlebars functions:
Template.tables.tableList = function(){
return Tables.find();
}
Template.tableBox.table = function(tableID){
return Rolls.find({tableName: tableID});
}
The problem is each table that is rendered on screen has all the 'Rolls' listed (All 100 lines). Instead of being filtered for the parameter I am trying to pass through to the Roll template function which is the table name {{name}}.
In the "table id" tag of TableBox, the {{name}} gets converted correctly. i.e. "T1", "T2", "T3" etc. But it's that same TableID that i need to pass to the function to filter from the db query properly. Is there a way to do this more simply? I would like to stick to the handlebars templating way of doing things here if possible.
For reference below is the JSON initialisation code for the test data:
//initiate tables
for (i = 1; i < 11; i++) {
Tables.insert({
name: 'T' + i,
minBet: '300',
cPlayers: '(8)'
});
}
//initiate rolls within tables
for (i = 1; i < 11; i++) {
for(j=1; j<11; j++){
var die1 = ((Math.floor(Math.random() * 5) +1).toString());
var die2 = ((Math.floor(Math.random() * 5) +1).toString());
var die3 = ((Math.floor(Math.random() * 5) +1).toString());
var t = (parseInt(die1) + parseInt(die2) + parseInt(die3)).toString();
Rolls.insert({
Roll: {
tableName: 'T' + i,
rollNumber: j;
dice1: die1,
dice2: die2,
dice3: die3,
total: t
},
});
}
}
Ok - After trial and error - figured it out:
In the helper function:
Template.tableBox.table = function(tableID){
return Rolls.find({"Roll.tableName": tableID});
}
I needed to add the nested Roll.tableName property name but within brackets as the query.
And back in the tableBox template:
<template name="tableBox">
<table id="{{name}}" class="table table-condensed table-striped">
<tr>
<td>{{name}}</td>
<td> Min:</td>
<td>{{minBet}}</td>
<td>{{cPlayers}}</td>
</tr>
<tr>
<td>Dice1</td>
<td>Dice2</td>
<td>Dice3</td>
<td>Total</td>
</tr>
{{#each table name}}
{{> tableRow}}
{{/each}}
</table>
</template>
No need for the curly braces for the 'Name' argument for the function. Somehow handlebars and Meteor recognise the context you are referring to without the curly braces and applies it like it is within {{name}} for the tableBox. I.e. "T1", "T2", "T3" are passed correctly to the function and now my unique tables only contain the list of rolls specific to individual tables.
I have a table structure as follows. Now I need to sort these nested tables separately. Forexample: sorting chapter's row will only update chapters order in a separate table. Whereas, sorting items will update their order in another table.
I managed to setup the code and sorting. However, when I drag the items from chapter 4, it pass on the order of the items in from chapter 1 since they come before chapter 4???
Could someone help me with sorting only relevant items??
NOTE: This list is dynamic coming from database. So I am interested in one jquery code covering all the ordering bits.
<table id=subsortsortable>
<tbody class=content>
<tr id="chapter_1"><td>Chapter one</td></tr>
<tr id="chapter_2"><td>Chapter two</td></tr>
<tr id="chapter_3">
<td>
<table>
<tbody class=subcontent>
<tr id="item_31"><td>three.one</td></tr>
<tr id="item_32"><td>three.two</td></tr>
</tbody>
</table>
</td>
</tr>
<tr id="chapter_4">
<td>
<table>
<tbody class=subcontent>
<tr id="item_41"><td>four.one</td></tr>
<tr id="item_42"><td>four.two</td></tr>
<tr id="item_43"><td>four.three</td></tr>
<tr id="item_44"><td>four.four</td></tr>
<tr id="item_45"><td>four.five</td></tr>
</tbody>
</table>
</td>
</tr>
<tr id="chapter_4"><td>Chapter Four</td></tr>
</tbody>
</table>
The code I am using is as follows:
//for sorting chapters - which is outer table
$("#subsortable tbody.content").sortable({
opacity: 0.7,
cursor: 'move',
placeholder: "ui-state-highlight",
forcePlaceholderSize: true,
update: function(){
var order = $('#subsortable tbody.content').sortable('serialize') + '&action=updateChaptersOrder';
$.post("/admin/ajax/ajax_calls.php", order, function(theResponse){
});
}
});
// For sorting and updating items within a specific chapter - which is nested tbody
$("tbody.sortItems").subcontent({
opacity: 0.7,
cursor: 'move',
placeholder: "ui-state-highlight",
forcePlaceholderSize: true,
update: function(){
var order = $('tbody.subcontent').sortable('serialize');// + '&action=updateListings';
$.post("/admin/ajax/ajax_calls.php", order, function(theResponse){
});
}
});
I have got the answer to my own question.. In case someone else encounter the same problem. I have changed the following code inside the internal table:
var order = $('tbody.subcontent').sortable('serialize');
to
var order = $(this).sortable('serialize');