javascript - meteor :在单独的列表标签中显示 mongo 数组中的每个项目

标签 javascript html mongodb templates meteor

注意:完整代码可以在这里找到:

https://github.com/Julian-Th/crowducate-platform/tree/feature/courseEditRights

目前,数组中的所有项目都显示在一个列表中,而不是单独的列表标签中:

enter image description here

我的 JS(我注释掉了一些之前的方法):

Template.modalAddCollaborators.events({
    'click #js-addCollaborator' : function (event) {
        var collaboratorName = $('#collaboratorName').val(); // 
        Courses.update(
           { _id: this._id },
           { $addToSet: {canEditCourse: collaboratorName } }
        )
        $('#collaboratorName').val("");
    }
});

Template.modalAddCollaborators.helpers({
    'addedCollaborators': function () {
        return Courses.find();
        //return Courses.find({_id: this._id}, {$in: "canEditCourse"});
        //return Courses.distinct("canEditCourse");
    }
});

我的 HTML:

<template name="modalAddCollaborators">
  <div id="modalAddCollaborators" class="modal fade" role="dialog">
    <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Manage Your Collaborators</h4>
      </div>
      <div class="modal-body">
        <form class="form" role="form">
          <ul class="list-group">
            {{#each  addedCollaborators}}<li class="list-group-item">{{canEditCourse}}</li>{{/each}}
          </ul>
          <div class="form-group">
            <input type="text" id="collaboratorName" placeholder="add a collaborator  ...">
            <button type="button" id="js-addCollaborator" class="btn btn-success">Add</button>
          </div>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>
</template>

我的 JSON:

{
    "_id" : "rmZEFmfoBwf4NwqX4",
    "title" : "Love",
    "coverImageId" : "P7PyR6x64uCSX7X9m",
    "author" : "test",
    "keywords" : [ 
        "test"
    ],
    "published" : "true",
    "about" : "test",
    "canEditCourse" : [ 
        "wicazRk3EsThE5E8W", 
        "Jolle", 
        "jolle", 
        "vW59A6szZijMDLDNh"
    ],
    "createdById" : "wicazRk3EsThE5E8W",
    "dateCreated" : ISODate("2015-12-27T15:06:28.272Z")
}

感谢任何帮助,谢谢。

最佳答案

Courses.find(); 返回游标而不是数组。使用 fetch() 方法代替:

Template.modalAddCollaborators.helpers({
    'addedCollaborators': function () {
        return Courses.find().fetch();        
    }
});

在您的模板中,创建嵌套的 {{#each}}第一个 block 遍历 courses 数组,下一个 block 获取 canEditCourse 数组作为参数。在 block 内部,您可以使用 this 引用被迭代的元素,例如如下所示:

<template name="modalAddCollaborators">
    {{#each addedCollaborators}}
        <h1>{{title}}</h1>
        <ul class="list-group">
        {{#each canEditCourse}}
            <li class="list-group-item">{{this}}</li>
        {{/each}}
        </ul>
    {{/each}}
</template>

关于javascript - meteor :在单独的列表标签中显示 mongo 数组中的每个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34494369/

相关文章:

javascript - jQuery - 查找所有输入,除了从 div 降序的输入

php - 如何在 php 7 中连接到 mongodb 数据库

node.js - 如何从 node-mongo native 驱动程序获取数据库实例?

node.js - 处理 MongoDB 中的(长期)连接丢失

javascript - 我可以向另一个域发出 XMLHttpRequest 吗?

javascript - 使用 useEffect 清空页面

javascript - JS 在窗口调整大小时中断

javascript - 如何在 html5 canvas 中绘制椭圆?

html - 这是指定 HTML 固定列宽度(宽度或样式属性)的更好方法

html - Bootstrap,无论 html 布局如何,都允许内容响应/灵活