stimulus_rails_datatables 0.3.0 → 0.3.2

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: 0a6c48a66c1fdfb21381c809b19c6a920f370a0c5840de680a698aa613e6ec82
4
- data.tar.gz: 9020cb07384fcbd8edb98d6070ec6732cc14a2160ba6468ba13d9292799882e0
3
+ metadata.gz: d508095bbb7cd25e12447b92081260fa8fa373f139be6b065c5eb6f17b3118af
4
+ data.tar.gz: 9ae1b95a92a6898b2d2b6c55310f06dd7b8ca58902fb55d8f378d1a8605cf5ec
5
5
  SHA512:
6
- metadata.gz: 7963d1211951e6bd536001f24cbee70710b073efaf00ccbd1c151769ab8534e4aa6d66503d34cc2630ef30197bef31609abb4442904c81e320b72e684d0c3e4e
7
- data.tar.gz: 115e262ea157c6ac4dc3b1756dc146d17d7c82d5b9e6801074cebed0d542833178938d45c0c605f7f9653f6b7a53118511575623092d387d2f1c91fb5ec28df3
6
+ metadata.gz: fe105aa5f8bc6b0ffcc8bda21fe78c070d75c09b17a211d7e05a217a81ffcf680051e7cd2e5778f864d6735c191a63897ed25b2418e51e7fc9fea3fc14ece080
7
+ data.tar.gz: c87088b4327210d783377c05291fcf5194f8ae0fcc523408f99d2e678b820fd2ca3c3d465879e9f423d782df255914ef2f6c5bc37f13e640e5430631d301f19f
data/README.md CHANGED
@@ -73,13 +73,16 @@ import 'datatables_config'
73
73
  <%= opts.option 'inactive', 'Inactive' %>
74
74
  <% end %>
75
75
 
76
+ # If set_value matches, it will be mark as selected
77
+ # When set_value is present, localStorage restoration is skipped
78
+ # so the default is not overridden on page reload.
76
79
  <%= f.role(
77
80
  remote: {
78
81
  url: roles_path,
79
82
  label: 'name',
80
83
  value: 'id',
81
84
  placeholder: 'Select Role',
82
- set_value: 1 # If set_value matches, it will be mark as selected
85
+ set_value: 1
83
86
  }
84
87
  ) %>
85
88
 
@@ -92,6 +95,92 @@ import 'datatables_config'
92
95
  <% end %>
93
96
  ```
94
97
 
98
+ #### Dependent Location Filters
99
+
100
+ Use dependent remote selects when one filter should load its options from the value of another filter. The built-in `location` helper creates three filters named `province_id`, `city_id`, and `barangay_id`.
101
+
102
+ ```ruby
103
+ <%= filter_for 'locations-table' do |f| %>
104
+ <%= f.location(
105
+ province_url: provinces_path(format: :json),
106
+ city_url: cities_path(province_id: '{province_id}', format: :json),
107
+ barangay_url: barangays_path(city_id: '{city_id}', format: :json)
108
+ ) %>
109
+ <% end %>
110
+
111
+ <%= datatable_for 'locations-table', source: locations_path(format: :json) do |dt| %>
112
+ <% dt.column :name, title: 'Name' %>
113
+ <% dt.column :province_name, title: 'Province' %>
114
+ <% dt.column :city_name, title: 'City' %>
115
+ <% dt.column :barangay_name, title: 'Barangay' %>
116
+ <% end %>
117
+ ```
118
+
119
+ The `{province_id}` and `{city_id}` placeholders are replaced in the browser before fetching the next select's options:
120
+
121
+ - changing province fetches `cities_path(... province_id: selected_province_id)`
122
+ - changing city fetches `barangays_path(... city_id: selected_city_id)`
123
+ - changing any filter reloads the datatable with params such as `filters[province_id]=1&filters[city_id]=2`
124
+
125
+ Your JSON endpoints should return an array using the keys configured by the helper: `location_id` for the option value and `name` for the label.
126
+
127
+ ```ruby
128
+ class ProvincesController < ApplicationController
129
+ def index
130
+ render json: Province.select(:location_id, :name)
131
+ end
132
+ end
133
+
134
+ class CitiesController < ApplicationController
135
+ def index
136
+ cities = City.where(province_id: params[:province_id])
137
+ render json: cities.select(:location_id, :name)
138
+ end
139
+ end
140
+
141
+ class BarangaysController < ApplicationController
142
+ def index
143
+ barangays = Barangay.where(city_id: params[:city_id])
144
+ render json: barangays.select(:location_id, :name)
145
+ end
146
+ end
147
+ ```
148
+
149
+ Then apply the selected filters inside your datatable class:
150
+
151
+ ```ruby
152
+ def get_raw_records
153
+ Location.all.then { |relation| apply_filters(relation) }
154
+ end
155
+
156
+ def apply_filters(relation)
157
+ relation = relation.where(province_id: query_filters[:province_id]) if query_filters[:province_id].present?
158
+ relation = relation.where(city_id: query_filters[:city_id]) if query_filters[:city_id].present?
159
+ relation = relation.where(barangay_id: query_filters[:barangay_id]) if query_filters[:barangay_id].present?
160
+ relation
161
+ end
162
+ ```
163
+
164
+ To load the table already filtered from the page URL, use normal nested filter params:
165
+
166
+ ```text
167
+ /locations?filters[province_id]=1&filters[city_id]=2&filters[barangay_id]=3
168
+ ```
169
+
170
+ Pass those params into the datatable source on the initial render:
171
+
172
+ ```ruby
173
+ <% location_filters = params[:filters]&.permit(:province_id, :city_id, :barangay_id) || {} %>
174
+
175
+ <%= datatable_for 'locations-table',
176
+ source: locations_path(format: :json, filters: location_filters) do |dt| %>
177
+ <% dt.column :name, title: 'Name' %>
178
+ <% dt.column :province_name, title: 'Province' %>
179
+ <% dt.column :city_name, title: 'City' %>
180
+ <% dt.column :barangay_name, title: 'Barangay' %>
181
+ <% end %>
182
+ ```
183
+
95
184
  ### Backend DataTable Class
96
185
 
97
186
  ```ruby
@@ -9,7 +9,6 @@ export default class extends Controller {
9
9
  get filterDtId() {
10
10
  const today = new Date()
11
11
  const key = today.toISOString().split('T')[0]
12
-
13
12
  return `${key}:${this.element.dataset.filterDatatableId}`
14
13
  }
15
14
 
@@ -18,14 +17,14 @@ export default class extends Controller {
18
17
  this.restoreState()
19
18
 
20
19
  // single delegated listener — saves and triggers dependent populates
21
- this.element.addEventListener('change', (event) => {
20
+ this.element.addEventListener('change', async (event) => {
22
21
  if (!event.target.matches('[data-filter-field-name]')) return
23
22
 
24
- // persist the user's change
25
- this.saveState()
23
+ // if this field has dependents, reset stale child values and re-populate them
24
+ await this.populateDependents(event.target)
26
25
 
27
- // if this field has dependents, re-populate them
28
- this.populateDependents(event.target, this.currentParams()[this.element.dataset.filterRootKey] || {})
26
+ // persist the user's change after dependent filters have been cleaned up
27
+ this.saveState()
29
28
 
30
29
  // trigger datatable reload
31
30
  this.reloadAppDatatable()
@@ -81,7 +80,6 @@ export default class extends Controller {
81
80
  let url = select.dataset.filterRemoteUrlValue
82
81
  const labelKey = select.dataset.filterLabelKey
83
82
  const valueKey = select.dataset.filterValueKey
84
- const placeholder = select.dataset.filterPlaceholder || 'Select'
85
83
  const set_value = select.dataset.filterSetValue || ''
86
84
 
87
85
  url = decodeURIComponent(url).replace(/{(\w+)}/g, (_, key) => {
@@ -96,7 +94,7 @@ export default class extends Controller {
96
94
  if (!response.ok) throw new Error(`Failed to fetch ${url}`)
97
95
  const data = await response.json()
98
96
 
99
- select.innerHTML = `<option value="">${placeholder}</option>`
97
+ this.resetSelect(select, false)
100
98
  data.forEach(item => {
101
99
  const option = document.createElement('option')
102
100
  option.value = item[valueKey]
@@ -117,6 +115,19 @@ export default class extends Controller {
117
115
  }
118
116
  }
119
117
 
118
+ resetSelect(select, disabled = true) {
119
+ const placeholder = select.dataset.filterPlaceholder || 'Select'
120
+ const option = document.createElement('option')
121
+
122
+ option.value = ''
123
+ option.textContent = placeholder
124
+
125
+ select.innerHTML = ''
126
+ select.appendChild(option)
127
+ select.value = ''
128
+ select.disabled = disabled
129
+ }
130
+
120
131
  currentParams() {
121
132
  const rootKey = this.element.dataset.filterRootKey
122
133
  const params = {}
@@ -176,7 +187,9 @@ export default class extends Controller {
176
187
  await this.populate(root)
177
188
  // after root options exist, restore saved root value (if any)
178
189
  const sv = savedParams[root.dataset.filterFieldName]
179
- if (sv) root.value = sv
190
+ const set_value = root.dataset.filterSetValue || ''
191
+
192
+ if (sv && !set_value) root.value = sv
180
193
  // cascade down children
181
194
  await this.populateDependents(root, savedParams)
182
195
  }))
@@ -212,11 +225,22 @@ export default class extends Controller {
212
225
  const children = this.selects.filter(s => s.dataset.filterDependsOn === parentKey)
213
226
 
214
227
  for (const child of children) {
228
+ this.resetSelect(child)
229
+
230
+ if (!parent.value) {
231
+ await this.populateDependents(child, savedParams)
232
+ continue
233
+ }
234
+
215
235
  // populate child using parent's current value substituted by populate()
216
236
  await this.populate(child)
217
237
  // restore child's saved value if exists
218
238
  const childSaved = savedParams[child.dataset.filterFieldName]
219
- if (childSaved) child.value = childSaved
239
+ const set_value = child.dataset.filterSetValue || ''
240
+
241
+ if (childSaved && !set_value) {
242
+ child.value = childSaved
243
+ }
220
244
  // recurse deeper
221
245
  await this.populateDependents(child, savedParams)
222
246
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusRailsDatatables
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimulus_rails_datatables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Den Meralpis
@@ -141,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
141
  - !ruby/object:Gem::Version
142
142
  version: '0'
143
143
  requirements: []
144
- rubygems_version: 3.6.9
144
+ rubygems_version: 4.0.10
145
145
  specification_version: 4
146
146
  summary: Rails integration for DataTables with filters and remote data support
147
147
  test_files: []