Rails
Vložit
- čas přidán 8. 05. 2024
- Datatables - tables that you can search, sort, and paginate.
Gem Ransack is an great tool that I use in most of my apps to add search and sort.
Let's dive deep into how we can use Ransack!
At the end, we will also add pagination of our search results with gem Pagy.
When I was starting this channel, a "Ransack" video was one of my first ideas.
But ransack is a tool that has so many features, I just didn't know how to approach it in an easy-to-learn manner.
I hope you enjoy the example usecases I demonstrate in this screencast.
Episode source code: github.com/corsego/147-datata...
0:00 DataTables example
1:40 Ransack SORT
7:13 Ransack SEARCH
9:15 highlight search results
10:15 Date search fields
11:53 Select fields
13:45 One search field for many attributes
14:50 Search association
19:43 Ransack settings
22:08 Pagination with Pagy
24:20 Search, Sort and Pagination works!
Thank you for these tutorials. I like that you make mistakes in the video, and then resolve them. It helps others learn how to diagnose errors.
Exactly what I was looking for, thank you.
Як завжди - чудовий і дуже корисний матеріал!
Ярослав, ти майстер своєї справи!
Дякую.
Прим. Якби була трохи складніша розмітка, може б запхнув би цю таблицю (разом із сортуванням, фільтрацією) у турбо фрейм щоб оновлювати тільки її контент.
Слава Україні!
turbo frames and complex markup problems? ok, in the next video!
yep, it will be a greate tutorial.@@SupeRails
In my case, I already use ransak, pagy, turbo stream and frame in tables (like you did tutrorials before czcams.com/video/A9q6YwhLCyI/video.html)
But I have issues with sort_link, when I take filtered data.
By the way, thanks a lot, and plz. continue your greate work
@@volodymyrshabaldas6349 what kind of issues with sort link?
It display turbo request in UI (views), when I tried to press sort link after filtered data @@SupeRails
@@volodymyrshabaldas6349
If both turbo_request and turbo_stream are used within one action in the controller, it would be better to separate them to avoid complicating the logic of their interaction.
In my opinion, it is better to have turbo_frame + broadcast or turbo_stream + broadcast. Although there are exceptions in real projects)
thanks sir for the tutorial it was really helpful
Thank you
Gem Ransack: github.com/activerecord-hackery/ransack
Thanks, Yaroslav! Your videos are always interesting and useful! I have one question, though. When you do the sort, the gem makes it very easy, and when you click on the column's title it sorts the column right away. My question is, once I load the page how can I have some kind of default sorting already executed, without clicking on any particular column's title? So, the table should have some initial sort (let's say by first_name: DSC) and then the user could change it by clicking on the different column's title.
if you are using ransack, the best way to set default sort order is with "@q.sorts".
Example:
@q = Event.where(visible: true).ransack(params[:q])
@q.sorts = "name asc" if @q.sorts.empty?
@events = @q.result(distinct: true)
without ransack:
@events = Event.all.order(created_at: :desc)
there is rsome issue of ransack with rails 7.1 , don't know how much time will it take to be compatible with rails 7.1
actually for this tutorial I am using rails 7.1 - seems to work!