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
Canada Ottawa 41,651,653 Americas Yes
Cape Verde Praia 491,233 Africa Yes
Central African Republic Bangui 6,470,307 Africa Yes
Chad N'Djamena 19,340,757 Africa Yes
Chile Santiago 20,206,953 Americas Yes
China Beijing 1,408,280,000 Asia Yes
Colombia Bogotá 53,057,212 Americas Yes
Comoros Moroni 919,901 Africa Yes
Costa Rica San José 5,309,625 Americas Yes
Croatia Zagreb 3,866,233 Europe 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.