stimulus_rails_datatables 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3c608c879de27fc4bd4b8ddead4a52eb5f425194cbdf5ee3fd598ad754057c36
|
|
4
|
+
data.tar.gz: 47f4a33a22dcfca000ae5a36761c314ca6d286d351fa2522e5b79b1fbedeb5c7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d5f347f96168ea3599e75a8b8ae8dbf14e99f106d05ba44336c3d5886dfafe0c8b6a76da9141b5b025ff5f293a0fa714e3f7d01467d99c998c04f5f53b44b43a
|
|
7
|
+
data.tar.gz: 1930559a899f34aefb359a093e2859ab33190460616363854472ab50c001f90433acbe6d3527154d58c9a14303b2cac98906ce04a05258922fb5192e6cbdb4c6
|
data/README.md
CHANGED
|
@@ -96,7 +96,7 @@ Pass a block to render custom HTML in the column header instead of using `title:
|
|
|
96
96
|
label: 'name',
|
|
97
97
|
value: 'id',
|
|
98
98
|
placeholder: 'Select Role',
|
|
99
|
-
set_value: 1
|
|
99
|
+
set_value: 1
|
|
100
100
|
}
|
|
101
101
|
) %>
|
|
102
102
|
|
|
@@ -233,6 +233,40 @@ class UserDatatable < StimulusRailsDatatables::BaseDatatable
|
|
|
233
233
|
end
|
|
234
234
|
```
|
|
235
235
|
|
|
236
|
+
### Additional Data
|
|
237
|
+
|
|
238
|
+
You can return extra fields alongside the standard DataTables JSON response by defining `additional_data` in your datatable class. This is useful for displaying aggregate stats (e.g. counts, totals) that are scoped to the current filtered result set.
|
|
239
|
+
|
|
240
|
+
```ruby
|
|
241
|
+
class OrdersDatatable < StimulusRailsDatatables::BaseDatatable
|
|
242
|
+
def additional_data
|
|
243
|
+
{ unread_orders_count: get_raw_records.where(is_read: false).count }
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# ...
|
|
247
|
+
end
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
Any element with a `data-datatable-field` attribute matching a key from `additional_data` will have its text content updated automatically after every draw (page change, sort, filter):
|
|
251
|
+
|
|
252
|
+
```html
|
|
253
|
+
<span data-datatable-field="unread_orders_count">0</span> unread orders
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
When you have **multiple datatables on the same page**, scope the element to a specific table using `data-for-datatable`:
|
|
257
|
+
|
|
258
|
+
```html
|
|
259
|
+
<span data-datatable-field="unread_orders_count" data-for-datatable="orders-table">0</span>
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
You can also listen for the `datatable:additional-data` event for custom JavaScript handling:
|
|
263
|
+
|
|
264
|
+
```javascript
|
|
265
|
+
document.addEventListener('datatable:additional-data', (e) => {
|
|
266
|
+
console.log(e.detail) // { unread_orders_count: 5 }
|
|
267
|
+
})
|
|
268
|
+
```
|
|
269
|
+
|
|
236
270
|
### JavaScript API
|
|
237
271
|
|
|
238
272
|
```javascript
|
|
@@ -127,6 +127,34 @@ export default class extends Controller {
|
|
|
127
127
|
detail: { table: appDataTable }
|
|
128
128
|
}))
|
|
129
129
|
})
|
|
130
|
+
|
|
131
|
+
// Dispatch additional_data returned by the server alongside standard DataTables fields
|
|
132
|
+
const STANDARD_DT_KEYS = new Set(['draw', 'recordsTotal', 'recordsFiltered', 'data', 'error'])
|
|
133
|
+
appDataTable.on('xhr', (_e, _settings, json) => {
|
|
134
|
+
if (!json) return
|
|
135
|
+
const additionalData = Object.fromEntries(
|
|
136
|
+
Object.entries(json).filter(([key]) => !STANDARD_DT_KEYS.has(key))
|
|
137
|
+
)
|
|
138
|
+
if (Object.keys(additionalData).length === 0) return
|
|
139
|
+
|
|
140
|
+
// Dispatch event so consumers can react programmatically
|
|
141
|
+
this.element.dispatchEvent(new CustomEvent('datatable:additional-data', {
|
|
142
|
+
bubbles: true,
|
|
143
|
+
detail: additionalData
|
|
144
|
+
}))
|
|
145
|
+
|
|
146
|
+
// Auto-update elements: <span data-datatable-field="unread_orders_count">
|
|
147
|
+
// Scope with optional data-for-datatable="<id>" to support multiple tables per page
|
|
148
|
+
// Single DOM query for all field elements, then group by field name to avoid N queries per key
|
|
149
|
+
document.querySelectorAll('[data-datatable-field]').forEach(el => {
|
|
150
|
+
const key = el.dataset.datatableField
|
|
151
|
+
if (!(key in additionalData)) return
|
|
152
|
+
const forId = el.dataset.forDatatable
|
|
153
|
+
if (!forId || forId === datatableId) {
|
|
154
|
+
el.textContent = additionalData[key]
|
|
155
|
+
}
|
|
156
|
+
})
|
|
157
|
+
})
|
|
130
158
|
}
|
|
131
159
|
}
|
|
132
160
|
|