Module: Datagrid::Helper

Defined in:
lib/datagrid/helper.rb,
lib/datagrid/renderer.rb

Defined Under Namespace

Classes: HtmlRow

Instance Method Summary collapse

Instance Method Details

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

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

Parameters:

Returns:

  • (String)

    form HTML tag markup



108
109
110
# File 'lib/datagrid/helper.rb', line 108

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

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

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

  • :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’.

Parameters:

Returns:

  • (String)

    HTML table header tag markup



62
63
64
# File 'lib/datagrid/helper.rb', line 62

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’.



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

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

#datagrid_order_path(grid, column, descending) ⇒ String

Generates an ascending or descending order url for the given column

Parameters:

  • grid (Datagrid)

    grid object

  • column (Datagrid::Columns::Column, String, Symbol)

    column name

  • descending (Boolean)

    specifies order direction. Ascending if false, otherwise descending.

Returns:

  • (String)

    order layout HTML markup



141
142
143
# File 'lib/datagrid/helper.rb', line 141

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

#datagrid_row(grid, asset, **options, &block) ⇒ Datagrid::Helper::HtmlRow, String

Provides access to datagrid columns data. Used in case you want to build html table completelly manually

Examples:

# Suppose that grid 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 %>
<% row = datagrid_row(grid, user) %>
First Name: <%= row.first_name %>
Last Name: <%= row.last_name %>
<%= datagrid_row(grid, user, columns: [:first_name, :last_name, :actions]) %>

Parameters:

  • grid (Datagrid)

    grid object

  • asset (Object)

    object from grid scope

  • block (Proc)

    block with Datagrid::Helper::HtmlRow as an argument returning a HTML markup as a String

Returns:



132
133
134
# File 'lib/datagrid/helper.rb', line 132

def datagrid_row(grid, asset, **options, &block)
  datagrid_renderer.row(grid, asset, **options, &block)
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’.

Examples:

= 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


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

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

#datagrid_table(grid, assets = grid.assets, **options) ⇒ String

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: 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’.

Examples:

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

Parameters:

  • grid (Datagrid)

    grid object

  • assets (Array) (defaults to: grid.assets)

    objects from grid scope

Returns:

  • (String)

    table tag HTML markup



45
46
47
# File 'lib/datagrid/helper.rb', line 45

def datagrid_table(grid, assets = grid.assets, **options)
  datagrid_renderer.table(grid, assets, **options)
end

#datagrid_value(grid, column, model) ⇒ Object

Returns individual cell value from the given grid, column name and model.

Examples:

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

Parameters:

Returns:

  • (Object)

    individual cell value from the given grid, column name and model



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

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