[英]How to populate autoForm select with values from collection in Meteor?
我剛剛為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的文檔。 在大氣頁面( 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生成的選擇輸入上使用select2? 編輯 :使用aldeed:autoform-select2來使用select2。
我用Meteor測試了這個解決方案
aldeed:collection2 aldeed:autoform natestrauser:select2 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',
}
},
您需要將集合映射到標簽和值; label是客戶端將看到的內容,value是將在提交時保存的內容。
https://github.com/aldeed/meteor-autoform#use-a-helper
Template.registerHelper({
options: function() {
return Clients.find({}, {fields: {clientName: 1}}).map(function (c){
return {label: c.clientName, value: c._id};;
}
});
如果要進行多選,則需要使模式鍵類型為[String]
而不是String
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.