【问题标题】:Add a class to each select options with SimpleForm使用 SimpleForm 为每个选择选项添加一个类
【发布时间】:2013-02-17 22:00:47
【问题描述】:

我想使用 SimpleForm 从数组(而不是模型集合)构建select 输入,并为每个options 设置不同的类。

我希望这会奏效:

f.input :method, collection: [
    ["option text", "option_value_1", { class: "class_name_1" }],
    ["option text 2", "option_value_2", { class: "class_name_2" }]
]

问题在于它会生成:

<select>
    <option value="option text" class="class_name_1">option text</option>
    <option value="option text 2" class="class_name_2">option text 2</option>
</select>

我怎样才能用简单的形式做我想要的(值应该是“选项值”)?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 forms select simple-form


    【解决方案1】:

    您也可以将数组数组作为参数传递

    = f.input :status, collection: [['option text', 'option_value_1', {"class" => "class_name_1"}], ['option text 2', 'option_value_2', {"class" => "class_name_2"}]]
    

    【讨论】:

      【解决方案2】:

      这似乎是使用集合时的限制,请参阅 SimpleForm 的解释作者here。他推荐了一种解决方法:

      f.input :method, :as => :select do
        f.select :method, [['option text', 'option_value_1', {"class" => "class_name_1"}], ['option text 2', 'option_value_2', {"class" => "class_name_2"}]]
      end
      

      【讨论】:

      • 非常感谢 :) 实际上我发现了一个提到这件事的 gh 问题,但它是 2 年前的,所以我希望情况有所改变。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-29
      相关资源
      最近更新 更多