2014-05-14 32 views
2

我希望能夠從用戶列表中選擇多個用戶。如何生成一個表單以選擇在流星中使用Autoform和Collection2的用戶?

我是用戶collection2,simple-schemaautoform

我想爲此生成一個簡單的快速表單。這裏是我的簡單模式:

Schemas.Item = new SimpleSchema({ 
    name: { 
     type: String, 
     label: "Name", 
     max: 100 
    }, 
    userIds: { 
     type: [String], 
     regEx: SimpleSchema.RegEx.Id 
    } 
}); 

望着autoform docs,我注意到,我想有一個選擇視圖,所以我需要在選項傳遞。

我希望能夠在我的模式中做到這一點!

userIds: { 
     type: [String], 
     regEx: SimpleSchema.RegEx.Id 
     options: function() { 
      // return users with {value:_id, label:username} 
     } 
    } 

否則,我必須生成一個帶有quickFormFields的模板來傳遞選項。

只是堆東西上,不應該有任何重複的用戶id ...

感謝所有幫助

回答

5

也許你已經找到了答案,但也許有人會覺得它有用。選擇用戶後,我有很多不同的事情需要指定,這就是爲什麼我的用戶類型是[Object]。在你的情況下,你可以修改它。最重要的部分是autoform.options方法,似乎是你正在尋找的部分。

users: { 
     type: [Object] 
    }, 
    "users.$.id": { 
     // you can use your own type, e.g. SimpleSchema.RegEx.Id, as I am using custom Schema for accounts 
     type: Schemas.Account._id, 
     label: 'Select user', 
     autoform: { 
      options: function() { 
       var options = []; 
       Meteor.users.find().forEach(function (element) { 
        options.push({ 
         label: element.username, value: element._id 
        }) 
       }); 
       return options; 
      } 
     } 
    } 

片段上方會給你所有用戶的列表,以便您可以輕鬆地從下拉列表中選擇。

請記住添加適當的發佈方法使其工作,因爲您不會總是隻能獲取當前已登錄的發佈方法。

相關問題