8000 TableFor, add tbody_data and row_data options · activeadmin/activeadmin@0340409 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0340409

Browse files
committed
TableFor, add tbody_data and row_data options
This patch adds a `tbody_data` and `row_data` options to the `TableFor` and `IndexAsTable` classes. These options allow you to pass a hash of data attributes to the `tbody` and `tr` elements respectively. This is useful when you want to add features to the table that require data attributes. For example, adding a sortable row via JavaScript.
1 parent d037b36 commit 0340409

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

docs/3-index-pages/index-as-table.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ index do
196196
end
197197
```
198198

199+
## Custom tbody data attributes
200+
201+
In order to add special data attributes to table tbody pass a `:tbody_data` option of the `index` method.
202+
203+
```ruby
204+
index tbody_data: { controller: 'stimulus-controller' } do
205+
# columns
206+
end
207+
```
208+
199209
## Custom row class
200210

201211
In order to add special class to table rows pass the proc object as a `:row_class` option
@@ -206,3 +216,14 @@ index row_class: ->elem { 'active' if elem.active? } do
206216
# columns
207217
end
208218
```
219+
220+
## Custom row data attributes
221+
222+
In order to add special data attributes to table rows pass the proc object as a `:row_data` option of the `index` method.
223+
224+
```ruby
225+
index row_data: ->elem { 8000 'element-id' => elem.id } do
226+
# columns
227+
end
228+
```
229+

lib/active_admin/views/components/table_for.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def build(obj, *attrs)
1616
@resource_class ||= @collection.klass if @collection.respond_to? :klass
1717

1818
@columns = []
19+
@tbody_data = options.delete(:tbody_data)
1920
@row_class = options.delete(:row_class)
21+
@row_data = options.delete(:row_data)
2022

2123
build_table
2224
super(options)
@@ -91,10 +93,10 @@ def build_table_header(col)
9193
end
9294

9395
def build_table_body
94-
@tbody = tbody do
96+
@tbody = tbody data: @tbody_data do
9597
# Build enough rows for our collection
9698
@collection.each do |elem|
97-
tr(id: dom_id_for(elem), class: @row_class&.call(elem))
99+
tr(id: dom_id_for(elem), class: @row_class&.call(elem), data: @row_data&.call(elem))
98100
end
99101
end
100102
end

lib/active_admin/views/index_as_table.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ def build(page_presenter, collection)
215215
sortable: true,
216216
i18n: active_admin_config.resource_class,
217217
paginator: page_presenter[:paginator] != false,
218-
row_class: page_presenter[:row_class]
218+
tbody_data: page_presenter[:tbody_data],
219+
row_class: page_presenter[:row_class],
220+
row_data: page_presenter[:row_data]
219221
}
220222

221223
if page_presenter.block

spec/unit/views/components/table_for_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,21 @@
294294
end
295295
end
296296

297+
context "when tbody_data" do
298+
let(:table) do
299+
render_arbre_component assigns, helpers do
300+
table_for(collection, tbody_data: { size: collection.size }) do
301+
column :starred
302+
end
303+
end
304+
end
305+
306+
it "should render data-size attribute within tbody tag" do
307+
tbody = table.find_by_tag("tbody").first
308+
expect(tbody.attributes).to include(data: { size: 3 })
309+
end
310+
end
311+
297312
context "when row_class" do
298313
let(:table) do
299314
render_arbre_component assigns, helpers do
@@ -313,6 +328,25 @@
313328
end
314329
end
315330

331+
context "when row_data" do
332+
let(:table) do
333+
render_arbre_component assigns, helpers do
334+
table_for(collection, row_data: -> e { { title: e.title } }) do
335+
column :title
336+
end
337+
end
338+
end
339+
340+
it "should render data-title attribute within collection row" do
341+
trs = table.find_by_tag("tr")
342+
expect(trs.size).to eq 4
343+
expect(trs.first.attributes.to_s).to eq ""
344+
expect(trs.second.attributes).to include(data: { title: "First Post" })
345+
expect(trs.third.attributes).to include(data: { title: "Second Post" })
346+
expect(trs.fourth.attributes).to include(data: { title: "Third Post" })
347+
end
348+
end
349+
316350
context "when i18n option is specified" do
317351
around do |example|
318352
with_translation %i[activerecord attributes post title], "Name" do

0 commit comments

Comments
 (0)
0