|
1 | 1 | # activeadmin_table_footer |
2 | 2 |
|
3 | | -Adds a `<tfoot>` row to ActiveAdmin index tables with a simple per-column DSL |
4 | | -and an optional single-query aggregate shared across cells. |
| 3 | +Adds a `<tfoot>` row to ActiveAdmin index tables. Totals are aggregated |
| 4 | +across **all pages** of the filtered scope — not just the visible one — in a |
| 5 | +single SQL query when you use `footer_data:`. |
5 | 6 |
|
6 | 7 | Works with **ActiveAdmin 3.5+ and 4.x**. |
7 | 8 |
|
8 | | - |
| 9 | +### ActiveAdmin 4 |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +### ActiveAdmin 3 |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +The page shows 30 rows, but the footer row reports the sum across all 42 |
| 18 | +subscriptions — that's the point. |
9 | 19 |
|
10 | 20 | ## Install |
11 | 21 |
|
@@ -36,70 +46,14 @@ ActiveAdmin.register Subscription do |
36 | 46 | end |
37 | 47 | ``` |
38 | 48 |
|
39 | | -`footer_data:` runs **once** against the filtered scope (without |
40 | | -`LIMIT/OFFSET/ORDER`), so totals cover every page — not just the visible |
41 | | -slice. The result is exposed inside each `column :x, footer:` Proc via the |
42 | | -`footer_data` method. |
43 | | - |
44 | | -## `footer:` values |
45 | | - |
46 | | -| Value | Behavior | |
47 | | -|---|---| |
48 | | -| `String` / Numeric | Rendered as-is | |
49 | | -| `:sum`, `:count`, `:average`, `:minimum`, `:maximum` | Aggregate over the (unscoped) collection — SQL for AR, Ruby for plain Arrays | |
50 | | -| `Proc` (arity 0) | Run inside the table — view helpers (`number_to_currency`, `link_to`), Arbre tags (`strong`, `span`), and `footer_data` work | |
51 | | -| `Proc` (arity 1) | Receives the unscoped collection as argument | |
52 | | -| `Arbre::Element` | Inserted directly | |
53 | | - |
54 | | -Columns without `:footer` render an empty cell so widths align with the |
55 | | -header. `<tfoot>` itself appears only when at least one column has a footer — |
56 | | -regular tables are untouched. |
57 | | - |
58 | | -## Plain-Array tables |
59 | | - |
60 | | -Works the same when `table_for` gets a plain Array (custom panels, dashboards): |
61 | | - |
62 | | - |
63 | | - |
64 | | -```ruby |
65 | | -table_for items do # items = [DashboardItem.new(...), ...] |
66 | | - column :name, footer: -> { strong { "Totals" } } |
67 | | - column :qty, footer: :sum |
68 | | - column :price, footer: ->(arr) { number_to_currency(arr.sum(&:price)) } |
69 | | -end |
70 | | -``` |
71 | | - |
72 | | -Symbol aggregators fall back to `Enumerable` (nil-safe) when the collection |
73 | | -isn't an AR relation. |
74 | | - |
75 | | -## Styling |
76 | | - |
77 | | -For AA 4 (Tailwind) sensible defaults are applied. Override globally: |
78 | | - |
79 | | -```ruby |
80 | | -# config/initializers/activeadmin_table_footer.rb |
81 | | -ActiveadminTableFooter.configure do |c| |
82 | | - c.footer_th_class = "px-4 py-3 bg-blue-50 font-bold" |
83 | | -end |
84 | | -``` |
85 | | - |
86 | | -For AA 3 (Sass): |
87 | | - |
88 | | -```scss |
89 | | -.index_table tfoot td { |
90 | | - background: #f3f4f6; |
91 | | - font-weight: 600; |
92 | | - border-top: 1px solid #ddd; |
93 | | - padding: 8px 10px; |
94 | | -} |
95 | | -``` |
96 | | - |
97 | | -## Compatibility |
| 49 | +The `footer_data:` Proc runs once over the filtered scope (LIMIT/OFFSET/ORDER |
| 50 | +are stripped automatically). The result is exposed inside each |
| 51 | +`column …, footer: …` Proc via `footer_data`. |
98 | 52 |
|
99 | | -| AA | Status | |
100 | | -|---|---| |
101 | | -| 4.0.0.beta22 | ✅ | |
102 | | -| 3.5 | ✅ | |
| 53 | +`footer:` accepts a string, a symbol (`:sum`, `:count`, `:average`, |
| 54 | +`:minimum`, `:maximum`), or a Proc — Procs run inside the table view, so view |
| 55 | +helpers (`number_to_currency`, `link_to`) and Arbre tags (`strong`, `span`) |
| 56 | +work as expected. |
103 | 57 |
|
104 | 58 | ## License |
105 | 59 |
|
|
0 commit comments