类别中的组子类别

时间:2015-12-16 01:56:06

标签: meteor

如何将子类别归类为他们所属的类别?例如,类别是ICT,我只想在下拉列表中显示属于ICT的子类别(如笔记本电脑,计算机等)。

任何人都可以向我展示解决方案,因为我是编程领域的新手。谢谢先进..

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({});
    }
});

2 个答案:

答案 0 :(得分:1)

我认为你需要为你的类别设置一个分层数据结构,你可以在创建类别时在类别上有一个可选的 <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> 字段,然后通过id查询子类别您子类别所属的类别。

addItem.html

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);
    }
});

addItem.js

$(document).ready(function(){
setTimeout(function(){
var multiselectHeight=$('.multiselect-container').height();
  $('.content').height(multiselectHeight);
  
},1000);
})

答案 1 :(得分:0)

我已经解决了这个问题。它可以引用任何有同样问题的人.. :)

addItem.html


<template name="allCategory">
        <div class="form-group">
            <select class="form-control category-selection" name="name_category">
                <option selected> --Category-- </option>
                {{#each category}}
                    {{> downCategory}}
                {{/each}}
            </select>   
        </div>

</template>

<template name="allSubcategory">
            <div class="form-group">
            <select class="form-control subcategory-selection" name="name_subcategory">
                <option selected> --Subcategory-- </option>
                {{#each subcategory}}
                    {{> downSubcategory}}
                {{/each}}
            </select>   
            </div>


</template>


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

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

addItem.js

    enter code here

Template.allCategory.helpers({
    category: function(){
        return Category.find({});
    }
});

Template.allCategory.events({
    "change .category-selection": function(e, t){
      return Session.set("category", $("[name=name_category]").val());
    }
  });

Template.allSubcategory.helpers({
    subcategory: function(){
        return Subcategory.find({
            category_id: Session.get('category')
        });
    }
});