【问题标题】:Populating HTML Select dropdown from a database in Meteor从 Meteor 的数据库中填充 HTML Select 下拉列表
【发布时间】:2016-02-22 11:20:58
【问题描述】:

我无法找到正确的答案,因此请查看 stackOverflow 工作人员是否可以提供帮助。我知道这似乎很常见,但这个例子与我在搜索时发现的有点不同。

我有一个流星模板,其中有一个选择下拉菜单,相关部分如下:

    {{#each phoneNumber}} 
        <li>
            <input id="{{this._id}}" type="text" name="phoneNumberItem" placeholder="Phone number here"  value="{{number}}">
            <select id="{{this._id}}" name="type">
            <!-- go through collection and see which is selected there, mark it as selected here so they match -->
                <option selected="{{selectedOrNot}}" name="selection">Work</option>
                <option selected="{{selectedOrNot}}" name="selection">Home</option>
                <option selected="{{selectedOrNot}}" name="selection">Mobile</option>
                <option selected="{{selectedOrNot}}" name="selection">Office</option>
                <option selected="{{selectedOrNot}}" name="selection">Fax</option>
                <option selected="{{selectedOrNot}}" name="selection">Other</option>
            </select>
            <input id="{{this._id}}" type="button" class="remove" value="X">
        </li>
    {{/each}}

selectedOrNot 所在的助手的完整代码如下所示:

  Template.addPhoneNumber.helpers({

    //the full list of phone numbers in the collection, so we can loop through them in the template and draw them into the form
    'phoneNumber': function() {
      return newOrgPhoneNumbers.find();
    },


    //let's us choose which of the dropdown items are selected
    'selectedOrNot': function(event){

      var collectionType = newOrgPhoneNumbers.findOne();  //get the type from the collection (EG: Home, Mobile, Fax, etc)
      var typeFormName = $('[name="selection"]').val();   //get the data from the form (EG: Home, Mobile, Fax, etc)


      //When drawing the select dropdown, see if there's a match and return selected if so
     if(collectionType.type === typeFormName){
      console.log ('selected!');
      return 'selected';
     }

  }

  });

我想做什么:

我将数据存储在一个名为 newOrgPhoneNumbers 的集合中。

此数据包括一个“类型”,其中包含“家庭”、“移动”、“工作”等。

当我在模板中绘制选择下拉列表时,我希望选择与 newOrgPhoneNumbers 集合中的“类型”匹配的选项。请注意,有多个选择下拉菜单,数据集合中的每个项目都有一个。所以可能会说,表格中有 6 个电话号码,每个号码都有自己的 ID,该 ID 取自它的集合。

就本示例而言,集合数据的“类型”为“Home”,因此我希望它在选择下拉列表中选择 Home 的情况下进行绘制。

现在我可以看到这里出了什么问题。 typeFormName 采用选择器下拉列表的值,默认情况下始终为“工作”。

我怎样才能修改它以在我的 If 条件中进行匹配并在我得到匹配时返回“已选择”?我想我需要从模板中检索一个与集合中的值匹配的值,但选择器下拉列表没有这样的值(因为它在绘制时总是“工作”)。

我今天花了大约 5 个小时进行各种逻辑尝试,所以是时候提问了。任何帮助是极大的赞赏。抢

【问题讨论】:

    标签: javascript mongodb templates meteor return


    【解决方案1】:

    不是这个

    //let's us choose which of the dropdown items are selected
        'selectedOrNot': function(event){
    
          var collectionType = newOrgPhoneNumbers.findOne();  //get the type from the collection (EG: Home, Mobile, Fax, etc)
          var typeFormName = $('[name="selection"]').val();   //get the data from the form (EG: Home, Mobile, Fax, etc)
    
    
          //When drawing the select dropdown, see if there's a match and return selected if so
         if(collectionType.type === typeFormName){
          console.log ('selected!');
          return 'selected';
         }
    
      }
    

    试试这个

    //let's us choose which of the dropdown items are selected
        'selectedOrNot': function(event){
          var typeFormName = $('[name="selection"]').val();   //get the data from the form (EG: Home, Mobile, Fax, etc)
    
    
          //When drawing the select dropdown, see if there's a match and return selected if so
         if(this.type === typeFormName){
          console.log ('selected!');
          return 'selected';
         }
      }
    

    确保您通过console.log 获得正确的值

    编辑

    试试下面的代码

    {{#each phoneNumber}} 
            <li>
                <input id="{{this._id}}" type="text" name="phoneNumberItem" placeholder="Phone number here"  value="{{number}}">
                <select id="{{this._id}}" name="type">
                <!-- go through collection and see which is selected there, mark it as selected here so they match -->
                    <option value="Work">Work</option>
                    <option value="Home">Home</option>
                    <option value="Mobile">Mobile</option>
                    <option value="Office">Office</option>
                    <option value="Fax">Fax</option>
                    <option value="Other">Other</option>
                </select>
                {{setDropdownValue}}
                <input id="{{this._id}}" type="button" class="remove" value="X">
            </li>
        {{/each}}
    

    在助手中

    setDropdownValue: function(){
     $("#"+this._id).val(this.type);
    }
    

    【讨论】:

    • 谢谢,这样更有效率,是的,非常感谢。不幸的是,结果仍然是一样的。 this.type 确实返回数据库保存的内容(很棒)。 typeFormName 每次都返回“Work”。选择器在下拉列表中有所有其他选项(移动、工作、家庭等),但默认选择“工作”,正如我所说,typeFormName 返回“工作”。这似乎是问题所在,我还找不到解决方法。
    • @robster,这里为什么需要typeFormName?根据您的问题,我了解这一点,您的电话号码记录每个都有type 属性,并且您想默认从下拉列表中选择type,我正确吗?如果我是正确的,我有不同的解决方案
    • 你正在扩展我的初学者大脑,我很感激。 :-) 我试了一下,但不完全理解 $("#"+this._id).val(this.type);我相信它正在获取表单元素的价值并对其进行处理?我也不确定它如何与模板中的 {{setDropdownValue}} 一起使用?对不起,我还没有那么先进。目前它没有更新表格,因为我不够熟练,但我不知道为什么。就目前而言,表单绘制与您调整后的代码一样。再次感谢...
    • OK 进行更多测试,结果发现输入与按钮具有相同的 ID。因此,如果我尝试输入输入,它会将自身重写为 HOME。所以我看到我们正在将 ID 设置为 HOME 的表单对象,但在这种情况下,它们都具有相同的 ID。它们共享数据库 ID(用于其他处理)。在这种情况下,我需要为选择下拉菜单设置不同的标识符。我会用你到目前为止给我的东西玩一玩,看看我能想出什么。慢慢到达那里。再次感谢
    • 是的,我没有注意到,对于选择,您可以像 &lt;select id="sel_{{this._id}}" name="type"&gt; 一样给出,在帮助器中您可以在这里使用 $("#sel_"+this._id).val(this.type); 只是为了区分 id 我在 id 前面加上 sel_ 希望帮助
    【解决方案2】:

    非常感谢@Sasikanth。这是他对我的帮助,并在下面完整提供以帮助其他人/将来参考。

    流星模板:

    {{#each phoneNumber}} 
            <li>
                <input id="{{this._id}}" type="text" name="phoneNumberItem" placeholder="Phone number here"  value="{{number}}">
                <select id="sel_{{this._id}}" name="type">
                <!-- go through collection and see which is selected there, mark it as selected here so they match -->
                    <option value="Work">Work</option>
                    <option value="Home">Home</option>
                    <option value="Mobile">Mobile</option>
                    <option value="Office">Office</option>
                    <option value="Fax">Fax</option>
                    <option value="Other">Other</option>
                </select>
                {{setDropdownValue}}
                <input id="{{this._id}}" type="button" class="remove" value="X">
            </li>
        {{/each}}
    

    流星助手:

      Template.addPhoneNumber.helpers({
    
        //the full list of phone numbers in the collection, so we can loop through them in the template and draw them into the form
        'phoneNumber': function() {
          return newOrgPhoneNumbers.find();
        },
    
        //This will take the value of the database item's "type" field and send it back to the select html element.  
        //It's using the sel_ prefix to separate it from the other items with the same id in the template so they don't receive the data by accident.
        'setDropdownValue': function(){
         $("#sel_"+this._id).val(this.type);
        }
    
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-09
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      相关资源
      最近更新 更多