首页 文章

带用户下拉菜单的Meteor SimpleSchema

提问于
浏览
1

我在Meteor中有一个应用程序,其想法是管理员用户可以添加文档并将其分配给其客户(客户存储在用户集合中) . 所以我想在文档插入视图中向客户提供一个下拉框 . 架构的相关代码如下:

customer: {
    type: [String],
    label: "Customers",
    allowedValues: function () {
      return Meteor.users.find().map(function (user) {
        return user._id
      });
    },
    autoform: {
      options: function () {
        return Meteor.users.find({}).map(function(user) {
          return {
            value: user._id,
            label: user.profile.name
          }
        })
    }
  },
    optional: true
  }

当我输入一个类型:String(而不是[String])时,它只在下拉框中显示当前用户 . 如果我使用[String](应该是),下拉框实际上会在一个文本框中打开(它没有典型的下拉行为),包含3个字段(对于它找到的所有用户),但它只显示第一个再次,但留下其他2的占位符 .

该视图使用:

{{> afQuickField name='customer'}}

1 回答

  • 1

    IMPORTANT :帐户包,默认情况下不会发布用户集合 . 您必须在服务器中编写新的发布方法,并在客户端中进行相应的订阅才能使其正常工作 .

    没有读过..

    嗯..这有点棘手 . 查看下面的代码并相应地编辑“autoform”部分 .

    autoform: {
                 options: function () {
                var options = [];
                Meteor.users.find().forEach(function (element) {
                    options.push({
                        label: element.profile.name, value: element._id
                    })
                });
                return options;
                }
    
              }
    

    选择框'选项'所需的语法是:

    选项:{[标签值],...}

    上面的代码读取用户集合中的所有行,并将每行推送到名为“options”的数组作为数组 .

    希望这有助于或给你一些见解 .

    请注意,上述内容仅适用于您的收藏订阅/发布正确的情况 .

    查看以下代码以获得一个简单的想法 .

    if (Meteor.isClient) {
                Meteor.subscribe('allUsers')
               }
    
    
           if (Meteor.isServer) {
             Meteor.publish('allUsers', function() {
               return Meteor.users.find({}, {fields:{username:1,emails:1}})
             })
            }
    

相关问题