Redirect on select option with meteor - meteor

I need some help with meteor. I've template with select options:
<select id="collapseFour" class="form-control collapse">
{{#each examNum}}
<option>{{this}}</option>
{{/each}}
</select>
And I've helper that helps me in creating list of exam numbers:
Template.adminLayout.helpers({
examNum: function() {
var number = [];
for(var i=1;i<=50;i++){
number[i] = i;
}
return number;
}
});
I need to make page redirect to the specified exam page, when I choose one of the options. Like onchange, or href in a tag element. I know that it can be solved with helpers, so tried this:
Template.adminLayout.helpers({
'change [type=select]': function(e,t){
// page redirect
},
});
But it doesn't work. What can choose the problem, any help will be appreciated!

Already solved, just in case if somebody needs. Here is the template:
<select id="collapseFour" name="examNo" class="form-control collapse">
{{#each examNum}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
Js file:
Template.adminLayout.events({
'change #collapseFour': function(event, template) {
no = template.find('[name=examNo]').value;
Router.go('adminExamPage', {no: no});
}
});
And router.js:
this.route('adminExamPage',{
path: '/coordinator/exam/:no',
layoutTemplate: 'adminLayout'
});

Related

Meteor Dropdown #each over a collection

I've been around this all day and i can't figure why my each cycle is not working. I'm trying to create a dropdown with some countries.
Helper
Template.register.helpers({
countries: function(){
return Country.find({ });
},
});
View, template register
<select id="country-select">
<option disabled="disabled" selected="selected">Please Select</option>
{{#each countries}}
<option value={{ name }}>{{ name }}</option>
{{/each}}
</select>
I have records in the country collection
meteor:PRIMARY> db.country.find({ }).count() ->
4
The only options that the dropwdown displays is the placeholder.
I'm using mongol this is a country record
Try this...
<option disabled selected>Please select</option>
{{#each countries}}
<option>{{name}}</option>
{{/each}}
It works here
Try
return Country.find().fetch()
in the helper
Thanks
Hope it helps
"The solution that worked for me is by calling the 'material_select' function after the options data has been loaded.
Template.[name].rendered = function() { this.autorun(function() { var optionsCursor = OptionsList.find().count(); if(optionsCursor > 0){ $('select').material_select(); } }); };"
from https://github.com/Dogfalo/materialize/issues/1469

Meteor: return ._id of a selected value from dropdown and feed to another dropdown

I'm trying to study Meteor with a simple example to populate two dropdowns where one is a list of fruit and another one is the color according to the fruit chosen:
collection (ProductList):
a = new Array();
a.push({color:"orange"});
a.push({color:"red"});
ProductList.insert({ product: "Orange", a });
a = new Array();
a.push({color:"green"});
a.push({color:"red"});
ProductList.insert({ product: "Apple", a });
a = new Array();
a.push({color:"green"});
a.push({color:"yellow"})
ProductList.insert({ product: "Banana", a });
html (dropdown menu):
<template name="prodlist">
<select id="category-select">
<option disabled="disabled" selected="selected">Please Select</option>
{{#each prodlist}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
</template>
js:
Template.prodlist.events({
"change #category-select": function (event, template) {
var category_prod = $(event.currentTarget).val();
//this return the correct element selected in the dropdown(i.e Orange Apple Banana)
console.log("category : " + category_prod);
var productID = ProductList.findOne({product: category_prod })._id
console.log("current ID: " + productID);
}
});
Please see the ****** above for my first question This has been solved by chrisklaussner. Code updated.
Also I'm trying to feed the ._id (saved to productID) found to the second dropdown:
html:
<template name="colorlist">
<select id="category-select">
<option disabled="disabled" selected="selected">colors</option>
{{#each colorlist}}
<option value="{{this}}">{{this}}</option>
{{/each}}
</select>
</template>
js:
Template.colorlist.helpers({
colorlist: function () {
const ans = productID.get();
const product = ProductList.findOne({ _id: ans});
if (product) {
return product.a.map(function (doc) {
return doc.color;
});
}
}
});
Please see the ****** above for my second question This has been resolved.
EDIT: This question has been resolved and the Original post contains the correct answer
You have a mistake in the findOne calls. The first parameter (where you pass {}) is the selector that specifies what you want to fetch. The second one is for options.
For example, ProductList.findOne({ product: category_prod })._id should give you the right product ID. In your case, the selector {} matches all products in the collection and findOne returns the first of them. That's why you always get the same ID.

UI updates with Meteor.js?

I'm having issues finding a way to update the UI AFTER adding to a collection. So in the example below after you click the button and add to the collection an additional input is added to the DOM. All good, but i'd like to find a way to target the new input element and preferably give it focus in addition to CSS. Unfortunately I can't find any info that helps solve this AFTER the DOM's been updated. Any ideas? Thanks
<body>
{{> myTemplate}}
</body>
<template name="myTemplate">
{{#each myCollection}}
<input type="text" value="{{name}}"><br>
{{/each}}
<br>
<button>Click</button><input type="text" value="test" name="testBox">
</template>
test = new Meteor.Collection("test");
if (Meteor.isClient) {
Template.myTemplate.rendered = function()
{
console.log("rendered");
this.$('input').focus()
}
Template.myTemplate.helpers({
'myCollection' : function(){
var testCollection = test.find({});
console.log("helpers");
return testCollection;
}
});
Template.myTemplate.events({
'click button': function(event){
event.preventDefault();
var val = $('[name="testBox"]').val();
console.log("events");
return test.insert({name: val});
}
});
}
Turn what you're adding into a template and call that template's rendered to set the needed css or do whatever transforms are needed.
HTML:
<body>
{{> myTemplate}}
</body>
<template name="item">
<input type="text" value="{{name}}"><br>
</template>
<template name="myTemplate">
{{#each myCollection}}
{{> item this}}
{{/each}}
<br>
<button>Click</button><input type="text" value="test" name="testBox">
</template>
JS:
test = new Meteor.Collection("test");
if (Meteor.isClient) {
Template.myTemplate.onRendered(function() {
console.log("rendered");
this.$('input').focus()
});
Template.myTemplate.helpers({
'myCollection' : function(){
var testCollection = test.find({});
console.log("helpers");
return testCollection;
}
});
Template.myTemplate.events({
'click button': function(event){
event.preventDefault();
var val = $('[name="testBox"]').val();
console.log("events");
test.insert({name: val});
}
});
Template.item.onRendered(function() {
this.$('input').focus();
}
}
On a side note, you should use onRendered instead of rendered as the latter has been deprecated for the former.
Do it inside of your myCollection helper function. Use jquery to target the last input in your template and focus it, add css. Meteor's template helpers are reactive computations based on the DOMs usage of their reactive variables, so it will run each time the DOM updates based on your collection.

Filter a collection

I have a page which shows all Stories. I have added a filter form where you can select a priority and then the page should only show the stories with that priority. This is the form:
<form name="filter_form">
<select name="prio" class="form-control">
<option value=1>High</option>
<option value=2>Medium</option>
<option value=3>Low</option>
</select>
<button type="submit" class="btn btn-default">Filter</button>
</form>
This is the code to show all stories:
<div class="stories">
{{#each stories}}
{{> storyItem}}
{{/each}}
</div>
And this is the form handler:
Template.storiesList.events({
'submit form': function(e) {
e.preventDefault();
var prio = $(e.target).find('[name=prio]').val();
var stories = Stories.find({prio: prio});
return stories;
}
});
I guess it isn't this easy to just do a find query and return the results, because nothing happens at the moment. What am I doing wrong here?
You need to define stories as a reactive helper. Then you can use a Session variable to link the event to the list:
Template.storiesList.helpers({
'stories': function() {
return Stories.find({prio: Session.get('prio')});
}
});
Template.storiesList.events({
'submit form': function(e) {
e.preventDefault();
var prio = parseInt($(e.target).find('[name=prio]').val());
Session.set('prio'), prio);
}
});

Handlebars how to get sibling value inside an object

ok so iv got an object which looks like this:
Magnetim.photos = {
facebook:{albums: {id:["id1","id2","id3","id4"] , albumName:["n1","n2","n3","n4"]}},
}
inside a handlebars template im doing this line of code:
<select id="albums-selection">
{{#each photos.facebook.albums.albumName}}
<option id="{{photos.facebook.albums.id}}" value="{{this}}">{{this}}</option>
{{/each}}
</select>
this outputs this result:
<option id="id1,id2,id3,id4" value="n1">n1</option>
<option id="id1,id2,id3,id4" value="n2">n2</option>
<option id="id1,id2,id3,id4" value="n4">n3</option>
<option id="id1,id2,id3,id4" value="n4">n4</option>
but what i'm trying to achieve is for every name set the id element to its own id, so it looks like this:
<option id="id1" value="n1">n1</option>
<option id="id2" value="n2">n2</option>
etc..
While you are in the {{#each}} statement handlebars provides you with #index variable to track the current item index. Knowing this, you should be able to get corresponding item from other array.
So first you need to step outside of your {{#each}}, this is done by using ../, which will move you one level up.
My initial aproach was simply to do something like this
{{../albumName.[#index]}}
But for some reason that didn't work out. So I wrote the helper to simply grab item from array by index you pass there:
Handlebars.registerHelper('getAlbumName', function(array, id) {
return array[id];
});
Now your handlebars template will look like this:
<script id="template">
<select id="albums-selection">
{{#each id}}
<option id="{{this}}" value="{{getAlbumName ../albumName #index}}">
{{getAlbumName ../albumName #index}}
</option>
{{/each}}
</select>
</script>
<div id="output"></div>
And here is the js:
var Magnetim = {};
Magnetim.photos = {
facebook: {
albums: {
id: ["id1", "id2", "id3", "id4"],
albumName: ["n1", "n2", "n3", "n4"]
}
}
};
var source = $("#template").html();
var template = Handlebars.compile(source);
Handlebars.registerHelper('getAlbumName', function(albums, id) {
return albums[id];
});
$("#output").html(template(Magnetim.photos.facebook.albums));
Final jsfiddle with everything in place

Resources