我刚刚找到了这个很棒的Meteor autoForm软件包,我想将它与select2一起使用。

我的目标是使用autoForm轻松为我的一个收藏集创建输入表单。障碍是:如何用另一个集合中的字段填充它,以及如何对其进行多选?

在我的lib / collections内部,我声明了Meteor集合:

Clients = new Mongo.Collection('clients');
Clients.attachSchema(new SimpleSchema({
    clientName: {
        type: String,
        label: "Mandator Name",
        max: 200
    }
}));

现在我没有关于autoForm的文档。如果没有记错的话,在atmospherejs页面(https://atmospherejs.com/aldeed/autoform)上,我应该使用类似的东西:
{{#autoForm collection="Clients" id="insertClientForm" type="insert"}}
    {{> afFieldInput name="clientName" options=options}}
{{/autoForm}}

然后像这样写一些JS:
Template.registerHelper({
    options: function() {
        return Clients.find({}, {fields: {clientName: 1}});
    }
});

模板已正确渲染,就像我看到的输入框一样。但是,这不是多重选择,它根本不允许我选择任何值。

关于问题所在的任何想法?

奖励问题:如何在autoForm生成的select输入上使用select2?
编辑:使用aldeed:autoform-select2来使用select2。

最佳答案

我已经使用Meteor测试了此解决方案

aldeed:collection2
aldeed:autoform
natestrauser:选择2
aldeed:autoform-select2

假设您有一个包含有关用户的个人资料信息的表单,并且其中一个字段是“职业”(例如在他们的工作中),并且您希望他们从列表中选择职业。

1)发布要用于“选择”选项的集合。

在服务器上

Meteor.publish('occupations', function () {
  return Occupations.find();
});

2)在客户端上订阅集合

在客户端上
Meteor.subscribe('occupations');

3)为您的表单模板创建一个帮助器
Template.CreateUser.helpers({
  listOccupations: function () {
    return Occupations.find({}).fetch();
  },
});

4)然后最后在autoForm字段的options参数中引用该帮助程序-在这种情况下,我使用afQuickField
{{> afQuickField name='occupations' multiple=true tags=true options=listOccupations}}

5)并确保您的架构已正确设置为使用Select2
occupations: {
    type: [String],
    optional:true,
    label: 'Occupation',
    autoform:{
      type:"select2",
      placeholder: 'Comma spaced list of occupations',
    }
  },

关于javascript - 如何用Meteor中的集合值填充autoForm select?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33436200/

10-10 22:03