stimulus_rails_datatables 0.2.2 → 0.2.3

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: afa31906e681160cdf26849d88dfd9984e538926651a4fb6bdabc09099d6f0f2
4
+ data.tar.gz: c885e25ed65fe73935b4db0b56a4a350e13c3c2da85f1df783bda34c92a069c3
5
5
  SHA512:
6
- metadata.gz: f2f88681c77f4d031577426fcc757c8f6955ae940cd22e9dbad49c8470bcc6e7ff08119857f2756b275431d5f482318b8b48244a31e1f3e24eacfe45aef55786
7
- data.tar.gz: f88309876186c9209b36673ac36b40553ebf4e090ed63e20b740d958e6667ebefb8182f0966a756e9d149f1088a2dc3e6cf1e14d2e5e919f79b39c3889ff26fa
6
+ metadata.gz: acbcc5845c267568662b028883a5136f979969f083d928f2605db0045851801180713a659e801f4d5717eaecc8b5d07c2ab95cff1ac95f3827d75f8329d91205
7
+ data.tar.gz: 56da43c223037d59eee5c353bb796183e7efc13b7e1aefdee6995b700f1c053cd8d91aa3faa985fb6f7496be345078c448290811773e294164555ae68423708b
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
@@ -109,6 +132,9 @@ end
109
132
  ### JavaScript API
110
133
 
111
134
  ```javascript
135
+ // Access via window.AppDataTable or import directly
136
+ import { AppDataTable } from 'stimulus_rails_datatables/app_datatable'
137
+
112
138
  // Reload a specific datatable
113
139
  AppDataTable.reload('#users-table')
114
140
 
@@ -119,35 +145,68 @@ AppDataTable.load('#users-table', '/users?status=active')
119
145
  AppDataTable.reloadAll()
120
146
  ```
121
147
 
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
148
+ ## Controller Setup
128
149
 
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:
150
+ In your Rails controller, respond to JSON format for DataTables:
131
151
 
132
- ```bash
133
- rails generate stimulus_rails_datatables:install
152
+ ```ruby
153
+ class UsersController < ApplicationController
154
+ def index
155
+ respond_to do |format|
156
+ format.html
157
+ format.json { render json: UsersDatatable.new(view_context) }
158
+ end
159
+ end
160
+ end
134
161
  ```
135
162
 
136
- Once created, you can pin it in your importmap configuration:
163
+ ## Configuration
137
164
 
138
- ```ruby
139
- # config/importmap.rb
140
- pin 'datatables_config', to: 'datatables_config.js'
141
- ```
165
+ ### Customizing DataTables Settings
142
166
 
143
- Finally, import it in your application JavaScript:
167
+ Edit `app/javascript/datatables_config.js` to customize the DataTables appearance and behavior:
144
168
 
145
169
  ```javascript
146
- // app/javascript/application.js
147
- import 'datatables_config'
170
+ window.datatablesConfig = {
171
+ // Language strings for DataTables UI
172
+ language: {
173
+ processing: '<div class="spinner-border"></div><div class="mt-2">Loading...</div>',
174
+ lengthMenu: '_MENU_',
175
+ search: `<div class="input-group">
176
+ <span class="input-group-text"><i class="material-symbols-outlined">search</i></span>
177
+ _INPUT_
178
+ <span class="input-group-text">
179
+ <kbd>ctrl + k</kbd>
180
+ </span>
181
+ </div>`,
182
+ info: 'Showing _START_ to _END_ of _TOTAL_ entries',
183
+ paginate: {
184
+ first: 'First',
185
+ last: 'Last',
186
+ next: 'Next',
187
+ previous: 'Previous'
188
+ }
189
+ },
190
+
191
+ // Layout configuration
192
+ layout: {
193
+ topStart: 'pageLength',
194
+ topEnd: 'search',
195
+ bottomStart: 'info',
196
+ bottomEnd: 'paging'
197
+ },
198
+
199
+ // Length menu options
200
+ lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]]
201
+ }
148
202
  ```
149
203
 
150
- Note: You may need to restart your Rails server for changes to take effect.
204
+ ### Available Stimulus Controllers
205
+
206
+ The gem automatically registers the following Stimulus controllers:
207
+
208
+ - `datatable` - Main DataTable controller with server-side processing
209
+ - `filter` - Filter controller with state management and localStorage persistence
151
210
 
152
211
  ## Dependencies
153
212
 
@@ -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
@@ -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.2.3'
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.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Den Meralpis