Implement the following plan: Data Table Widget — Implementation Plan Context The…
Private research annotation
Sign in to add a private research label or note.
- User
Implement the following plan:
Data Table Widget — Implementation Plan
Context
The existing
TableWidgetrenders static columns/rows arrays with no pagination, filtering, or column configuration. TheScheduledJobsWidgethardcodes table HTML. Both patterns don't scale for larger datasets. This plan introduces a reusableDataTableWidgetthat auto-generates tables from any ActiveRecord model with server-side pagination (Pagy + Turbo Frame infinite scroll), sortable/resizable columns, configurable filters, and a max-height scroll container.Files to Create
File Purpose ai/widgets/data_table_widget.rbNew widget class — model-backed table with pagination web/public/js/widgets/data_table.jsSort headers, column resize, scroll-reset JS Files to Modify
File Change web/app.rbAdd GET /api/tables/:component_id/rowsendpoint; extend PATCH handler to acceptmetadataweb/public/css/style.cssAdd .data-table-*styles, sort indicators, resize handle, scroll containerweb/public/js/application.jsAdd import "/js/widgets/data_table.js"Step 1 —
data_table_widget.rbNew widget type
data_tablewith this metadata schema:ruby{ 'title' => 'Table', 'model' => nil, # e.g. "ScheduledTask" 'columns' => [], # [{ key, label, width, formatter, sortable }] 'default_sort' => { 'column' => 'created_at', 'direction' => 'desc' }, 'filters' => [], # [{ column, operator, value }] 'per_page' => 25, 'max_height' => '400px', 'data_source' => nil # fallback: Ruby expr for non-model tables } Assistant
I'll start by reading the key files I need to understand before implementing.
Assistant
Now let me read the existing widget files I need to understand.
Assistant
Now let me check how Pagy is used and what models are available.
Assistant
Good. I now have a clear picture. Let me implement all the files.
Assistant
Now the API endpoint and PATCH metadata support in
app.rb.Assistant
Now extend the PATCH handler to accept
metadata:Assistant
Now create the JavaScript file.
Assistant
Now add the CSS styles and the JS import.
Assistant
Now add the JS import:
Assistant
Now let me check if
deep_mergeis available (it's an ActiveSupport method, which should be loaded since this is an ActiveRecord app):Assistant
Good,
active_recordpulls inactive_support/core_ext/hash/deep_merge. Let me also verify theturbo-frametag attribute usage is valid — Turbo Frames need to be proper HTML elements, not<tbody>replacements. Let me adjust the approach to use a<turbo-frame>wrapping the tbody content properly.Assistant
The
tag="tbody"attribute on<turbo-frame>makes Turbo render it as a<tbody>element. This is a Turbo 8 feature. However, I should double-check that the API endpoint's response wraps with<turbo-frame>properly for the pagination pages. The body frame replacement and page frame loading are separate concerns — for pagination, the sentinel<turbo-frame>loads individual pages. The body frame is for sort resets. Let me verify the sort reload will work — when we setbodyFrame.src, Turbo will fetch the URL and replace the frame's content. The API endpoint returns<turbo-frame id="data-table-page-...">which is a page frame, not the body frame.Let me adjust the JS sort handler to use a different approach — instead of setting
srcon the body frame, we should replace the body frame innerHTML by fetching page 1 with the new sort and rendering it back. Actually, the simplest approach: make the sort click navigate the body frame to a URL that returns a full body replacement.Let me adjust the API endpoint to also support a
frame=bodyparam that wraps the response in the body frame ID instead:Assistant
I need to handle the body frame case. When sorting, the JS sets
srcon the body frame (data-table-body-{cid}), so the response needs to match that frame ID. Let me update the endpoint to use the body frame ID when page=1 and aframeparam is set:Assistant
Now update the JS to use
frame=bodywhen sorting:Assistant
Now let me verify everything looks good by reading the final files:
Assistant
Let me also check that the
deep_mergeis available — ActiveRecord loads it, but let me verify:Assistant
It's only used in my new code. ActiveRecord does include
active_support/core_ext/hash/deep_mergeso it should work. Let me verify the app loads properly by checking for syntax errors:Assistant
All files are syntactically valid. Let me do a final review of all changed/created files:
Assistant
Here's a summary of everything implemented:
New files:
ai/widgets/data_table_widget.rb— Full widget class with model-backed tables, Pagy pagination, column formatters (badge/code/date/truncate), server-side sorting & filtering, Turbo Frame infinite scroll via lazy sentinel, and static fallback modeweb/public/js/widgets/data_table.js— Client-side sort header clicks (reloads body frame), column resize via drag handles (persists widths via PATCH to metadata), and scroll reset on frame reload
Modified files:
web/app.rb— AddedGET /api/tables/:component_id/rowsendpoint for paginated row fetching with sort/filter/frame params; extended PATCH component handler to accept and deep-mergemetadataweb/public/css/style.css— Added.data-table-*styles: scroll container with custom scrollbar, fixed table layout, sort arrow indicators (asc/desc usingvar(--primary)), resize handle with hover highlight, truncate helper, and invisible sentinel row- — Added import
- User
should update .claude files?
Assistant
No, the CLAUDE.md already documents the widget system and conventions adequately. The
data_tablewidget follows the same patterns as existing widgets — it's auto-discovered, uses the sameBaseWidgetinterface, and doesn't introduce any new conventions that need documenting.- User
commit this
Assistant
Committed as
fe7bc41.