【问题标题】:Meteor: How to set a radio button’s value depending on the value in the DatabaseMeteor:如何根据数据库中的值设置单选按钮的值
【发布时间】:2016-06-28 14:42:28
【问题描述】:

网站管理员可以创建新的学术出版物(例如:已发表的论文 (pp) 或书籍章节 (bc))。发布的类型由单选按钮确定。有四种不同的类型。

问题:管理员也可能在稍后阶段编辑已经存在的条目。为此,我需要检索 type 的值并将其传递给 template.html 以预先选择正确的单选按钮。

这怎么可能?任何帮助表示赞赏。

这是 JSON,带有来自集合 Publicationstype 字段:

{
    "_id" : "t4Zc23YSB9fJc8rW4",
    "authors" : [ 
        {
            "lastName" : "Whittington",
            "firstName" : "Richard"
        }, 
        {
            "lastName" : "Cailluet",
            "firstName" : "Ludovic "
        }, 
        {
            "lastName" : "Douglas",
            "firstName" : "Basak Yakis "
        }
    ],
    "title" : "Opening strategy: Evolution of a precarious profession",
    "outlet" : "British Journal of Management, 22 (3)",
    "year" : "2011",
    "abstract" : "No Abstract ...",
    "type" : "pp"
}

helper.js:

Template.adminPublicationsEdit.helpers({
  pubEntry: function () {
    return Publications.findOne({_id: this._id});
  }
});

还有template.html:

<template name="adminPublicationsEdit">
    <div class="container extra-spacing-bottom">
    <div class="putdown extra-spacing-bottom">
            <h1 class="extra-spacing-bottom">EDIT ENTRY</h1>
            <hr>
      {{#with pubEntry}}
      <form class="form-horizontal" id="addPub" role="form">
        <div class="form-group">
            <label for="title" class="col-sm-2 control-label">Title</label>
          <div class="col-sm-10">
              <input type="text" class="form-control" id="title" name="title" value="{{title}}" placeholder="Enter Title">
            </div>
        </div>

        <div class="form-group">
            <label for="year" class="col-sm-2 control-label">Year</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="year" name="year" value="{{year}}" placeholder="Enter Year of Publication">
            </div>
        </div>

       <div class="form-group">
          <label for="abstract" class="col-sm-2 control-label">Abstract</label>
          <div class="col-sm-10">
          <div name="abstract" id="abstract">
            {{{abstract}}}
          </div>
          </div>
      </div>

            <div class="form-group js-radios">
                <label for="Outlet" class="col-sm-2 control-label">Outlet</label>
                <div class="col-sm-10">
                    <label class="radio-inline"><input type="radio" name="outlet-type" id="wp" value="wp">Working Paper</label>
                    <label class="radio-inline"><input type="radio" name="outlet-type" id="pp" value="pp">Published Paper</label>
                    <label class="radio-inline"><input type="radio" name="outlet-type" id="bk" value="bk">Book</label>
                    <label class="radio-inline"><input type="radio" name="outlet-type" id="bc" value="bc">Book Chapter</label>
                </div>
            </div>

              {{#if ppSelected}}
               <div class="form-group">
                  <label for="journal" class="col-sm-2 control-label">Journal</label>
                  <div class="col-sm-10">
                      <input type="text" class="form-control" id="journal" name="journal" placeholder="Enter Name of Journal">
                  </div>
              </div>

              <div class="form-group">
                  <label for="pages" class="col-sm-2 control-label">Pages</label>
                  <div class="col-sm-10">
                      <input type="text" class="form-control" id="pages" name="pages" placeholder="Enter Pages">
                  </div>
              </div>
              {{/if}}

            {{#if bkSelected}}
             <div class="form-group">
                <label for="publisher" class="col-sm-2 control-label">Publisher</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="publisher" name="publisher" placeholder="Enter Publisher">
                </div>
            </div>

            <div class="form-group">
                <label for="location" class="col-sm-2 control-label">Location of Publisher</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="location" name="location" placeholder="Enter Location of Publisher">
                </div>
            </div>
            {{/if}}

            {{#if bcSelected}}
             <div class="form-group">
              <label for="publisher" class="col-sm-2 control-label">Publisher</label>
              <div class="col-sm-10">
                  <input type="text" class="form-control" id="publisher" name="publisher" placeholder="Enter Publisher">
              </div>
            </div>

            <div class="form-group">
              <label for="location" class="col-sm-2 control-label">Location of Publisher</label>
              <div class="col-sm-10">
                  <input type="text" class="form-control" id="location" name="location" placeholder="Enter Location of Publisher">
              </div>
            </div>

             <div class="form-group">
                <label for="editors" class="col-sm-2 control-label">Editor(s)</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="editors" id="firstEditor"  placeholder="Enter Book Editor(s)">
                </div>
              </div>


            <div class="form-group">
              <label for="pages" class="col-sm-2 control-label">Pages</label>
              <div class="col-sm-10">
                <input type="text" class="form-control" id="pages" name="pages" value="{{pages}}">
              </div>
            </div>
            {{/if}}

            <div class="form-group">
              <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" id="js-addEntry" class="btn btn-primary admin-button">UPDATE ENTRY</button>
              </div>
            </div>

          </form> <!-- end form -->

      {{/with}}
    </div> <!-- putdown -->
  </div> <!-- container -->
</template>

【问题讨论】:

    标签: javascript jquery mongodb templates meteor


    【解决方案1】:

    如果我理解了您的问题,您想在 Outlet form-group 中选择一个特定的单选按钮,它对应于 typepubEntry 对象的值。如果是这种情况,您可以实现 Meteor 助手并将类型作为函数参数传递。

    例如:

    if (Meteor.isClient) {
      Template.adminPublicationsEdit.helpers({
        // ...
        isChecked: function(type) {
          return (this.type === type) ? "checked" : "";
        }
        // ...
      });
    }
    

    <template name="adminPublicationsEdit">
      <!-- ... -->
      <div class="form-group js-radios">
        <label for="Outlet" class="col-sm-2 control-label">Outlet</label>
        <div class="col-sm-10">
          <label class="radio-inline">
            <input type="radio" name="outlet-type" id="wp" value="wp" {{isChecked 'wp'}}>Working Paper</label>
          <label class="radio-inline">
            <input type="radio" name="outlet-type" id="pp" value="pp" {{isChecked 'pp'}}>Published Paper</label>
          <label class="radio-inline">
            <input type="radio" name="outlet-type" id="bk" value="bk" {{isChecked 'bk'}}>Book</label>
          <label class="radio-inline">
            <input type="radio" name="outlet-type" id="bc" value="bc" {{isChecked 'bc'}}>Book Chapter</label>
        </div>
      </div>
      <!-- ... -->
    </template>
    

    【讨论】:

    • 天啊,它完美地工作。维伦丹克! :) 我了解 html 部分,但在这种情况下 this 到底是什么?为什么这行得通? :D Grüße
    • @AmirRahbaran 欢迎您!好吧,this 是当前上下文,在您的情况下是 pubEntry(请注意,isChecked 助手位于 {{#with pubEntry}}…{{/with}} 块内)。
    • 啊,当然。忘记了{{#with pubEntry}}…{{/with}}context。我从你身上学到了很多。非常感谢。
    猜你喜欢
    • 2022-10-17
    • 2015-11-11
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 2021-04-14
    • 2014-05-10
    相关资源
    最近更新 更多