Newer
Older
dmpopidor / app / views / shared / _accessible_combobox.html.erb
<% if !models.nil? %>
  <% json = {} %>
  <% models.map{|m| json[m[attribute]] = m.id} %>
  <% err_msg = error ||= _('Please select an item from the list.') %>

  <input type="text" name="<%= name %>" id="<%= id %>" 
         class="js-combobox <%= classes %>" list="<%= id %>_list" 
         placeholder="<%= _('Begin typing to see a filtered list') %>"
         data-combobox-prefix-class="combobox"
         data-combobox-case-sensitive="no"
         data-combobox-search-option="containing"
         data-combobox-allow-suggestion-on-empty="true"
         value="<%= default_selection[attribute] unless default_selection.nil? %>" />
  <datalist id="<%= id %>_list">
    <% models.each do |model| %>
      <option value="<%= model[attribute] %>" />
    <% end %>
  </datalist>

  <!-- The JQuery accessible combobox doesn't allow for an id to be present in the value attribute -->
  <!-- so we use this hidden input to store the selected id -->
  <!-- the selected id is found after the combobox loses focus by finding the selected item in the crosswalk json -->
  <input type="hidden" id="<%= id.gsub("_#{attribute}", "_id") %>" name="<%= name.gsub("_#{attribute}]", "_id]") %>"
         value="<%= default_selection.id unless default_selection.nil? %>" />

  <span role="" id="<%= id %>_error" class="error-tooltip left-indent"></span>
  
<% else %>
  <span id="<%= id %>_no_items"><%= _('No items available.') %></span>
<% end %>