Search code examples
meteor

Group subcategory in category


How can I group subcategory in category that they belongs to? For example category is ICT and I want only subcategory (such as laptop,komputer,etc.) that belongs to ICT display in the dropdown.

addItem.html

 <template name="addItem">
    <div class="form-group">
                <select class="form-control" name="category_name">
                    <option selected> --Category-- </option>
                    {{#each category}}
                        {{> downCategory}}
                    {{/each}}
                </select>   
                </div>
            
                <div class="form-group">
                <select class="form-control" name="subcategory_name">
                    <option selected> --Subcategory-- </option>
                    {{#each subcategory}}
                        {{> downSubcategory}}
                    {{/each}}
                </select>   
                </div>
    </template>


<template name="downCategory">
    <option id="{{_id}}">{{category_name}} </option>
</template>

<template name="downSubcategory">
    <option id="{{_id}}">{{subcategory_name}} </option>
</template>

addItem.js

Template.addItem.helpers({
    category: function(){
        return Category.find({});
    },
    subcategory: function(){
        return Subcategory.find({});
    }
});

Solution

  • I would imagine that you'll need to have a hierarchical data structure for your categories, you can do this easily when creating categories have an optional parentId field on your category, and then query for subcategories by the id of the category your subcategories belong to.

    addItem.html

     <template name="addItem">
        <div class="form-group">
                    <select class="form-control cat-select" name="category_name">
                        <option selected> --Category-- </option>
                        {{#each category}}
                            {{> categoryOptions}}
                        {{/each}}
                    </select>   
                    </div>
    
                    <div class="form-group">
                    <select class="form-control" name="subcategory_name">
                        <option selected> --Subcategory-- </option>
                        {{#each subcategory}}
                            {{> categoryOptions }}
                        {{/each}}
                    </select>   
                    </div>
        </template>
    
    
    <template name="categoryOptions">
        <option id="{{ _id }}">{{ name }} </option>
    </template>
    

    addItem.js

    Template.addItem.helpers({
        category: function(){
            return Category.find({});
        },
        subcategory: function(){
            var id = Session.get('selectedCategory');
            return id ? Subcategory.find({parentId: id}) : false;
        }
    });
    
    Template.addItem.events({
        'change .cat-select': function(e, t) {
            //... get the selected option and set a var called carId then set a session var to make it available 
            var catId = $(e.target).val() || false;
            Session.set('selectedCategory', catId);
        }
    });