Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 流星类别和子类别选择菜单_Javascript_Recursion_Meteor_Meteor Blaze_Spacebars - Fatal编程技术网

Javascript 流星类别和子类别选择菜单

Javascript 流星类别和子类别选择菜单,javascript,recursion,meteor,meteor-blaze,spacebars,Javascript,Recursion,Meteor,Meteor Blaze,Spacebars,我对Meteor还不熟悉,但对这个框架有了一定的了解。我正在创建一个应用程序,我必须在其中构建一个类别管理模块,我正在为此使用一个类别集合,在文档中,我的值如下所示 { _id: name: parentID: ..... } 我尝试了一些方法使其递归,但没有成功,我需要的是一个下拉列表,其中包含所有类别及其子类别。像这样: 如果在座的任何人能在这个问题上提供帮助,我将不胜感激: 现在我所做的只是把我带到两个级别,我是说上一级父级和下一级子级,我想要无限级别,我知道通过递归函数可能是可行的

我对Meteor还不熟悉,但对这个框架有了一定的了解。我正在创建一个应用程序,我必须在其中构建一个类别管理模块,我正在为此使用一个类别集合,在文档中,我的值如下所示

{
_id:
name:
parentID:

.....
}
我尝试了一些方法使其递归,但没有成功,我需要的是一个下拉列表,其中包含所有类别及其子类别。像这样:

如果在座的任何人能在这个问题上提供帮助,我将不胜感激:

现在我所做的只是把我带到两个级别,我是说上一级父级和下一级子级,我想要无限级别,我知道通过递归函数可能是可行的,但找不到方法

模板:

<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>

下面是一个经过测试的解决方案:

html

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

您好,大卫,这是我正在寻找的:),谢谢您的帮助,顺便说一句,它也让我对Underline.JS helpers有了一些了解;)
<template name="categoryselect">
  <select id="category" name="category" class="category">
    <option value="">--Select--</option>
    {{#each get_categories}}
      <option value="{{_id}}">{{name}}</option>
    {{/each}}
  </select>
</template>
Template.categoryselect.helpers({
  get_categories: function() {
    var results = [];

    var mapChildren = function(category, level) {
      // add the appropriate number of dashes before each name
      var prefix = Array(2 * level).join('--');
      results.push({_id: category._id, name: prefix + category.name});

      // repeat for each child category
      var children = Categories.find({parentID: category._id}).fetch();
      _.each(children, function(c) {
        // make sure to increment the level for the correct prefix
        mapChildren(c, level + 1);
      });
    };

    // map each of the root categories - I'm unsure if the parent
    // selector is correct or if it should be {parentId: {$exists: false}}
    _.each(Categories.find({parentID: ''}).fetch(), function(c) {
      mapChildren(c, 0);
    });

    // results should be an array of objects like {_id: String, name: String}
    return results;
  }
});