Skip to content

Commit a626a9d

Browse files
committed
Add customizable prev/next labels and hide pagination for single page
Add prev_label and next_label params (default "<" and ">"). Skip rendering when total pages <= 1.
1 parent 5959464 commit a626a9d

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

lib/ruby_ui/data_table/data_table_pagination.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77

88
module RubyUI
99
class DataTablePagination < Base
10-
def initialize(with: nil, pagy: nil, kaminari: nil, page: nil, per_page: nil, total_count: nil, page_param: "page", path: "", query: {}, window: 1, **attrs)
10+
def initialize(with: nil, pagy: nil, kaminari: nil, page: nil, per_page: nil, total_count: nil, page_param: "page", path: "", query: {}, window: 1, prev_label: "<", next_label: ">", **attrs)
1111
@adapter = resolve_adapter(with:, pagy:, kaminari:, page:, per_page:, total_count:)
1212
@page_param = page_param
1313
@path = path
1414
@query = query.to_h.transform_keys(&:to_s)
1515
@window = window
16+
@prev_label = prev_label
17+
@next_label = next_label
1618
super(**attrs)
1719
end
1820

1921
def view_template
22+
return if total <= 1
23+
2024
render RubyUI::Pagination.new(class: "mx-0 w-auto justify-end", **attrs) do
2125
render RubyUI::PaginationContent.new do
2226
prev_item
@@ -56,20 +60,20 @@ def build_query(hash)
5660
def prev_item
5761
if current <= 1
5862
li do
59-
span(class: "opacity-50 pointer-events-none px-3 h-9 inline-flex items-center text-sm") { plain "Previous" }
63+
span(class: "opacity-50 pointer-events-none px-3 h-9 inline-flex items-center text-sm") { plain @prev_label }
6064
end
6165
else
62-
render RubyUI::PaginationItem.new(href: page_href(current - 1)) { plain "Previous" }
66+
render RubyUI::PaginationItem.new(href: page_href(current - 1)) { plain @prev_label }
6367
end
6468
end
6569

6670
def next_item
6771
if current >= total
6872
li do
69-
span(class: "opacity-50 pointer-events-none px-3 h-9 inline-flex items-center text-sm") { plain "Next" }
73+
span(class: "opacity-50 pointer-events-none px-3 h-9 inline-flex items-center text-sm") { plain @next_label }
7074
end
7175
else
72-
render RubyUI::PaginationItem.new(href: page_href(current + 1)) { plain "Next" }
76+
render RubyUI::PaginationItem.new(href: page_href(current + 1)) { plain @next_label }
7377
end
7478
end
7579

0 commit comments

Comments
 (0)