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

Che*_*het 2 meteor meteor-collection2

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

我是用户collection2,simple-schema而且autoform.

我想为此做一个简单的quickForm.这是我的简单架构:

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

看一下autoform docs,我注意到我想要一个select视图,所以我需要传入选项.

我希望能够在我的架构中做到这一点!

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

否则,我必须生成一个带有quickFormFields的模板,只是为了传递选项.

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

谢谢你的帮助

Mik*_*ski 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;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

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