选择标签,指定选定的选项(或将数组元素移动到索引 0) [英] Select tag, specifying the selected option (or moving an array element to index 0)

查看:47
本文介绍了选择标签,指定选定的选项(或将数组元素移动到索引 0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在查询水果列表,然后只将水果的 ID 和名称收集到 @fruit 中.

Say I'm querying a list of fruit and then collecting just the id's and name's of the fruit into @fruit.

[32, "apple"],
[8, "bannana"],
[10, "cantelope"],
[11, "grape"],
[15, "orange"],
[41, "peach"],
[22, "watermelon"]

@fruit 被用在 Select 助手中.位于@fruit 索引 0 处的apple"将是选择的选定值(第一个选项).这是一个虚构的示例,但默认情况下,我将始终通过名称(而不是 id)知道橙色"是什么.我需要orange"作为选择标签的选定值(第一个选项).

@fruit is being used in a Select helper. "apple" being at index 0 of @fruit will be the selected value (first option) of the select. This is a made up example but by default I'll always know what "orange" is by name (not by id). I need "orange" to be the selected value of the select tag (the first option).

":prompt => 'orange'" 只是在选择中添加了orange"的第二个实例.到目前为止,我在 Google 上找到的所有内容似乎都是关于在列表中添加一个额外的值或空白.

":prompt => 'orange'" just adds a 2nd instance of "orange" in the select. Everything I've found on Google so far seems to be about adding an extra value or blank to the list.

由于数组的索引 0 始终成为选定值(如果在选择帮助器中没有使用提示或空白),有没有办法找到包含名称orange"(@fruit[x].name == 'orange'),并将其移动到索引 0,同时在列表的其余部分保留现有的 alpha 排序?所以,@fruit 数组看起来像:

Since index 0 of the array always becomes the selected value (if no prompt or blank is used in the select helper), is there a way I could find the index containing the name "orange" (@fruit[x].name == 'orange'), and move it to index 0 while retaining the existing alpha sort in the rest of the list? So, the @fruit array would look like:

@fruit[0]    [15, "orange"],
@fruit[1]    [32, "apple"],
@fruit[2]    [8, "bannana"],
@fruit[3]    [10, "cantelope"],
@fruit[4]    [11, "grape"],
@fruit[5]    [41, "peach"],
@fruit[6]    [22, "watermelon"]

我现在唯一能想到的就是遍历@fruit,如果找到orange",则将其添加到新数组中.然后再次遍历@fruit 数组并添加任何名称不为orange"的内容.我不知道如何写出来,但似乎它会做我正在寻找的东西.也许有一些简单的方法可以做到这一点我错过了(指定数组中的哪个索引是第一个写入的选项)?

The only thing I can think of right now would be to iterate through @fruit and if "orange" is found add it to a new array. Then iterate through the @fruit array again and add anything that doesn't have a name of "orange". I'm not sure how to write that out but it seems like it would do what I'm looking for. Maybe there's some easy way to do this I'm missing (specifying which index in an Array is to be the first option written)?

谢谢!

推荐答案

参见此处:http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease

如果您使用现有的助手,您可以通过 id 指定您想要选择的选项.特别是这个例子:

If you use the existing helpers, you can specify by id which option you want selected. Specifically this example:

<%= options_for_select([['Lisbon', 1], ['Madrid', 2]], 2) %>

output:

<option value="1">Lisbon</option>
<option value="2" selected="selected">Madrid</option>

您说您不知道 ID 中哪个选项是橙色".如果你想在水果数组中找到orange"的 ID 就可以了,那么你可以使用助手:

You say you don't know which option is "orange" by ID. If you want to find the ID for "orange" in the fruit array this would do it, then you can use the helper:

(@fruit.detect { |i| i[1] == 'orange' } || []).first

这篇关于选择标签,指定选定的选项(或将数组元素移动到索引 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆