stimulus_rails_datatables 0.3.1 → 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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d508095bbb7cd25e12447b92081260fa8fa373f139be6b065c5eb6f17b3118af
|
|
4
|
+
data.tar.gz: 9ae1b95a92a6898b2d2b6c55310f06dd7b8ca58902fb55d8f378d1a8605cf5ec
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fe105aa5f8bc6b0ffcc8bda21fe78c070d75c09b17a211d7e05a217a81ffcf680051e7cd2e5778f864d6735c191a63897ed25b2418e51e7fc9fea3fc14ece080
|
|
7
|
+
data.tar.gz: c87088b4327210d783377c05291fcf5194f8ae0fcc523408f99d2e678b820fd2ca3c3d465879e9f423d782df255914ef2f6c5bc37f13e640e5430631d301f19f
|
data/README.md
CHANGED
|
@@ -95,6 +95,92 @@ import 'datatables_config'
|
|
|
95
95
|
<% end %>
|
|
96
96
|
```
|
|
97
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
|
+
|
|
98
184
|
### Backend DataTable Class
|
|
99
185
|
|
|
100
186
|
```ruby
|
|
@@ -17,14 +17,14 @@ export default class extends Controller {
|
|
|
17
17
|
this.restoreState()
|
|
18
18
|
|
|
19
19
|
// single delegated listener — saves and triggers dependent populates
|
|
20
|
-
this.element.addEventListener('change', (event) => {
|
|
20
|
+
this.element.addEventListener('change', async (event) => {
|
|
21
21
|
if (!event.target.matches('[data-filter-field-name]')) return
|
|
22
22
|
|
|
23
|
-
//
|
|
24
|
-
this.
|
|
23
|
+
// if this field has dependents, reset stale child values and re-populate them
|
|
24
|
+
await this.populateDependents(event.target)
|
|
25
25
|
|
|
26
|
-
//
|
|
27
|
-
this.
|
|
26
|
+
// persist the user's change after dependent filters have been cleaned up
|
|
27
|
+
this.saveState()
|
|
28
28
|
|
|
29
29
|
// trigger datatable reload
|
|
30
30
|
this.reloadAppDatatable()
|
|
@@ -80,7 +80,6 @@ export default class extends Controller {
|
|
|
80
80
|
let url = select.dataset.filterRemoteUrlValue
|
|
81
81
|
const labelKey = select.dataset.filterLabelKey
|
|
82
82
|
const valueKey = select.dataset.filterValueKey
|
|
83
|
-
const placeholder = select.dataset.filterPlaceholder || 'Select'
|
|
84
83
|
const set_value = select.dataset.filterSetValue || ''
|
|
85
84
|
|
|
86
85
|
url = decodeURIComponent(url).replace(/{(\w+)}/g, (_, key) => {
|
|
@@ -95,7 +94,7 @@ export default class extends Controller {
|
|
|
95
94
|
if (!response.ok) throw new Error(`Failed to fetch ${url}`)
|
|
96
95
|
const data = await response.json()
|
|
97
96
|
|
|
98
|
-
select
|
|
97
|
+
this.resetSelect(select, false)
|
|
99
98
|
data.forEach(item => {
|
|
100
99
|
const option = document.createElement('option')
|
|
101
100
|
option.value = item[valueKey]
|
|
@@ -116,6 +115,19 @@ export default class extends Controller {
|
|
|
116
115
|
}
|
|
117
116
|
}
|
|
118
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
|
+
|
|
119
131
|
currentParams() {
|
|
120
132
|
const rootKey = this.element.dataset.filterRootKey
|
|
121
133
|
const params = {}
|
|
@@ -213,6 +225,13 @@ export default class extends Controller {
|
|
|
213
225
|
const children = this.selects.filter(s => s.dataset.filterDependsOn === parentKey)
|
|
214
226
|
|
|
215
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
|
+
|
|
216
235
|
// populate child using parent's current value substituted by populate()
|
|
217
236
|
await this.populate(child)
|
|
218
237
|
// restore child's saved value if exists
|
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.
|
|
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: 4.0.
|
|
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: []
|