简体   繁体   English

如何使用Selectize.js从完整表中进行选择?

[英]How can I select from a full table with Selectize.js?

I'm trying to use the selectize-rails gem. 我正在尝试使用selectize-rails gem。 How can I use selectize to select from a full table? 如何使用selectize从整个表格中进行选择?

The following code allows me to multi-select "Option1" and "Option2". 以下代码允许我多选“ Option1”和“ Option2”。

views/users/show.html.erb : views / users / show.html.erb

$('#select-to').selectize({
    persist: false,
    maxItems: null,
    valueField: 'email',
    labelField: 'name',
    searchField: ['name', 'email'],
    options: [
        {email: 'option1@example.com', name: 'Option1'},
        {email: 'option2@example.com', name: 'Option2'},
    ],
    render: {
        item: function(item, escape) {
            return '<div>' +
                (item.name ? '<span class="name">' + escape(item.name) + '</span>' : '') +
                (item.email ? '<span class="email">' + escape(item.email) + '</span>' : '') +
            '</div>';
        },
        option: function(item, escape) {
            var label = item.name || item.email;
            var caption = item.name ? item.email : null;
            return '<div>' +
                '<span class="label">' + escape(label) + '</span>' +
                (caption ? '<span class="caption">' + escape(caption) + '</span>' : '') +
            '</div>';
        }
    },
    createFilter: function(input) {
        var match, regex;

        // email@address.com
        regex = new RegExp('^' + REGEX_EMAIL + '$', 'i');
        match = input.match(regex);
        if (match) return !this.options.hasOwnProperty(match[0]);

        // name <email@address.com>
        regex = new RegExp('^([^<]*)\<' + REGEX_EMAIL + '\>$', 'i');
        match = input.match(regex);
        if (match) return !this.options.hasOwnProperty(match[2]);

        return false;
    },
    create: function(input) {
        if ((new RegExp('^' + REGEX_EMAIL + '$', 'i')).test(input)) {
            return {email: input};
        }
        var match = input.match(new RegExp('^([^<]*)\<' + REGEX_EMAIL + '\>$', 'i'));
        if (match) {
            return {
                email : match[2],
                name  : $.trim(match[1])
            };
        }
        alert('Invalid email address.');
        return false;
    }
});

How can I select from a full table? 如何从完整表格中选择? For example, I have a table named "User". 例如,我有一个名为“ User”的表。 How can I select from User.all , not just pre-set options? 如何从User.all选择,而不仅仅是预设选项?

schema.rb : schema.rb

create_table "things", force: true do |t|
  t.string   "name"
  t.string   "email"
  #...
end

According to the docs , you need to create a load method that will invoke callback with the array of options. 根据docs ,您需要创建一个load方法,该方法将使用选项数组调用callback Setting preload to true forces it to be populated at the time the select is created. 将preload设置为true会强制在创建选择时填充它。

In your .selectize(): 在您的.selectize()中:

load: function(query, callback) {
        $.ajax({
            url: 'http://railshost/users.json',
            type: 'GET',
            dataType: 'json',
            data: {
               // query: query  // if your data source allows
            },
            error: function() {
                callback();
            },
            success: function(response) {
                callback(response.table);
            }
        });
    }
...

app/controllers/users_controller.rb: app / controllers / users_controller.rb:

class UsersController < ApplicationController
  def index
    @users = User.all.where(type: "A")
    respond_to do |format|
      format.json { render json: @users}
    end
  end
end

Since it's only about 100 objects, I don't think you need to mess around within the selectize code; 由于它只有大约100个对象,因此我认为您无需在selectize代码中四处寻找; you can simply dynamically set the options with Rails, and selectize will make them look pretty. 您可以简单地使用Rails动态设置选项,然后选择使其看起来更漂亮。

Here's the method I used in my app, adjusted for yours: 这是我在应用程序中使用的方法,已针对您的情况进行了调整:

<select id='select-to'>
  <option value="">Please select an option</option>
  <% User.where(type: "A").each do |user| %>
    <option value="<%= user.id %>"><%= user.name %></option>
  <% end %>
</select>

And then just selectize #select-to. 然后只需选择#select-to。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM