【问题标题】:Load a select input field with collection data in Meteor在 Meteor 中加载带有集合数据的选择输入字段
【发布时间】:2018-07-01 01:45:14
【问题描述】:

我是 Meteor 的新手。我正在使用Meteor-x-editable-bootstrap 创建一个下拉列表,其中的选项必须使用 Mongo 集合中的数据填充。

现在,我的代码是:

$('#options').editable({
  type: 'select',
  source: [
    {text: 'Option 1'},
    {text: 'Option 2'},
    {text: 'Option 3'}
  ]
});

这些是硬编码值(“选项 1”、“选项 2”等)。现在,我有一个集合Options,其中有一个字段Values。如何动态设置source 数组以从集合中检索数据?请帮忙!!

【问题讨论】:

    标签: javascript meteor


    【解决方案1】:

    菜单的流星模式根本不需要 jQuery。在您的模板中,您只需遍历集合即可创建菜单:

    <template name="myMenu">
      <select #id="mySelect>
        {{#each item}}
          <option value={{_id}}>{{name}}</option>
        {{/each}}
      </select>
    </template>
    

    那么你只需要一个帮助器来返回你需要的项目:

    Template.myMenu.helpers({
      items() {
        return MyCollection.find();
      }
    });
    

    还有一个事件处理程序:

    Template.myMenu.events({
      'onChange #mySelect'(ev) {
        ...handle the event.
      }
    });
    

    【讨论】:

      【解决方案2】:

      为什么不每次检索数据时都调用您编写的相同代码?

      var receivedJson = getNewOptions(); // your data retrieving code here
      $('#options').editable({
        type: 'select',
        source: receivedJson
      });
      

      【讨论】:

        猜你喜欢
        • 2015-07-24
        • 1970-01-01
        • 1970-01-01
        • 2019-02-25
        • 1970-01-01
        • 1970-01-01
        • 2015-08-21
        • 2014-01-03
        • 1970-01-01
        相关资源
        最近更新 更多