trk_datatables 0.1.6 → 0.1.7
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/Gemfile.lock +1 -1
- data/README.md +112 -17
- data/lib/trk_datatables/base.rb +5 -1
- data/lib/trk_datatables/dt_params.rb +2 -3
- data/lib/trk_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: 1364b277e524ba846d027f01c3411715423f6947525154f96711eb3fabade651
|
4
|
+
data.tar.gz: d97b863eb1497fa9c9ee894fc0406f2986003a72a927bbc7fff00ccde255007a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2545a0e9c8aa3dfc4215c99f0be6cd2123e9178397b4c2eb74b3f5dbe0f89be3d0df410626512ddf5eb667f7731c1029ae2c3da73e9bc8123d755d9672cbdee4
|
7
|
+
data.tar.gz: 7dd7f80721e171e207fb237f051f4884eec69584972344f67981dd46f0e3e502052c1393e11c51221ffe017d18c0b64c0a23d45ce3b6104533873d460b3f8039
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,16 +1,34 @@
|
|
1
1
|
# Trk Datatables
|
2
2
|
|
3
|
-
This is a [trk_datatables gem](https://
|
3
|
+
This is a [trk_datatables gem](https://rubygems.org/gems/trk_datatables) that you
|
4
4
|
can use with trk_datatables npm package for easier usage of [Datatables js library](https://datatables.net)
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
After [configuring one
|
7
|
+
datatable](https://github.com/trkin/trk_datatables#configuration), it gives you
|
8
|
+
one liner command to generate first page in html (so non-js crawlers can see
|
9
|
+
it), global search, filtering and sorting by one or more columns, adding map and
|
10
|
+
other complex listing based on GET params.
|
11
|
+
|
12
|
+
Table of Contents
|
13
|
+
=================
|
14
|
+
|
15
|
+
* [Trk Datatables](#trk-datatables)
|
16
|
+
* [Table of Contents](#table-of-contents)
|
17
|
+
* [Installation](#installation)
|
18
|
+
* [Usage example in Ruby on Rails](#usage-example-in-ruby-on-rails)
|
19
|
+
* [Configuration](#configuration)
|
20
|
+
* [Global search](#global-search)
|
21
|
+
* [Column 'ILIKE' and 'BETWEEN' search](#column-ilike-and-between-search)
|
22
|
+
* [Column 'IN' search](#column-in-search)
|
23
|
+
* [Action column](#action-column)
|
24
|
+
* [Params](#params)
|
25
|
+
* [Saved Preferences (optional)](#saved-preferences-optional)
|
26
|
+
* [Debug](#debug)
|
27
|
+
* [Alternatives](#alternatives)
|
28
|
+
* [Development](#development)
|
29
|
+
* [Contributing](#contributing)
|
30
|
+
* [License](#license)
|
31
|
+
* [Code of Conduct](#code-of-conduct)
|
14
32
|
|
15
33
|
## Installation
|
16
34
|
|
@@ -97,13 +115,13 @@ And finally in a view, use `render_html` to have first page show up prerendered
|
|
97
115
|
|
98
116
|
## Configuration
|
99
117
|
|
100
|
-
|
118
|
+
Datatable will search all columns that you defined as keys in `columns` using a
|
119
|
+
`ILIKE` (ie `.matches` in Arel ActiveRecord).
|
101
120
|
|
102
|
-
|
103
|
-
(search is done for specific columns).
|
121
|
+
On frontend there are two types of search: global (search all columns) and
|
122
|
+
column search (search is done for specific columns).
|
104
123
|
|
105
|
-
|
106
|
-
(ie `.matches`).
|
124
|
+
### Global search
|
107
125
|
|
108
126
|
You can add more columns to global search by overriding `global_search_columns`
|
109
127
|
method.
|
@@ -151,7 +169,7 @@ You can use column_option `search: :checkbox` so for column_type_in_db `:boolean
|
|
151
169
|
it will provide checkbox. For other column_type_in_db it will match if value is
|
152
170
|
NULL or NOT NULL.
|
153
171
|
|
154
|
-
|
172
|
+
### Action column
|
155
173
|
|
156
174
|
You can use one column for actions (so it is not related to any db column) just
|
157
175
|
use empty column_key
|
@@ -174,7 +192,7 @@ use empty column_key
|
|
174
192
|
end
|
175
193
|
```
|
176
194
|
|
177
|
-
|
195
|
+
### Params
|
178
196
|
|
179
197
|
To set parameters that you can use for links to set column search value, use
|
180
198
|
this `PostsDatatable.param_set` for example
|
@@ -194,7 +212,7 @@ If you need, you can fetch params with this helper
|
|
194
212
|
PostsDatatable.param_get('users.email', params)
|
195
213
|
```
|
196
214
|
|
197
|
-
|
215
|
+
### Saved Preferences (optional)
|
198
216
|
|
199
217
|
You can save column order and page length in User.preferences field so
|
200
218
|
next time user navigate to same page will see the same order and page length. It
|
@@ -225,6 +243,76 @@ end
|
|
225
243
|
It will store order and page lenght inside `dt_preferences` on
|
226
244
|
`user.preferences`.
|
227
245
|
|
246
|
+
### Additional data to json response
|
247
|
+
|
248
|
+
You can override `additional_data_for_json` that will be included in json
|
249
|
+
response
|
250
|
+
```
|
251
|
+
# app/datatables/posts_datatable.rb
|
252
|
+
class PostsDatatable < TrkDatatables::ActiveRecord
|
253
|
+
def additional_data_for_json
|
254
|
+
{ columns: columns }
|
255
|
+
end
|
256
|
+
end
|
257
|
+
```
|
258
|
+
|
259
|
+
## Different response for mobile app
|
260
|
+
|
261
|
+
You can use condition to provide different data, for example let's assume
|
262
|
+
`@view.api_user?` returns true for json requests from mobile app. Here is
|
263
|
+
example that provides different columns for normal and api_user:
|
264
|
+
|
265
|
+
```
|
266
|
+
# app/datatables/posts_datatable.rb
|
267
|
+
class PostsDatatable < TrkDatatables::ActiveRecord
|
268
|
+
def columns
|
269
|
+
@view.api_user? ? columns_for_api : columns_for_html
|
270
|
+
end
|
271
|
+
|
272
|
+
def columns_for_html
|
273
|
+
{
|
274
|
+
'subscribers.subscriberid': {},
|
275
|
+
'subscribers.name': {},
|
276
|
+
}
|
277
|
+
end
|
278
|
+
|
279
|
+
def columns_for_api
|
280
|
+
{
|
281
|
+
'subscribers.id': {},
|
282
|
+
'subscribers.subscriberid': {},
|
283
|
+
'subscribers.name': {},
|
284
|
+
}
|
285
|
+
end
|
286
|
+
|
287
|
+
def rows(filtered)
|
288
|
+
@view.api_user? ? rows_for_api(filtered) : rows_for_html(filtered)
|
289
|
+
end
|
290
|
+
|
291
|
+
def rows_for_html(filtered)
|
292
|
+
filtered.map do |subscriber|
|
293
|
+
[
|
294
|
+
@view.link_to(subscriber.subscriberid, subscriber),
|
295
|
+
subscriber.name,
|
296
|
+
]
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
def rows_for_api(filtered)
|
301
|
+
filtered.map do |subscriber|
|
302
|
+
[
|
303
|
+
subscriber.id,
|
304
|
+
subscriber.subscriberid,
|
305
|
+
subscriber.name,
|
306
|
+
]
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
def additional_data_for_json
|
311
|
+
@view.api_user? ? columns_for_api : nil
|
312
|
+
end
|
313
|
+
end
|
314
|
+
```
|
315
|
+
|
228
316
|
## Debug
|
229
317
|
|
230
318
|
You can override some of the methos and put byebug, for example
|
@@ -239,6 +327,13 @@ end
|
|
239
327
|
|
240
328
|
```
|
241
329
|
|
330
|
+
## Alternatives
|
331
|
+
|
332
|
+
There are alternatives, for example:
|
333
|
+
* [jbox-web/ajax-datatables-rails](https://github.com/jbox-web/ajax-datatables-rails)
|
334
|
+
excellent gem but it's scope is only to produce JSON. I wanted to have server
|
335
|
+
side rendering and more advance listing
|
336
|
+
|
242
337
|
## Development
|
243
338
|
|
244
339
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/trk_datatables/base.rb
CHANGED
@@ -169,11 +169,15 @@ module TrkDatatables
|
|
169
169
|
@dt_params.as_json(
|
170
170
|
all_items.count,
|
171
171
|
filtered_items.count,
|
172
|
-
columns,
|
173
172
|
rows(ordered_paginated_filtered_items),
|
173
|
+
additional_data_for_json
|
174
174
|
)
|
175
175
|
end
|
176
176
|
|
177
|
+
def additional_data_for_json
|
178
|
+
{}
|
179
|
+
end
|
180
|
+
|
177
181
|
def filtered_items
|
178
182
|
filter_by_search_all filter_by_columns all_items
|
179
183
|
end
|
@@ -89,15 +89,14 @@ module TrkDatatables
|
|
89
89
|
@params.dig(:search, :value) || ''
|
90
90
|
end
|
91
91
|
|
92
|
-
def as_json(all_count, filtered_items_count,
|
92
|
+
def as_json(all_count, filtered_items_count, data, additional = {})
|
93
93
|
draw = @params[:draw].to_i
|
94
94
|
{
|
95
95
|
draw: draw,
|
96
96
|
recordsTotal: all_count,
|
97
97
|
recordsFiltered: filtered_items_count,
|
98
|
-
columns: columns,
|
99
98
|
data: data,
|
100
|
-
}
|
99
|
+
}.merge additional
|
101
100
|
end
|
102
101
|
|
103
102
|
def self.param_set(column_index, value)
|