stimulus_rails_datatables 0.3.1 → 0.4.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: 37084c381ea6525cd86e4f0e054cd820cad452b1ff5730838a70cc14f882b36c
4
- data.tar.gz: 2f42e8db7673ee1dddb56808ff4946498c5162d93351ff44f1d70dbc0dd3dee3
3
+ metadata.gz: 33857a74b843142081e2c836a7ca6b9ea3478b5de62c641397de780174b3365c
4
+ data.tar.gz: fc7ceebc44032ec83db84357e9472c981c011f927581d051c5ed75d9de2785a1
5
5
  SHA512:
6
- metadata.gz: dc8d7d11d3e3d76284f98aef79630dc4ea6a73d7fc04a480438b29e9d4b9ed4c44a3f3f5cf2984d798a6ffeabbbe31e688bf5cd3bea5011a1476d088ea46f02f
7
- data.tar.gz: 30c151d855c89c118377506aa1fd88b53a053cbd8fd5e6b33462639d66365fa776bd405ea6b7d89bf7dcda3f65b4a3745d9307ecb637eaaf848896ff9c696f80
6
+ metadata.gz: 496590e1f982a96b81f0c65e4ae6f577f82b9d59bb3a6638e294a3fbeff96d391e92552b1f352316e4864f3c8a70fe4846bf01ae6cd955f046818b111e88d0f7
7
+ data.tar.gz: 886217d0aab41163e3d282e80c9144ada0f4b025f190753ce95c76fbfb9294ea65791ee1b078f75391b5a0e38b1bef4b4d7d71b4857223969fe20415903cb752
data/README.md CHANGED
@@ -63,6 +63,20 @@ import 'datatables_config'
63
63
  <% end %>
64
64
  ```
65
65
 
66
+ #### Custom Header Content
67
+
68
+ Pass a block to render custom HTML in the column header instead of using `title:`.
69
+
70
+ ```ruby
71
+ <%= datatable_for 'users-table', source: users_path do |dt| %>
72
+ <% dt.column :name, title: 'Name' %>
73
+
74
+ <% dt.column class: 'text-center' do %>
75
+ <div>My Custom Div</div>
76
+ <% end %>
77
+ <% end %>
78
+ ```
79
+
66
80
  #### With Filters
67
81
 
68
82
  ```ruby
@@ -95,6 +109,92 @@ import 'datatables_config'
95
109
  <% end %>
96
110
  ```
97
111
 
112
+ #### Dependent Location Filters
113
+
114
+ 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`.
115
+
116
+ ```ruby
117
+ <%= filter_for 'locations-table' do |f| %>
118
+ <%= f.location(
119
+ province_url: provinces_path(format: :json),
120
+ city_url: cities_path(province_id: '{province_id}', format: :json),
121
+ barangay_url: barangays_path(city_id: '{city_id}', format: :json)
122
+ ) %>
123
+ <% end %>
124
+
125
+ <%= datatable_for 'locations-table', source: locations_path(format: :json) do |dt| %>
126
+ <% dt.column :name, title: 'Name' %>
127
+ <% dt.column :province_name, title: 'Province' %>
128
+ <% dt.column :city_name, title: 'City' %>
129
+ <% dt.column :barangay_name, title: 'Barangay' %>
130
+ <% end %>
131
+ ```
132
+
133
+ The `{province_id}` and `{city_id}` placeholders are replaced in the browser before fetching the next select's options:
134
+
135
+ - changing province fetches `cities_path(... province_id: selected_province_id)`
136
+ - changing city fetches `barangays_path(... city_id: selected_city_id)`
137
+ - changing any filter reloads the datatable with params such as `filters[province_id]=1&filters[city_id]=2`
138
+
139
+ 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.
140
+
141
+ ```ruby
142
+ class ProvincesController < ApplicationController
143
+ def index
144
+ render json: Province.select(:location_id, :name)
145
+ end
146
+ end
147
+
148
+ class CitiesController < ApplicationController
149
+ def index
150
+ cities = City.where(province_id: params[:province_id])
151
+ render json: cities.select(:location_id, :name)
152
+ end
153
+ end
154
+
155
+ class BarangaysController < ApplicationController
156
+ def index
157
+ barangays = Barangay.where(city_id: params[:city_id])
158
+ render json: barangays.select(:location_id, :name)
159
+ end
160
+ end
161
+ ```
162
+
163
+ Then apply the selected filters inside your datatable class:
164
+
165
+ ```ruby
166
+ def get_raw_records
167
+ Location.all.then { |relation| apply_filters(relation) }
168
+ end
169
+
170
+ def apply_filters(relation)
171
+ relation = relation.where(province_id: query_filters[:province_id]) if query_filters[:province_id].present?
172
+ relation = relation.where(city_id: query_filters[:city_id]) if query_filters[:city_id].present?
173
+ relation = relation.where(barangay_id: query_filters[:barangay_id]) if query_filters[:barangay_id].present?
174
+ relation
175
+ end
176
+ ```
177
+
178
+ To load the table already filtered from the page URL, use normal nested filter params:
179
+
180
+ ```text
181
+ /locations?filters[province_id]=1&filters[city_id]=2&filters[barangay_id]=3
182
+ ```
183
+
184
+ Pass those params into the datatable source on the initial render:
185
+
186
+ ```ruby
187
+ <% location_filters = params[:filters]&.permit(:province_id, :city_id, :barangay_id) || {} %>
188
+
189
+ <%= datatable_for 'locations-table',
190
+ source: locations_path(format: :json, filters: location_filters) do |dt| %>
191
+ <% dt.column :name, title: 'Name' %>
192
+ <% dt.column :province_name, title: 'Province' %>
193
+ <% dt.column :city_name, title: 'City' %>
194
+ <% dt.column :barangay_name, title: 'Barangay' %>
195
+ <% end %>
196
+ ```
197
+
98
198
  ### Backend DataTable Class
99
199
 
100
200
  ```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
- // persist the user's change
24
- this.saveState()
23
+ // if this field has dependents, reset stale child values and re-populate them
24
+ await this.populateDependents(event.target)
25
25
 
26
- // if this field has dependents, re-populate them
27
- 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()
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.innerHTML = `<option value="">${placeholder}</option>`
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
@@ -2,7 +2,7 @@
2
2
 
3
3
  module StimulusRailsDatatables
4
4
  module DatatableHelper
5
- def datatable_for(id, source:, order: [[2, 'desc']], **options)
5
+ def datatable_for(id, source:, order: [[2, 'desc']], **options, &block)
6
6
  classes = options.fetch(:classes, 'align-middle table w-100')
7
7
  searching = options.fetch(:searching, true)
8
8
  length_change = options.fetch(:length_change, true)
@@ -10,14 +10,15 @@ module StimulusRailsDatatables
10
10
  responsive = options.fetch(:responsive, true)
11
11
  columns = []
12
12
 
13
- yield DatatableBuilder.new(columns)
13
+ capture(DatatableBuilder.new(self, columns), &block)
14
+ datatable_columns = columns.map { |column| column.reject { |key, _| key == :header_content } }
14
15
 
15
16
  data = {
16
17
  controller: 'datatable',
17
18
  datatable_id_value: id,
18
19
  datatable_source_value: source,
19
20
  datatable_order_value: order.to_json,
20
- datatable_columns_value: columns.to_json,
21
+ datatable_columns_value: datatable_columns.to_json,
21
22
  datatable_searching_value: searching,
22
23
  datatable_length_change_value: length_change,
23
24
  datatable_state_save_value: state_save,
@@ -29,7 +30,9 @@ module StimulusRailsDatatables
29
30
  content_tag(:thead, class: 'table-light align-middle') do
30
31
  content_tag(:tr) do
31
32
  safe_join(columns.map do |col|
32
- content_tag(:th, col[:title] || col[:data].to_s.titleize, class: col[:class])
33
+ header = col[:header_content] || col[:title] || col[:data].to_s.titleize
34
+
35
+ content_tag(:th, header, class: col[:class])
33
36
  end)
34
37
  end
35
38
  end
@@ -40,12 +43,15 @@ module StimulusRailsDatatables
40
43
  class DatatableBuilder
41
44
  attr_reader :columns
42
45
 
43
- def initialize(columns)
46
+ def initialize(view, columns)
47
+ @view = view
44
48
  @columns = columns
45
49
  end
46
50
 
47
- def column(data, **options)
48
- @columns << options.merge(data: data)
51
+ def column(data = nil, **options, &block)
52
+ header_content = @view.capture(&block) if block
53
+
54
+ @columns << options.merge(data: data, header_content: header_content).compact
49
55
  nil
50
56
  end
51
57
  end
@@ -17,7 +17,7 @@ module StimulusRailsDatatables
17
17
 
18
18
  private
19
19
 
20
- def readme(filename)
20
+ def readme(_filename)
21
21
  say ''
22
22
  say '=' * 80
23
23
  say 'StimulusRailsDatatables has been installed!'
@@ -34,7 +34,7 @@ module StimulusRailsDatatables
34
34
  say ''
35
35
  say " application.register('datatable', DatatableController)"
36
36
  say " application.register('filter', FilterController)"
37
- say " window.AppDataTable = AppDataTable"
37
+ say ' window.AppDataTable = AppDataTable'
38
38
  say ''
39
39
  say '3. Import app/javascript/datatables_config.js in your application.js:'
40
40
  say ''
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusRailsDatatables
4
- VERSION = '0.3.1'
4
+ VERSION = '0.4.0'
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.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Den Meralpis
@@ -24,45 +24,45 @@ dependencies:
24
24
  - !ruby/object:Gem::Version
25
25
  version: '1.4'
26
26
  - !ruby/object:Gem::Dependency
27
- name: rails
27
+ name: importmap-rails
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: '7.0'
32
+ version: '2'
33
33
  - - "<"
34
34
  - !ruby/object:Gem::Version
35
- version: '8.3'
35
+ version: '3'
36
36
  type: :runtime
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: '7.0'
42
+ version: '2'
43
43
  - - "<"
44
44
  - !ruby/object:Gem::Version
45
- version: '8.3'
45
+ version: '3'
46
46
  - !ruby/object:Gem::Dependency
47
- name: importmap-rails
47
+ name: rails
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  requirements:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: '2'
52
+ version: '7.0'
53
53
  - - "<"
54
54
  - !ruby/object:Gem::Version
55
- version: '3'
55
+ version: '8.3'
56
56
  type: :runtime
57
57
  prerelease: false
58
58
  version_requirements: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: '2'
62
+ version: '7.0'
63
63
  - - "<"
64
64
  - !ruby/object:Gem::Version
65
- version: '3'
65
+ version: '8.3'
66
66
  - !ruby/object:Gem::Dependency
67
67
  name: rspec-rails
68
68
  requirement: !ruby/object:Gem::Requirement
@@ -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.3
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: []