Module: Datagrid::Helper

Defined in:
lib/datagrid/helper.rb

Defined Under Namespace

Classes: HtmlRow

Instance Method Summary collapse

Instance Method Details

#datagrid_form_for(grid, options = {}) ⇒ Object

Renders HTML for for grid with all filters inputs and lables defined in it

Supported options:

  • :partials - Path for form partial lookup. Default: 'datagrid'.

  • All options supported by Rails form_for helper


96
97
98
# File 'lib/datagrid/helper.rb', line 96

def datagrid_form_for(grid, options = {})
  datagrid_renderer.form_for(grid, options)
end

#datagrid_format_value(grid, column_name, model) ⇒ Object

:nodoc:


20
21
22
# File 'lib/datagrid/helper.rb', line 20

def datagrid_format_value(grid, column_name, model) #:nodoc:
  datagrid_value(grid, column_name, model)
end

#datagrid_header(grid, options = {}) ⇒ Object

Renders HTML table header for given grid instance using columns defined in it

Supported options:

  • :order - display ordering controls built-in into header Default: true

  • :partials - Path for partials lookup. Default: 'datagrid'.


53
54
55
# File 'lib/datagrid/helper.rb', line 53

def datagrid_header(grid, options = {})
  datagrid_renderer.header(grid, options)
end

#datagrid_order_for(grid, column, options = {}) ⇒ Object

Renders ordering controls for the given column name

Supported options:

  • :partials - Path for partials lookup. Default: 'datagrid'.


85
86
87
# File 'lib/datagrid/helper.rb', line 85

def datagrid_order_for(grid, column, options = {})
  datagrid_renderer.order_for(grid, column, options)
end

#datagrid_order_path(grid, column, descending) ⇒ Object

Generates an ascending or descending order url for the given column


120
121
122
# File 'lib/datagrid/helper.rb', line 120

def datagrid_order_path(grid, column, descending)
  datagrid_renderer.order_path(grid, column, descending, request)
end

#datagrid_row(grid, asset, &block) ⇒ Object

Provides access to datagrid columns data.

# Suppose that <tt>grid</tt> has first_name and last_name columns
<%= datagrid_row(grid, user) do |row| %>
  <tr>
    <td><%= row.first_name %></td>
    <td><%= row.last_name %></td>
  </tr>
<% end %>

Used in case you want to build html table completelly manually


111
112
113
114
115
116
117
# File 'lib/datagrid/helper.rb', line 111

def datagrid_row(grid, asset, &block)
  HtmlRow.new(self, grid, asset).tap do |row|
    if block_given?
      return capture(row, &block)
    end
  end
end

#datagrid_rows(grid, assets = grid.assets, **options, &block) ⇒ Object

Renders HTML table rows using given grid definition using columns defined in it. Allows to provide a custom layout for each for in place with a block

Supported options:

  • :columns - Array of column names to display. Used in case when same grid class is used in different places and needs different columns. Default: all defined columns.

  • :partials - Path for partials lookup. Default: 'datagrid'.

    datagrid_rows(grid) # Generic table rows Layout

    datagrid_rows(grid) do |row| # Custom Layout

    %tr
      %td= row.project_name
      %td.project-status{class: row.status}= row.status
    

75
76
77
# File 'lib/datagrid/helper.rb', line 75

def datagrid_rows(grid, assets = grid.assets, **options, &block)
  datagrid_renderer.rows(grid, assets, **options, &block)
end

#datagrid_table(grid, *args) ⇒ Object

Renders html table with columns defined in grid class. In the most common used you need to pass paginated collection to datagrid table because datagrid do not have pagination compatibilities:

assets = grid.assets.page(params[:page])
datagrid_table(grid, assets, options)

Supported options:

  • :html - hash of attributes for <table> tag

  • :order - If false do not generate ordering controlls. Default: true.

  • :columns - Array of column names to display. Used in case when same grid class is used in different places and needs different columns. Default: all defined columns.

  • :partials - Path for partials lookup. Default: 'datagrid'.


41
42
43
# File 'lib/datagrid/helper.rb', line 41

def datagrid_table(grid, *args)
  datagrid_renderer.table(grid, *args)
end

#datagrid_value(grid, column_name, model) ⇒ Object

Returns individual cell value from the given grid, column name and model Allows to render custom HTML layout for grid data

<ul>
  <% @grid.columns.each do |column|
    <li><%= column.header %>: <%= datagrid_value(@grid, column.name, @resource %></li>
  <% end %>
</ul>

16
17
18
# File 'lib/datagrid/helper.rb', line 16

def datagrid_value(grid, column_name, model)
  datagrid_renderer.format_value(grid, column_name, model)
end