stimulus_rails_datatables 0.1.0 → 0.1.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 +4 -4
- data/README.md +23 -0
- data/app/assets/javascripts/stimulus_rails_datatables/app_datatable.js +5 -0
- data/app/assets/javascripts/stimulus_rails_datatables/config.js +44 -0
- data/app/assets/javascripts/stimulus_rails_datatables/datatables_controller.js +16 -2
- data/config/importmap.rb +1 -0
- data/lib/generators/{nueca_datatables → stimulus_rails_datatables}/install/install_generator.rb +11 -4
- data/lib/generators/stimulus_rails_datatables/install/templates/datatables_config.js +50 -0
- data/lib/stimulus_rails_datatables/version.rb +1 -1
- metadata +4 -3
- data/lib/generators/nueca_datatables/install/templates/nueca_datatables.rb +0 -18
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d839615241e07844f4051bda906e5b0678c4a2e41072598c284966379ba8a576
|
|
4
|
+
data.tar.gz: 59f3872b9593296872b1838af1039f67687ffeab0ef875de5d36339053f370bd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fd41c028f9d65c93716f8d9a1317966271637c1c5f368bd623d7f1ad86bd39dbc59603d6dd31f025042c16d78e11fe37559cc4198bdcb13e417e67a1b9842609
|
|
7
|
+
data.tar.gz: aee985f8fa8699cc7280dc387c60db55976acaf869b2f16d550012ffe5c2e804c31dadffab7d2a5b8ecb5b9eb148fd8c192c8a572428fa9d0c3a94291020abf2
|
data/README.md
CHANGED
|
@@ -126,6 +126,29 @@ The gem automatically registers Stimulus controllers and imports required JavaSc
|
|
|
126
126
|
- `datatable` - Main DataTable controller
|
|
127
127
|
- `filter` - Filter controller with state management
|
|
128
128
|
|
|
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:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
rails generate stimulus_rails_datatables:install
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Once created, you can pin it in your importmap configuration:
|
|
137
|
+
|
|
138
|
+
```ruby
|
|
139
|
+
# config/importmap.rb
|
|
140
|
+
pin 'datatables_config', to: 'datatables_config.js'
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Finally, import it in your application JavaScript:
|
|
144
|
+
|
|
145
|
+
```javascript
|
|
146
|
+
// app/javascript/application.js
|
|
147
|
+
import 'datatables_config'
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Note: You may need to restart your Rails server for changes to take effect.
|
|
151
|
+
|
|
129
152
|
## Dependencies
|
|
130
153
|
|
|
131
154
|
- Rails >= 7.0
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import DataTable from 'datatables.net-responsive-bs5'
|
|
2
2
|
|
|
3
|
+
// Set global DataTables default for orderSequence
|
|
4
|
+
Object.assign(DataTable.defaults.column, {
|
|
5
|
+
orderSequence: ['desc', 'asc']
|
|
6
|
+
})
|
|
7
|
+
|
|
3
8
|
class AppDataTable {
|
|
4
9
|
constructor(selector, options) {
|
|
5
10
|
// Initialize the DataTable just like the original one
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// DataTables Configuration
|
|
2
|
+
// Customize these settings to match your application's needs
|
|
3
|
+
|
|
4
|
+
let datatablesConfig = {
|
|
5
|
+
// Language strings for DataTables UI
|
|
6
|
+
language: {
|
|
7
|
+
processing: '<div class="spinner-border"></div><div class="mt-2">Loading...</div>',
|
|
8
|
+
lengthMenu: 'show <span class="px-2">_MENU_</span> entries',
|
|
9
|
+
// Uncomment and customize any of these as needed:
|
|
10
|
+
// search: '_INPUT_',
|
|
11
|
+
// info: 'Showing _START_ to _END_ of _TOTAL_ entries',
|
|
12
|
+
// infoEmpty: 'Showing 0 to 0 of 0 entries',
|
|
13
|
+
// infoFiltered: '(filtered from _MAX_ total entries)',
|
|
14
|
+
// paginate: {
|
|
15
|
+
// first: 'First',
|
|
16
|
+
// last: 'Last',
|
|
17
|
+
// next: 'Next',
|
|
18
|
+
// previous: 'Previous'
|
|
19
|
+
// },
|
|
20
|
+
// emptyTable: 'No data available',
|
|
21
|
+
// zeroRecords: 'No matching records found'
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
// Default layout configuration
|
|
25
|
+
layout: {
|
|
26
|
+
topStart: 'pageLength',
|
|
27
|
+
topEnd: 'search',
|
|
28
|
+
bottomStart: 'info',
|
|
29
|
+
bottomEnd: 'paging'
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
// Length menu options
|
|
33
|
+
lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Function to let the app override defaults
|
|
37
|
+
export function setDatatablesConfig(overrideConfig) {
|
|
38
|
+
datatablesConfig = { ...datatablesConfig, ...overrideConfig };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Function to read the config
|
|
42
|
+
export function getDatatablesConfig() {
|
|
43
|
+
return datatablesConfig;
|
|
44
|
+
}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import { AppDataTable } from 'stimulus_rails_datatables/app_datatable'
|
|
5
5
|
import { Controller } from '@hotwired/stimulus'
|
|
6
|
+
import { getDatatablesConfig } from 'stimulus_rails_datatables/config'
|
|
6
7
|
|
|
7
8
|
export default class extends Controller {
|
|
8
9
|
static values = {
|
|
@@ -19,6 +20,8 @@ export default class extends Controller {
|
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
connect() {
|
|
23
|
+
this.datatablesConfig = getDatatablesConfig();
|
|
24
|
+
|
|
22
25
|
// try to use saved filters if present, else listen for filters:ready
|
|
23
26
|
const filterEl = document.querySelector('.filter-form[data-filter-root-key]')
|
|
24
27
|
if (filterEl) {
|
|
@@ -79,7 +82,7 @@ export default class extends Controller {
|
|
|
79
82
|
if (datatableWrapper === null) {
|
|
80
83
|
Turbo.cache.exemptPageFromCache()
|
|
81
84
|
|
|
82
|
-
|
|
85
|
+
const options = {
|
|
83
86
|
lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]],
|
|
84
87
|
searching: this.searchingValue,
|
|
85
88
|
lengthChange: this.lengthChangeValue,
|
|
@@ -101,7 +104,18 @@ export default class extends Controller {
|
|
|
101
104
|
bottomStart: 'info',
|
|
102
105
|
bottomEnd: 'paging'
|
|
103
106
|
}
|
|
104
|
-
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Add drawCallback to dispatch custom event
|
|
110
|
+
const appDataTable = new AppDataTable(`#${datatableId}`, options).table
|
|
111
|
+
if (appDataTable) {
|
|
112
|
+
appDataTable.on('draw', () => {
|
|
113
|
+
this.element.dispatchEvent(new CustomEvent('datatable:drawn', {
|
|
114
|
+
bubbles: true,
|
|
115
|
+
detail: { table: appDataTable }
|
|
116
|
+
}))
|
|
117
|
+
})
|
|
118
|
+
}
|
|
105
119
|
}
|
|
106
120
|
|
|
107
121
|
return appDataTable
|
data/config/importmap.rb
CHANGED
|
@@ -10,3 +10,4 @@ pin 'datatables.net-responsive-bs5', to: 'responsive.bootstrap5.min.js'
|
|
|
10
10
|
pin 'stimulus_rails_datatables/app_datatable', to: 'stimulus_rails_datatables/app_datatable.js'
|
|
11
11
|
pin 'stimulus_rails_datatables/datatables_controller', to: 'stimulus_rails_datatables/datatables_controller.js'
|
|
12
12
|
pin 'stimulus_rails_datatables/filter_controller', to: 'stimulus_rails_datatables/filter_controller.js'
|
|
13
|
+
pin 'stimulus_rails_datatables/config', to: 'stimulus_rails_datatables/config.js'
|
data/lib/generators/{nueca_datatables → stimulus_rails_datatables}/install/install_generator.rb
RENAMED
|
@@ -7,8 +7,8 @@ module StimulusRailsDatatables
|
|
|
7
7
|
|
|
8
8
|
desc 'Creates StimulusRailsDatatables initializer for your application'
|
|
9
9
|
|
|
10
|
-
def
|
|
11
|
-
template '
|
|
10
|
+
def copy_config
|
|
11
|
+
template 'datatables_config.js', 'app/javascript/datatables_config.js'
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
def show_readme
|
|
@@ -36,8 +36,15 @@ module StimulusRailsDatatables
|
|
|
36
36
|
say " application.register('filter', FilterController)"
|
|
37
37
|
say " window.AppDataTable = AppDataTable"
|
|
38
38
|
say ''
|
|
39
|
-
say '3.
|
|
40
|
-
say '4.
|
|
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'
|
|
42
|
+
say ''
|
|
43
|
+
say 'To override the default configuration, edit app/javascript/datatables_config.js and the ff:'
|
|
44
|
+
say '1. pin it in config/importmap.rb if using importmap:'
|
|
45
|
+
say " pin 'datatables_config', to: 'datatables_config.js'"
|
|
46
|
+
say '2. import and set the config in app/javascript/application.js:'
|
|
47
|
+
say " import 'datatables_config'"
|
|
41
48
|
say ''
|
|
42
49
|
say 'For more information, see: https://github.com/denmarkmeralpis/stimulus_rails_datatables'
|
|
43
50
|
say '=' * 80
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Import the functions to set and get the config
|
|
2
|
+
import { setDatatablesConfig } from 'stimulus_rails_datatables/config'
|
|
3
|
+
|
|
4
|
+
// 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 = {
|
|
8
|
+
// Language strings for DataTables UI
|
|
9
|
+
language: {
|
|
10
|
+
processing: '<div class="spinner-border"></div><div class="mt-2">Loading...</div>',
|
|
11
|
+
lengthMenu: 'show <span class="px-2">_MENU_</span> entries',
|
|
12
|
+
|
|
13
|
+
// Uncomment and customize any of these as needed:
|
|
14
|
+
// search: `<div class="ms-auto d-flex flex-wrap btn-list">
|
|
15
|
+
// <div class="input-group input-group-flat w-auto" data-controller="kbd-focus">
|
|
16
|
+
// <span class="input-group-text"><i class="material-symbols-outlined">search</i></span>
|
|
17
|
+
// _INPUT_
|
|
18
|
+
// <span class="input-group-text">
|
|
19
|
+
// <kbd>ctrl + k</kbd>
|
|
20
|
+
// </span>
|
|
21
|
+
// </div>
|
|
22
|
+
// </div>`,
|
|
23
|
+
|
|
24
|
+
// info: 'Showing _START_ to _END_ of _TOTAL_ entries',
|
|
25
|
+
// infoEmpty: 'Showing 0 to 0 of 0 entries',
|
|
26
|
+
// infoFiltered: '(filtered from _MAX_ total entries)',
|
|
27
|
+
// paginate: {
|
|
28
|
+
// first: 'First',
|
|
29
|
+
// last: 'Last',
|
|
30
|
+
// next: 'Next',
|
|
31
|
+
// previous: 'Previous'
|
|
32
|
+
// },
|
|
33
|
+
// emptyTable: 'No data available',
|
|
34
|
+
// zeroRecords: 'No matching records found'
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
// Default layout configuration
|
|
38
|
+
layout: {
|
|
39
|
+
topStart: 'pageLength',
|
|
40
|
+
topEnd: 'search',
|
|
41
|
+
bottomStart: 'info',
|
|
42
|
+
bottomEnd: 'paging'
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
// Length menu options
|
|
46
|
+
lengthMenu: [[10, 25, 50, 100], [10, 25, 50, 100]]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Apply the custom configuration
|
|
50
|
+
setDatatablesConfig(datatablesConfig)
|
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.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Den Meralpis
|
|
@@ -104,14 +104,15 @@ files:
|
|
|
104
104
|
- README.md
|
|
105
105
|
- Rakefile
|
|
106
106
|
- app/assets/javascripts/stimulus_rails_datatables/app_datatable.js
|
|
107
|
+
- app/assets/javascripts/stimulus_rails_datatables/config.js
|
|
107
108
|
- app/assets/javascripts/stimulus_rails_datatables/datatables_controller.js
|
|
108
109
|
- app/assets/javascripts/stimulus_rails_datatables/filter_controller.js
|
|
109
110
|
- app/datatables/stimulus_rails_datatables/base_datatable.rb
|
|
110
111
|
- app/helpers/stimulus_rails_datatables/datatable_helper.rb
|
|
111
112
|
- app/helpers/stimulus_rails_datatables/filter_helper.rb
|
|
112
113
|
- config/importmap.rb
|
|
113
|
-
- lib/generators/
|
|
114
|
-
- lib/generators/
|
|
114
|
+
- lib/generators/stimulus_rails_datatables/install/install_generator.rb
|
|
115
|
+
- lib/generators/stimulus_rails_datatables/install/templates/datatables_config.js
|
|
115
116
|
- lib/stimulus_rails_datatables.rb
|
|
116
117
|
- lib/stimulus_rails_datatables/engine.rb
|
|
117
118
|
- lib/stimulus_rails_datatables/version.rb
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# StimulusRailsDatatables configuration
|
|
4
|
-
#
|
|
5
|
-
# This initializer is optional. The gem works out of the box with sensible defaults.
|
|
6
|
-
# You can customize the behavior by uncommenting and modifying the options below.
|
|
7
|
-
|
|
8
|
-
StimulusRailsDatatables.configure do |config|
|
|
9
|
-
# Configure default options for all datatables
|
|
10
|
-
# config.default_order = [[1, 'desc']]
|
|
11
|
-
# config.default_page_length = 25
|
|
12
|
-
# config.default_searching = true
|
|
13
|
-
# config.default_length_change = true
|
|
14
|
-
|
|
15
|
-
# Configure filter state persistence
|
|
16
|
-
# config.filter_state_key_prefix = 'filterState'
|
|
17
|
-
# config.filter_state_expires_in = 1.day
|
|
18
|
-
end
|