简体   繁体   中英

add chosen-select in the helpers method rails

I am trying to use chosen-select for my options. I am not using any form field or form builder.

I am doing it via the helper method

 def options_for_part_child(part_child)
 attr_name = attribute_name(part_child.parent, part_child, 'id')
 html = "<div class='col-sm-2 uno_part_wrapper'>"
 html += "<select class='form-control switcher chosen-select'
                name='#{attr_name}'
                data-part-name='#{part_child.name}'
                data-part-id='#{part_child.id}'
                data-part-type='#{part_child.display_type}'
                data-parent-part-id='#{part_child.parent.id unless part_child.root?}'>"
 part_child.options.each do |o|
 html += "<option value='#{o.id}'
                data-option-part-id='#{part_child.id}'
                data-option-name='#{o.name}'
                data-option-id='#{o.id}'
                data-option-disables='#{o.disables.present? ? o.disables.map(&:disable_element_id) : nil}'
                data-option-enables='#{o.enables.present? ? o.enables.map(&:enable_element_id) : nil}'
                #{o.is_default? ? 'selected' : ''}>#{o.name}
          </option>"
 end
 html += "</select>"
 html += "</div>"
 html.html_safe
 end

This is how I am doing the selection.

Can someone please help me getting it worked with the chosen-select jquery plugin.

You can make the initialization on your page in a script tag.

<script type="text/javascript">
  $(document).ready(function(){  
    $('select.chosen-select').chosen()
  });
</script>

If this is used on multiple pages then better to use it in your custom js file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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