Collection
Feel free to check out the component specs.
The collection component is designed to
display instances from a model (Active Record or similar)
filter the displayed instances without full page reload
paginate the displayed instances without full page reload
order the displayed instances without full page reload
The collection component should be as flexible as possible while still reducing the complexity of implementing all typical collection features by hand.
Prerequisites
We use an ActiveRecord Model in the following examples. This Model has two columns: id and title.
Examples
Filterable collection
In this example, we want to display ALL instances of DummyModel and filter the collection by title using a text input.
class Pages::MyApp::Collection < Matestack::Ui::Page
include Matestack::Ui::Core::Collection::Helper
def prepare
my_collection_id = "my-first-collection"
current_filter = get_collection_filter(my_collection_id)
my_base_query = DummyModel.all
my_filtered_query = my_base_query
.where("title LIKE ?", "%#{current_filter[:title]}%")
@my_collection = set_collection({
id: my_collection_id,
data: my_filtered_query
})
end
def response
heading size: 2, text: "My Collection"
filter
# the content has to be wrapped in an `async` component
# the event has to be "your_custom_collection_id" + "-update"
async rerender_on: "my-first-collection-update", id: "my-collection-content" do
content
end
end
def filter
collection_filter @my_collection.config do
collection_filter_input key: :title, type: :text, placeholder: "Filter by Title"
collection_filter_submit do
button text: "filter"
end
collection_filter_reset do
button text: "reset"
end
end
end
def content
collection_content @my_collection.config do
ul do
@my_collection.data.each do |dummy|
li do
plain dummy.title
end
end
end
end
end
endFiltered & paginated collection
In this example, we want to display only a limited (10) amount of instances of DummyModel at once and filter the collection by title using a text input. We want to display the classic pagination buttons and information below the list of paginated instances.
Filtered & paginated & ordered collection
In this example, we want to display only a limited (10) amount of instances of DummyModel at once and filter the collection by title using a text input. We want to display the classic pagination buttons and information below the list of paginated instances. Additionally, we want to order the collection by title ascending or descending.
Action enriched collection
In this example, we want to display ALL instances of DummyModel and filter the collection by title using a text input. Additionally we want to be able to delete an item of the list using the action component.
Last updated
Was this helpful?