Module: Bootstrap5RailsExtensions::TableHelper

Defined in:
app/helpers/bootstrap5_rails_extensions/table_helper.rb

Defined Under Namespace

Classes: TableBuilder

Instance Method Summary collapse

Instance Method Details

#render_table(data: {}, wrapper_class: nil, wrapper_html: {}, **html_options) {|builder| ... } ⇒ Object

Bootstrapのtableコンポーネント向け簡易DSL。

使い方:

<%= render_table class: "table-striped" do |table| %>
  <%= table.thead class: "table-light" do %>
    <tr><th>

Yields:

  • (builder)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/bootstrap5_rails_extensions/table_helper.rb', line 16

def render_table(data: {}, wrapper_class: nil, wrapper_html: {}, **html_options)
  raise ArgumentError, "theadかtbodyを定義してください" unless block_given?

  builder = TableBuilder.new(self)
  yield(builder)

  raise ArgumentError, "theadかtbodyを最低1つは定義してください" if builder.empty?

  html_options[:class] = build_table_class(html_options[:class])
  html_options[:data] = merge_data_attributes(html_options[:data], data) if data.present?

  wrapper_options = build_wrapper_options(wrapper_html, wrapper_class)

  (:div, **wrapper_options) do
    (:table, builder.render, **html_options)
  end
end