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
Afghanistan Kabul 43,844,000 Asia Yes
Albania Tirana 2,363,314 Europe Yes
Algeria Algiers 47,400,000 Africa Yes
Andorra Andorra la Vella 88,406 Europe Yes
Angola Luanda 36,170,961 Africa Yes
Antigua and Barbuda Saint John's 103,603 Americas Yes
Argentina Buenos Aires 46,735,004 Americas Yes
Armenia Yerevan 3,076,200 Asia Yes
Australia Canberra 27,536,874 Oceania Yes
Austria Vienna 9,200,931 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.