Module: RailsBootstrapWidgets::FiltersHelper

Defined in:
app/helpers/rails-bootstrap-widgets/filters_helper.rb

Instance Method Summary collapse

Instance Method Details

#filters_widget(items, options = {}) ⇒ Object

To create html header for filtering, ordering and paging a list of items call filters_widget(items, options = {}) with parameters:

filters_widget (
  @items,                                                       # list of items (required)
  filters: { published: :Published, unpublished: :Unpublished}, # list of filters to select from in a format { key: :name }
  filter: 'published',                                          # key of the current filter to apply to items
  orders: { tree: :Tree, feed: :Feed },                         # list of types of items ordering to select from
  order: 'tree'                                                 # key of the current order type
)

This will provide html, that contains:

  1. Input field to select filter from those are available

  2. Input field to select order from those are available

  3. Hidden button to reset filter values (it will authomatically displayed via js)

  4. Button to reload the page with new filter values (it will authomatically removed via js)

  5. Items pager

<b>Use only one such a call on the page</p>

For example, the call of the code above will provide html:

<form accept-charset="UTF-8" action="/" class="span8" id="filters" method="get">
  <div style="margin:0;padding:0;display:inline">
    <input name="utf8" type="hidden" value="&#x2713;" />
  </div>
  <div class="input-prepend">
    <span class="add-on">
      <i class="icon-filter"></i>
    </span>
    <select id="filter" name="filter">
      <option value="published" selected="selected">Published</option>
      <option value="unpublished">Unpublished</option>
    </select>
  </div>
  <div class="input-prepend">
    <span class="add-on">
      <i class="icon-order"></i>
    </span>
    <select id="order" name="order">
      <option value="tree" selected="selected">Tree</option>
      <option value="feed">Feed</option>
    </select>
  </div>
  <a href="#" class="btn add-on hide" id="reset">
    <i class="icon-reset"></i>
  </a>
  <div class="input-prepend" id="reload">
    <span class="add-on">
      <i class="icon-reload"></i>
    </span>
    <input class="btn" name="commit" type="submit" value="Reload" />
  </div>
</form>
<aside class="span4">
  <!-- Standard result of 'will_paginate(items, inner_window: 0, outer_window: 0)' call -->
</aside>

This code provides basic html functionality for browsers with js suppord disabled. For those with js support is enabled, the file app/assets/javascripts/rails-bootstrap-widgets/filters.js.coffee turns on extended ajax behavior.



81
82
83
84
85
86
87
88
89
# File 'app/helpers/rails-bootstrap-widgets/filters_helper.rb', line 81

def filters_widget(items, options = {})
  if (options = _prepare(options)).present?
    form_tag(url_for("/"), method: :get, id: :filters, class: :span8) do
      (_selector(:filter, options[:filters], options[:filter]) << _selector(:order, options[:orders], options[:order]) << _reset << _reload).html_safe
    end
  else
    ""
  end << pagination_widget(items, class: :span4)
end

#pagination_widget(items, options = {}) ⇒ Object

To add pager to a view call pagination_widget(items, options = {}) with parameters:

pagination_widget items, class: 'class name'

This will simply wrap standard pagination from the ‘will-paginate’ gem into <aside class='class name'> tag

<aside class='class name'>
  <!-- Standard result of 'will_paginate(items, inner_window: 0, outer_window: 0)' call -->
</aside>


14
15
16
17
18
19
20
# File 'app/helpers/rails-bootstrap-widgets/filters_helper.rb', line 14

def pagination_widget(items, options = {})
  if items.present?
    (:aside, (options[:class].to_s.present? ? { class: options[:class] } : {})) do
      will_paginate(items, inner_window: 0, outer_window: 0).html_safe
    end
  end
end