如何在Meteor中使用Autoform和Collection2生成表单来选择用户?

时间:2014-05-14 01:45:42

标签: meteor meteor-collection2

我希望能够从用户列表中选择多个用户。

我是用户collection2simple-schemaautoform

我想生成一个简单的quickForm来执行此操作。这是我的简单架构:

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的模板,只是为了传递选项。

只是为了堆积东西,不应该有任何重复的userIds ...

感谢您的帮助

1 个答案:

答案 0 :(得分:5)

可能你已经找到了答案,但也许有人会发现它很有用。一旦你选择了用户,我就会指定很多不同的东西,这就是为什么我的用户类型是[对象]。在您的情况下,您可以修改它。最重要的部分是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;
            }
        }
    }

上面的代码段将为您提供所有用户的列表,以便您可以从下拉列表中轻松选择它们。

请记住添加适当的发布方法,使其无需工作,您将始终只获得当前记录的方法。