Class: TableRowComponent

Inherits:
Object
  • Object
show all
Includes:
HTMLComponent
Defined in:
lib/html-native/collections.rb

Overview

TableRowComponent represents an HTML table row generated from an Enumerable collection.

Attributes in an TableRowComponent are separated into multiple groups since there are multiple kinds of tags. These groups are:

  • row - The attributes associated with the <tr> element

  • cell - The attributes associated with <td> elements

For example, to have a row with 20px padding and the class of “table-cell” given to each cell, you could write: “‘ TableRowComponent.new(data, attributes:

{row: {style: "padding: 20px"}, cell: {class: "list-item"}})

“‘ which is equivalent to “` <tr style=“padding: 20px”>

<td class="list-item">...</td>
<td class="list-item">...</td>
...

</tr> “‘

Constant Summary

Constants included from HTMLComponent

HTMLComponent::FORBIDDEN_ATTRIBUTES, HTMLComponent::LIMITED_ATTRIBUTES, HTMLComponent::TAG_LIST

Instance Method Summary collapse

Methods included from HTMLComponent

#_if, #_label, #_unless, #doctype, singleton, #valid_attribute?

Constructor Details

#initialize(data, attributes: {}, &block) ⇒ TableRowComponent

Creates a new instance of TableRowComponent from the values of data.

If a block is given, each item in data is passed to it to render the row cells. If no block is given, data are used directly.



219
220
221
222
223
224
# File 'lib/html-native/collections.rb', line 219

def initialize(data, attributes: {}, &block)
  @data = data
  @row_attributes = attributes[:row] || {}
  @cell_attributes = attributes[:cell] || {}
  @block = block
end

Instance Method Details

#renderObject

Converts the TableRowComponent instance to the equivalent HTML.

render can be called directly, but that usually isn’t necessary. HTMLComponent::Builder handles this automatically, so it only needs to be done if there is no prior instance of one.



231
232
233
234
235
236
237
# File 'lib/html-native/collections.rb', line 231

def render
  tr(@row_attributes) do
    @data.component_map do |c|
      td(@cell_attributes) {@block ? @block.call(c) : c}
    end
  end
end