10 Nov
duccio

duccio il 10 November 2006 parla di Rails Snippet

Options raggruppate per le Select

Come sicuramente sapete è possibile raggruppare le option di una select, per fare questo il Rails vi viene in aiuto con questo Helper option_groups_from_collection_for_select. Io ho avuto un attimo di esitazione per passare i parametri corretti ecco il perchè del post.

    1 option_groups_from_collection_for_select(Category.find(:all),
    2 "children.find(:all)", 
    3 "name",
    4 "id",
    5 "name")

Category è la tabella con le categorie che si comporta, ad esempio, come Acts_as_tree, ecco perchè il parametro successivo dell’option_groups_from_collection_for_select è ciò che deve essere fatto su categories per riempire le option di ogni singolo raggruppamento.

Non è niente di importante ma spero lo troviate comunque utile.

8 Commenti a “Options raggruppate per le Select”

  1. karel il 8 March 2007 alle 18:31 dice:

    Thank you for this elegant solution with children.find!

    K.

  2. duccio il 8 March 2007 alle 20:05 dice:

    Thank you!

  3. dirceu il 13 May 2007 alle 22:59 dice:

    thank you.

  4. altan il 6 September 2007 alle 07:04 dice:

    thank you.

  5. vitaly il 13 October 2008 alle 15:14 dice:

    Thank you! =)

  6. Javix il 11 March 2009 alle 14:05 dice:

    Thank you for your solution. But I’m asking how to do if there are no children. So I tried like you with Category model where there are parents and their children. But some times I have only a parent without any children. In this case I have a ‘double’ showing like that:

    • Category1(shown as parent)
      -subcategory1 (shown as a child)
    • Subcatgeory1 (shown as parent too)
      More of that, I don’t know why I can’t select any of the categories in the list.
      Any suggestions?
  7. Javix il 12 March 2009 alle 14:26 dice:

    In fact, I can’t select any parent element, only its children are selectable. Isn’t it possible to select a ‘parent’ no matter if he has children or not.Thank you.

  8. Laura il 16 December 2011 alle 21:37 dice:

    Thanks! it works perfectly!

Scrivi un commento