Basically i have two questions which are related but i'll separate them with numbers 1) I am trying to load a single field into a select dropdown box from collection but its populating with all repetitive values it collected from the list below it instead of its own helper.
<template name="carsList">
{{> categoryFilter}}
<ul class="collection" id="listings">
{{#each cars}}
<li>
{{> carItem}}
</li>
{{/each}}
</ul>
</template>
category template is
<template name="categoryFilter">
<div class="input-field">
<select>
<option value="" disabled selected>Choose your option</option>
{{#each companyCategories}}
{{> companyCategory}}
{{/each}}
</select>
<label>Select Category</label>
</div>
</template>
<template name="companyCategory">
<option>{{ccategory}}</option>
</template>
categoryFilter is holding the dropdown template for which iam using the below helper
Template.categoryFilter.helpers({
companyCategories: function(){
return Cars.find();
}
});
instead of populating from its own helper its loading the data that is coming from "listings" below it and repeating data.
Image of the result in selectbox
2) I also want to filter the listings based on the value selected in the select dropdown box (Ofcourse reactive)
Please help
EDIT
This is how my template looks now
<template name="categoryFilter">
<div class="input-field">
<select>
<option value="" disabled selected>Choose your option</option>
{{#each companyCategories}}
{{> companyCategory}}
{{/each}}
</select>
<label>Select Category</label>
</div>
</template>
<template name="companyCategory">
<option>{{justCategory}}</option>
</template>
Here is how I would proceed. First, the dropdown: you should use a cursor instead of an array of documents. So your helper should be:
and your
categoryFilter
template HTML could be something like thisYou can then create an event to record the current category selected in the dropdown when it is clicked
Now, you can filter your cars using the
selectedCategory
reactive variable. For this, you need another helper. It could be for instance:That should do the trick. I never use Session, so it might not work as I suppose it does. If it does not work, use ReactiveDict instead. Basically, you add
var pageSession = new ReactiveDict();
at the beginning of your doc and you can replaceSession
withpageSession
in my code.EDIT
Ok, let's have a try with underscore. Based on this answer, here is what you can do to return all the distinct categories for every item of allJobs:
where
myfield
is your categories array field.EDIT 2
Try to change your html and replace
{{justCategory}}
with{{this}}
: