Class: Superview::Components::TableComponent

Inherits:
ApplicationComponent
  • Object
show all
Includes:
Phlex::DeferredRender
Defined in:
lib/superview/components/table_component.rb

Overview

Renders an HTML table for a collection. Each item is passed into the collection of the table.

“‘ruby render TableComponent.new(@posts) do |table|

# This is how you'd usually render a table.
table.column("Title") { show(_1, :title) }

# If you need to render HTML in the title, add a `column` argument
# to the block and call `title` or `item` on it.
table.column do |column|
  # Titles might not always be text, so we need to handle rendering
  # Phlex markup within.
  column.title do
    link_to(user_blogs_path(@current_user)) { "Blogs" }
  end
  column.item { show(_1.blog, :title) }
end

end “‘

Defined Under Namespace

Classes: Column

Instance Method Summary collapse

Constructor Details

#initialize(items = [], **attributes) ⇒ TableComponent

Returns a new instance of TableComponent.



54
55
56
57
58
# File 'lib/superview/components/table_component.rb', line 54

def initialize(items = [], **attributes)
  @items = items
  @attributes = attributes
  @columns = []
end

Instance Method Details

#before_templateObject

Phlex 2.0



29
30
31
32
# File 'lib/superview/components/table_component.rb', line 29

def before_template(&)
  vanish(&)
  super
end

#column(title = nil, &block) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/superview/components/table_component.rb', line 81

def column(title = nil, &block)
  @columns << if title
    Column.build(title: title, &block)
  else
    Column.new.tap(&block)
  end
end

#view_templateObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/superview/components/table_component.rb', line 60

def view_template(&)
  table(**@attributes) do
    thead do
      tr do
        @columns.each do |column|
          th(&column.title_template)
        end
      end
    end
    tbody do
      @items.each do |item|
        tr do
          @columns.each do |column|
            td { column.item_template.call(item) }
          end
        end
      end
    end
  end
end