stimulus_rails_datatables 0.2.2 → 0.3.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: 1fa5d82604ec3cbe676411866a0fb8f679f8428082064c17ee8fdb31bd2381eb
4
- data.tar.gz: 3af0d8d46c2a8a64ffd0584b6bd96a5b2227473b592a2acfe1b1d12b57d56bb9
3
+ metadata.gz: 0a6c48a66c1fdfb21381c809b19c6a920f370a0c5840de680a698aa613e6ec82
4
+ data.tar.gz: 9020cb07384fcbd8edb98d6070ec6732cc14a2160ba6468ba13d9292799882e0
5
5
  SHA512:
6
- metadata.gz: f2f88681c77f4d031577426fcc757c8f6955ae940cd22e9dbad49c8470bcc6e7ff08119857f2756b275431d5f482318b8b48244a31e1f3e24eacfe45aef55786
7
- data.tar.gz: f88309876186c9209b36673ac36b40553ebf4e090ed63e20b740d958e6667ebefb8182f0966a756e9d149f1088a2dc3e6cf1e14d2e5e919f79b39c3889ff26fa
6
+ metadata.gz: 7963d1211951e6bd536001f24cbee70710b073efaf00ccbd1c151769ab8534e4aa6d66503d34cc2630ef30197bef31609abb4442904c81e320b72e684d0c3e4e
7
+ data.tar.gz: 115e262ea157c6ac4dc3b1756dc146d17d7c82d5b9e6801074cebed0d542833178938d45c0c605f7f9653f6b7a53118511575623092d387d2f1c91fb5ec28df3
data/README.md CHANGED
@@ -23,6 +23,29 @@ And then execute:
23
23
 
24
24
  ```bash
25
25
  $ bundle install
26
+ $ rails generate stimulus_rails_datatables:install
27
+ ```
28
+
29
+ This will create:
30
+ - `config/initializers/stimulus_rails_datatables.rb`
31
+ - `app/javascript/datatables_config.js`
32
+
33
+ ### Setup JavaScript Controllers
34
+
35
+ In `app/javascript/controllers/index.js`:
36
+
37
+ ```javascript
38
+ import DatatableController from 'stimulus_rails_datatables/datatables_controller'
39
+ import FilterController from 'stimulus_rails_datatables/filter_controller'
40
+
41
+ application.register('datatable', DatatableController)
42
+ application.register('filter', FilterController)
43
+ ```
44
+
45
+ In `app/javascript/application.js`:
46
+
47
+ ```javascript
48
+ import 'datatables_config'
26
49
  ```
27
50
 
28
51
  ## Usage
@@ -55,7 +78,8 @@ $ bundle install
55
78
  url: roles_path,
56
79
  label: 'name',
57
80
  value: 'id',
58
- placeholder: 'Select Role'
81
+ placeholder: 'Select Role',
82
+ set_value: 1 # If set_value matches, it will be mark as selected
59
83
  }
60
84
  ) %>
61
85
 
@@ -109,6 +133,9 @@ end
109
133
  ### JavaScript API
110
134
 
111
135
  ```javascript
136
+ // Access via window.AppDataTable or import directly
137
+ import { AppDataTable } from 'stimulus_rails_datatables/app_datatable'
138
+
112
139
  // Reload a specific datatable
113
140
  AppDataTable.reload('#users-table')
114
141
 
@@ -119,35 +146,68 @@ AppDataTable.load('#users-table', '/users?status=active')
119
146
  AppDataTable.reloadAll()
120
147
  ```
121
148
 
122
- ## Configuration
123
-
124
- The gem automatically registers Stimulus controllers and imports required JavaScript dependencies. The following controllers are available:
125
-
126
- - `datatable` - Main DataTable controller
127
- - `filter` - Filter controller with state management
149
+ ## Controller Setup
128
150
 
129
- ## Overriding Default Configuration
130
- To override the default DataTables configuration, create a file at `app/javascript/datatables_config.js` with your custom settings or run rails generator:
151
+ In your Rails controller, respond to JSON format for DataTables:
131
152
 
132
- ```bash
133
- rails generate stimulus_rails_datatables:install
153
+ ```ruby
154
+ class UsersController < ApplicationController
155
+ def index
156
+ respond_to do |format|
157
+ format.html
158
+ format.json { render json: UsersDatatable.new(view_context) }
159
+ end
160
+ end
161
+ end
134
162
  ```
135
163
 
136
- Once created, you can pin it in your importmap configuration:
164
+ ## Configuration
137
165
 
138
- ```ruby
139
- # config/importmap.rb
140
- pin 'datatables_config', to: 'datatables_config.js'
141
- ```
166
+ ### Customizing DataTables Settings
142
167
 
143
- Finally, import it in your application JavaScript:
168
+ Edit `app/javascript/datatables_config.js` to customize the DataTables appearance and behavior:
144
169
 
145
170
  ```javascript
146
- // app/javascript/application.js
147
- import 'datatables_config'
171
+ window.datatablesConfig = {
172
+ // Language strings for DataTables UI
173
+ language: {
174
+ processing: '<div class="spinner-border"></div><div class="mt-2">Loading...</div>',
175
+ lengthMenu: '_MENU_',
176
+ search: `<div class="input-group">
177
+ <span class="input-group-text"><i class="material-symbols-outlined">search</i></span>
178
+ _INPUT_
179
+ <span class="input-group-text">
180
+ <kbd>ctrl + k</kbd>
181
+ </span>
182
+ </div>`,
183
+ info: 'Showing _START_ to _END_ of _TOTAL_ entries',
184
+ paginate: {
185
+ first: 'First',
186
+ last: 'Last',
187
+ next: 'Next',
188
+ previous: 'Previous'
189
+ }
190
+ },
191
+
192
+ // Layout configuration
193
+ layout: {
194
+ topStart: 'pageLength',
195
+ topEnd: 'search',
196
+ bottomStart: 'info',
197
+ bottomEnd: 'paging'
198
+ },
199
+
200
+ // Length menu options
201
+ lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]]
202
+ }
148
203
  ```
149
204
 
150
- Note: You may need to restart your Rails server for changes to take effect.
205
+ ### Available Stimulus Controllers
206
+
207
+ The gem automatically registers the following Stimulus controllers:
208
+
209
+ - `datatable` - Main DataTable controller with server-side processing
210
+ - `filter` - Filter controller with state management and localStorage persistence
151
211
 
152
212
  ## Dependencies
153
213
 
@@ -83,9 +83,18 @@ export default class extends Controller {
83
83
  if (datatableWrapper === null) {
84
84
  Turbo.cache.exemptPageFromCache()
85
85
 
86
+ // Get config - prioritize window.datatablesConfig, fallback to defaults
87
+ const defaultConfig = getDatatablesConfig()
88
+ const userConfig = window.datatablesConfig || {}
89
+ const config = {
90
+ language: { ...defaultConfig.language, ...(userConfig.language || {}) },
91
+ layout: { ...defaultConfig.layout, ...(userConfig.layout || {}) },
92
+ lengthMenu: userConfig.lengthMenu || defaultConfig.lengthMenu
93
+ }
94
+
86
95
  const responsiveValue = this.responsiveValue
87
96
  const options = {
88
- lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]],
97
+ lengthMenu: config.lengthMenu,
89
98
  searching: this.searchingValue,
90
99
  lengthChange: this.lengthChangeValue,
91
100
  processing: this.processingValue,
@@ -96,16 +105,8 @@ export default class extends Controller {
96
105
  order: this.orderValue,
97
106
  columns: this.columnsValue,
98
107
  responsive: this.responsiveValue,
99
- language: {
100
- processing: '<div class="spinner-border"></div><div class="mt-2">Loading...</div>',
101
- lengthMenu: 'show <span class="px-2">_MENU_</span> entries'
102
- },
103
- layout: {
104
- topStart: 'pageLength',
105
- topEnd: 'search',
106
- bottomStart: 'info',
107
- bottomEnd: 'paging'
108
- },
108
+ language: config.language,
109
+ layout: config.layout,
109
110
  initComplete: function() {
110
111
  if (responsiveValue === false) {
111
112
  // Add overflow-x only to the table wrapper (not the whole layout) this is alternative of scrollX
@@ -82,6 +82,7 @@ export default class extends Controller {
82
82
  const labelKey = select.dataset.filterLabelKey
83
83
  const valueKey = select.dataset.filterValueKey
84
84
  const placeholder = select.dataset.filterPlaceholder || 'Select'
85
+ const set_value = select.dataset.filterSetValue || ''
85
86
 
86
87
  url = decodeURIComponent(url).replace(/{(\w+)}/g, (_, key) => {
87
88
  const input = this.element.querySelector(`[data-filter-field-name='${key}']`)
@@ -100,6 +101,12 @@ export default class extends Controller {
100
101
  const option = document.createElement('option')
101
102
  option.value = item[valueKey]
102
103
  option.textContent = item[labelKey]
104
+
105
+ // If set_value matches, mark as selected
106
+ if (set_value && item[valueKey] == set_value) {
107
+ option.selected = true
108
+ }
109
+
103
110
  select.appendChild(option)
104
111
  })
105
112
 
@@ -46,7 +46,8 @@ module StimulusRailsDatatables
46
46
  filter_label_key: remote[:label],
47
47
  filter_value_key: remote[:value],
48
48
  filter_depends_on: depends_on&.to_s,
49
- filter_placeholder: remote[:placeholder] || 'Select an option'
49
+ filter_placeholder: remote[:placeholder] || 'Select an option',
50
+ filter_set_value: remote[:set_value]
50
51
  }
51
52
 
52
53
  select_options = {
@@ -36,9 +36,13 @@ module StimulusRailsDatatables
36
36
  say " application.register('filter', FilterController)"
37
37
  say " window.AppDataTable = AppDataTable"
38
38
  say ''
39
- say '3. Customize app/javascript/datatables_config.js to override default settings'
40
- say '4. Create your datatable classes inheriting from StimulusRailsDatatables::BaseDatatable'
41
- say '5. Use the datatable_for and filter_for helpers in your views'
39
+ say '3. Import app/javascript/datatables_config.js in your application.js:'
40
+ say ''
41
+ say " import './datatables_config'"
42
+ say ''
43
+ say '4. Customize app/javascript/datatables_config.js to override default settings'
44
+ say '5. Create your datatable classes inheriting from StimulusRailsDatatables::BaseDatatable'
45
+ say '6. Use the datatable_for and filter_for helpers in your views'
42
46
  say ''
43
47
  say 'To override the default configuration, edit app/javascript/datatables_config.js and the ff:'
44
48
  say '1. pin it in config/importmap.rb if using importmap:'
@@ -1,10 +1,8 @@
1
- // Import the functions to set and get the config
2
- import { setDatatablesConfig } from 'stimulus_rails_datatables/config'
3
-
4
1
  // DataTables Configuration Override
5
- // Customize these settings to match your application's needs
6
- // This file overrides the default gem configuration
7
- export const datatablesConfig = {
2
+ // Set window.datatablesConfig to customize DataTables settings
3
+ // This will override the gem's default configuration
4
+
5
+ window.datatablesConfig = {
8
6
  // Language strings for DataTables UI
9
7
  language: {
10
8
  processing: '<div class="spinner-border"></div><div class="mt-2">Loading...</div>',
@@ -45,6 +43,3 @@ export const datatablesConfig = {
45
43
  // Length menu options
46
44
  lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]]
47
45
  }
48
-
49
- // Apply the custom configuration
50
- setDatatablesConfig(datatablesConfig)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StimulusRailsDatatables
4
- VERSION = '0.2.2'
4
+ VERSION = '0.3.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.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Den Meralpis