class Erector::Widgets::Table

The Table widget provides the ability to render a table from a list of objects (one for each row).

Because the default for the column titles utilizes the ActiveSupport Inflector#titleize method, this widget requires active_support to be loaded (or some other gem that adds `titleize` to String).

class UsersTable < Erector::Widgets::Table
  column :first_name
  column :last_name
  column :email
  row_classes :even, :odd
end

widget UsersTable, :row_objects => [user_1, user_2, user_3]

Constants

ColumnDefinition

Attributes

row_class_list[R]

Public Class Methods

column(id, name=id.to_s.titleize, &cell_proc) click to toggle source

Define a column, optionally specifying the name (the heading that the user sees) and a block which renders the cell given a row object. If the block is not specified, the cell contains the result of calling a method whose name is id.

The name can be a string or a proc.

# File lib/erector/widgets/table.rb, line 27
def column(id, name=id.to_s.titleize, &cell_proc)
  cell_proc ||= proc {|object| text object.__send__(id)}
  column_definitions << ColumnDefinition.new(id, name, cell_proc)
end
row_classes(*row_classes) click to toggle source

A list of HTML classes to apply to the rows in turn. After the list is exhausted, start again at the start. The most common use for this is to specify one class for odd rows and a different class for even rows.

# File lib/erector/widgets/table.rb, line 40
def row_classes(*row_classes)
  @row_class_list = row_classes
end

Public Instance Methods

content() click to toggle source

The standard erector content method.

# File lib/erector/widgets/table.rb, line 47
def content
  table do
    thead do
      tr do
        column_definitions.each do |column_def|
          th do
            if column_def.name.is_a?(Proc)
              self.instance_exec(column_def.id, &column_def.name)
            else
              text column_def.name
            end
          end
        end
      end
    end
    tbody do
      @row_objects.each_with_index do |object, index|
        row object, index
      end
    end
  end
end

Protected Instance Methods

row_css_class(object, index) click to toggle source

You can override this method to provide a class for a row (as an alternative to calling ::row_classes).

# File lib/erector/widgets/table.rb, line 83
def row_css_class(object, index)
  cycle(index)
end