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
El Salvador San Salvador 6,029,976 Americas Yes
Equatorial Guinea Malabo 1,668,768 Africa Yes
Eritrea Asmara 3,607,000 Africa Yes
Estonia Tallinn 1,369,995 Europe Yes
Eswatini Mbabane 1,235,549 Africa Yes
Ethiopia Addis Ababa 111,652,998 Africa Yes
Fiji Suva 900,869 Oceania Yes
Finland Helsinki 5,650,325 Europe Yes
France Paris 66,351,959 Europe Yes
Gabon Libreville 2,469,296 Africa 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.