[SOLVED] Meteor Category - Sub Category Menu (Coding Help)


#1

Hi All,

I am new to Meteor but acquired some fair knowledge of this framework. I am creating one App in which I have to Build a Category Management Module, I am using a Category collection for this and in the document my values are like this

{
_id:
name:
parentID:


}

I have tried few things to make it recursive, but failed to do it, what I need is a drop down which contains all the categories with their children. like the sample below:

I would appreciate if anyone here can help in this issue:

Right Now what I am doing is fetching me to only 2 levels, I mean Top Parent and a Sub Child, I want unlimited levels for this, I know it might be possible through recursive function, but unable to find the way

Template:

<template name="categoryselect">

<select id="category" name="category" class="category">
<option value="">--Select--</option>
{{#each get_categories}}
<option value="{{_id}}">{{name}}</option>
{{#each get_sub_categories}}
{{> subcategoryselect}}
{{/each}}
{{/each}}

</select>

</template>


<template name="subcategoryselect">
<option value="{{_id}}">--{{name}}</option>
</template>

Template Helpers :

Template.categoryselect.helpers({
'get_categories': function(){
    return Categories.find({parentID:''});

},

'get_sub_categories': function(){
    return Categories.find({parentID:this._id});
 }

});

Thanks and Regards,
Manu


Nested Menu Component
#2

How do you store the categories in your database?


#3

Hi,

Thanks for your reply,

I store it like this, here parentID contains the _id of its parent, and it is blank if there is no parent associated with it.

Please refer below

{
_id:
name:
parentID: // _id of the parent and blank in case there is no parent
}

Thanks and Regards,
Manu


#4

Here is the official manual: http://docs.mongodb.org/manual/tutorial/model-tree-structures which shows simple queries on how to get the right queries. Gives you a good overview of the options.

What the best solution is can only be answered with more input. Like how many categories are we talking about, how many times are they being changed, etcetera.

Also are you sure you need nested categories? Recently also tagging becomes quite popular to use instead of nested categories.

Alternative approach:

You might consider to put them all together in one JSON object:

Anyway what I think is that the best way to store it is a nested object. That looks to me like the quickest and most simple approach. You could even generate that from the data model you have now.

Here you can see a very simple handlebars approach to show that tree model:


#5

Hi Luc,

Thanks for your detailed information, It has helped me to understand few things that I was not aware, I was aware of the creating of the tree using the <ul> <li> , but I was looking for some select menu option that can help me to generate the Menu with Categories and Sub Categories.

But anyways I would like to share with you that I have asked the same Question on StackOverflow and has got the right Answer, I hope that it will help others too.

Here is the Link : http://stackoverflow.com/questions/32930732/meteor-category-and-subcategory-select-menu

Thanks again for your help.
Manu