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 +4 -4
- data/README.md +78 -19
- data/app/assets/javascripts/stimulus_rails_datatables/datatables_controller.js +12 -11
- data/lib/generators/stimulus_rails_datatables/install/install_generator.rb +7 -3
- data/lib/generators/stimulus_rails_datatables/install/templates/datatables_config.js +4 -9
- data/lib/stimulus_rails_datatables/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: afa31906e681160cdf26849d88dfd9984e538926651a4fb6bdabc09099d6f0f2
|
|
4
|
+
data.tar.gz: c885e25ed65fe73935b4db0b56a4a350e13c3c2da85f1df783bda34c92a069c3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
##
|
|
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
|
-
|
|
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
|
-
```
|
|
133
|
-
|
|
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
|
-
|
|
163
|
+
## Configuration
|
|
137
164
|
|
|
138
|
-
|
|
139
|
-
# config/importmap.rb
|
|
140
|
-
pin 'datatables_config', to: 'datatables_config.js'
|
|
141
|
-
```
|
|
165
|
+
### Customizing DataTables Settings
|
|
142
166
|
|
|
143
|
-
|
|
167
|
+
Edit `app/javascript/datatables_config.js` to customize the DataTables appearance and behavior:
|
|
144
168
|
|
|
145
169
|
```javascript
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
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.
|
|
40
|
-
say '
|
|
41
|
-
say
|
|
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
|
-
//
|
|
6
|
-
// This
|
|
7
|
-
|
|
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)
|