Datatables

Datatable components with sorting, filtering, and pagination.

Full Example#

A full example of a datatable with sorting, filtering, and pagination.

Reset
Name
 
Capital Population
Region UN Member OpenStreetMap
Madagascar Antananarivo 31,727,042 Africa Yes
Malawi Lilongwe 20,734,262 Africa Yes
Malaysia Kuala Lumpur 34,231,700 Asia Yes
Maldives Malé 515,132 Asia Yes
Mali Bamako 22,395,489 Africa Yes
Malta Valletta 574,250 Europe Yes
Marshall Islands Majuro 42,418 Oceania Yes
Mauritania Nouakchott 4,927,532 Africa Yes
Mauritius Port Louis 1,243,741 Africa Yes
Mexico Mexico City 130,575,786 Americas Yes
class CountriesTable < RapidUI::Datatable::Base
  extension :bulk_actions
  extension :export
  extension :select_filters

  adapter :array
  adapter :rails

  columns do |t|
    t.string :name, sortable: true, searchable: true
    t.string :capital
    t.integer :population, sortable: true, sort_order: "desc"
    t.string :region
    t.boolean :un_member, label: "UN Member"
    t.string :openstreetmap, label: "OpenStreetMap"
  end

  self.sort_column = :name
  self.available_per_pages = [ 10, 25, 50, 100 ]
  self.per_page = 10

  # Display options
  self.responsive = true
  self.striped = true
  self.hover = true
  self.bordered = true

  attr_accessor :reset_button_disabled

  self.header_controls = [ :bulk_actions, %i[region_filter search_field_form reset_bulk_action] ]
  self.footer_controls = %i[per_page pagination exports]

  bulk_action :delete

  cell_value :openstreetmap, :html do |record|
    link_to helpers.icon("globe", size: 16), record.openstreetmap, target: "_blank"
  end

  select_filter :region,
    choices: ->(scope) { scope.map(&:region).uniq.sort },
    filter: ->(scope, value) { scope.keep_if { |record| record.region == value } }

  register_control :reset_bulk_action, ->(**kwargs) do
    build(
      RapidUI::Button,
      "Reset",
      path: table.component_path(view_context:, action: "bulk_action", bulk_action: "reset"),
      class: "btn btn-outline-naked",
      disabled: table.reset_button_disabled,
      data: { turbo_stream: true, turbo_method: :post },
    )
  end

  def skip_reset_bulk_action?
    reset_button_disabled.nil?
  end
end

Features#

RapidUI::Datatable::Base has these features built-in:

  • Columns — Define which columns appear in the table, their headers, how values are rendered and grouped.
  • Pagination — Split the dataset into pages so users can navigate through large result sets.
  • Search — Provide a search box that filters rows across specified columns.
  • Sorting — Allow users to sort rows by specific columns.

Extensions#

Optional extensions that add additional features to the datatable.

  • Bulk Actions — Enable selection of multiple rows and run actions on the selected set.
  • Export — Let users export the full dataset to CSV or JSON.
  • Select Filters — Filter rows by a specific value using a dropdown.

Adapters#

Provides support for different data sources and Ruby libraries.

  • ActiveRecord — use ActiveRecord models as the data source.
  • Array — use raw Ruby arrays as the data source.
  • Kaminari — Support for Kaminari pagination.