【问题标题】:How can we display html field based on the data in collection?我们如何根据集合中的数据显示 html 字段?
【发布时间】:2018-05-06 22:48:12
【问题描述】:

我是流星新手,我想检索 jenkins 作业并将该作业详细信息与参数一起存储在 mongoDB 集合中,然后显示不同的字段,例如文本框,检查集合中该数据的按钮。
我的工作集合

 db.jobs.find().pretty()
{
    "_id" : ObjectId("5a15bc45145a3789cb3b97eb"),
    "platform" : "xyz",
    "job_name" : "http://123.123.123.123:111/jobs",
    "a" : 0,
    "b" : 0,
    "c" : 0,
    "d" : 0,
    "e" : 0,
}

如您所见,我收集了这些数据,对于“job_name”,“platform”,我想要获取文本框,对于其他参数,我想要复选框。
可能有很多工作,每个工作都应具有上述属性。
joblist.html

<template name="jobs">
 <div id="listjobs">
  {{#each jobs}}
    ????how can i do it????
  {{/each}}
 </div>
</template>

我在js部分写的不多,请帮帮我。

【问题讨论】:

    标签: javascript mongodb meteor jenkins meteor-blaze


    【解决方案1】:

    #each 块只是 for each 的包装器,当前文档在此块中变为 this。然后,您可以将数据与 this 一起使用(如下面的示例所示)或仅由属性名称引用。更多信息请阅读here in the Blaze documentation

    只是作为预览:

    <template name="jobs">
     <div id="listjobs">
      {{#each jobs}}
         <div>: {{this.job_name}}</div>
         <form id="form_{{this._id}}">
            <textarea name="platform">{{this.platform}}</textarea>
            <textarea name="job_name">{{this.job_name}}</textarea>
            <input type="checkbox" name="a">{{this.a}}
            <input type="checkbox" name="b">{{this.b}}
            <input type="checkbox" name="c">{{this.c}}
            <input type="checkbox" name="d">{{this.d}}
            <input type="checkbox" name="e">{{this.e}}
            <button type="submit">Submit</button>
         </form>
      {{/each}}
     </div>
    </template>
    

    请注意,您甚至可以在属性中使用上下文,就像在表单 id 中使用的那样。如果您希望您的复选框检查初始值,则需要进行检查:

    <input type="checkbox" name="e" checked="{{this.e}}">{{this.e}}
    

    【讨论】:

      【解决方案2】:

      如果作业的返回值是一个对象,看起来像这样

      {
          "job": "job1",
          "job": "job2"
      }
      

      随便用吧

      <form id="demo">
          {# for data in jobs #}
          <div id="listjobs">
              <input type="checkbox">{{ data.job }}
          </div>
          {# endfor #}
      </form>
      

      【讨论】:

        猜你喜欢
        • 2021-05-24
        • 1970-01-01
        • 2021-12-28
        • 2021-12-18
        • 2019-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多