Table Edit

A simple table component for displaying data in rows and columns.

Examples

Title Artist
Total Eclipse of the Heart Bonnie Tyler
I Will Always Love You Whitney Houston
I Wanna Dance with Somebody Whitney Houston
I Will Survive Gloria Gaynor
My Heart Will Go On Celine Dion

Installation


  rails generate shadcn-ui table

Usage

<%= render_table do %>
  <%= table_head do %>
    <%= table_header %>
    <%= table_header %>
  <% end %>
  <%= table_body do %>
    <%= table_row do %>
      <%= table_column %>
      <%= table_column %>
    <% end %>
  <% end %>
<% end %>

The Table component introduces:

  • app/helpers/components/table_helper.rb

The table component is so far unique in that it is entirely provided by app/helpers/components/table_helper.rb. This helper introduces methods that rely on content_tag to create the markup for the table. To edit anything about the rendered table, look there.

app/helpers/components/table_helper.rb introduces the following methods that can be used to create a table.

  • render_table accepts an optional caption as the first argument along with the block for the rest of the table components.
  • table_head accepts a block for the content of the thead row.
  • table_header accepts the header content inline or in a block. This will be rendered within the thead for that column.
  • table_body accepts a block for the content of the tbody.
  • table_row accepts a block for the content of the tr.
  • table_column accepts the column content inline or in a block. This will be rendered within the td for the column.