-
Notifications
You must be signed in to change notification settings - Fork 9
Customizing tables
In this page you can find :
- Customizing table rows actions
- Making the rows orderable
- Customizing automatic cell data output
- Sorting columns
- Customize search and / or add filters
You can generate the table partials to edit shown fields or the whole table view with the following generator :
rails g para:table <your_model_name>
The table is built with helpers to simplify the markup. You can either configure the table with the existing helpers or replace it by a plain HTML table.
In the generated output, use the :actions
option of the resources_table
helper.
Whitelist actions (:edit, :delete) :
= resources_table(model: model, component: component, actions: [:edit]) do |table|
Or disable all actions :
= resources_table(model: model, component: component, actions: false) do |table|
Please refer to the Ordering models page to implement orderable behavior on your model, and the table will automatically be orderable.
You can disable ordering on an orderable model (yes ...) with the :orderable
option of the resources_table
helper :
= resources_table(model: model, component: component, orderable: false) do |table|
You can change the behavior of the table.data_for
helper that generates you cell data.
The first method is to simply override output data with a custom one, allowing you to insert any random data :
# Before
= table.data_for(resource, :name)
# After
= table.data_for(resource.full_name)
The second one, is to force the field to use a specific field type, which will change its output :
= table.data_for(resource, :created_at, :date)
One specific example is the enum
one, which allows you to translate enum values of your model's attributes.
# With resource being an `Order` model
= table.data_for(resource, :state, :enum)
Then in your translations use the following translation key (activerecord.enums.<model_i18n_key>.<field_name>.<field_value>
) :
activerecord:
enums:
order:
state:
payment: "Payment pending"
ready: "Ready to be shipped"
shipped: "Shipped"
All the fields of the table that are columns in the model's table, i.e. which are attributes and not just dynamic methods, are automatically sortable in the table.
Note : Para uses Ransack under the hood with the
sort_link
view helper.
You may want to customize that behavior in 2 ways :
= table.header_for(:name, sort: false)
Let's say we have a model Event
which belongs to Place
, and Place
has a column name
.
In our events table, we may want to see the the place name, and allow users to order the events depending on its place.
This can be achived easily with the following :sort
option configuration :
= table.header_for(:place, sort: :place_name)
Note : all what you pass to the
:sort
option in theheader_for
method is passed to thesort_link
helper, if you want to pass multiple arguments to the Ransack'ssort_link
helper, just pass the:sort
option an array with the different arguments.
Para includes Ransack by default for easy search and filtering generation.
To customize the search, you need to generate the filters partial for your resource with :
rails g para:filters <your_model_name>
A partial containing the search form will be create, where you'll be able to add fields and customize your search form. Note that except for Ransack, no specific tool is provided, but using Ransack will allow you not to have to touch the controller code.