You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* found a better way to declare models and columns inside the searchable
and sortable columns methods
* for the time being, the old way of declaring such columns will be
supported, but it will display a deprecation warning.
Copy file name to clipboardExpand all lines: README.md
+94-60Lines changed: 94 additions & 60 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,14 +72,12 @@ Take a look [here](#generator-syntax) for an explanation about the generator syn
72
72
# include AjaxDatatablesRails::Extensions::SimplePaginator
73
73
74
74
defsortable_columns
75
-
# list columns inside the Array in string dot notation.
76
-
# Example: 'users.email'
75
+
# Declare strings in this format: ModelName.column_name
77
76
@sortable_columns||= []
78
77
end
79
78
80
79
defsearchable_columns
81
-
# list columns inside the Array in string dot notation.
82
-
# Example: 'users.email'
80
+
# Declare strings in this format: ModelName.column_name
83
81
@searchable_columns||= []
84
82
end
85
83
```
@@ -90,23 +88,28 @@ the gems bundled in your project. For example, if your models are using `Kaminar
90
88
91
89
* For `sortable_columns`, assign an array of the database columns that correspond to the columns in our view table. For example `[users.f_name, users.l_name, users.bio]`. This array is used for sorting by various columns.
92
90
93
-
* For `searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]`
91
+
* For `searchable_columns`, assign an array of the database columns that you want searchable by datatables. Suppose we need to sort and search users`:first_name`, `last_name` and `bio`.
Starting on version `0.3.0`, we are implementing a pseudo code way of declaring the array of both `searchable_columns` and `sortable_columns` method.
314
+
315
+
Example. Suppose we have the following models: `User`, `PurchaseOrder`, `Purchase::LineItem` and we need to have several columns from those models available in our datatable to search and sort by.
316
+
317
+
```ruby
318
+
# we use the ModelName.column_name notation to declare our columns
319
+
320
+
defsearchable_columns
321
+
@searchable_columns||= [
322
+
'User.first_name',
323
+
'User.last_name',
324
+
'PurchaseOrder.number',
325
+
'PurchaseOrder.created_at',
326
+
'Purchase::LineItem.quantity',
327
+
'Purchase::LineItem.unit_price',
328
+
'Purchase::LineItem.item_total'
329
+
]
330
+
end
331
+
332
+
defsortable_columns
333
+
@sortable_columns||= [
334
+
'User.first_name',
335
+
'User.last_name',
336
+
'PurchaseOrder.number',
337
+
'PurchaseOrder.created_at'
338
+
]
339
+
end
340
+
```
341
+
342
+
##### What if the datatable itself is namespaced?
343
+
Example: what if the datatable is namespaced into an `Admin` module?
Taking the same models and columns, we would define it like this:
353
+
354
+
```ruby
355
+
defsearchable_columns
356
+
@searchable_columns||= [
357
+
'::User.first_name',
358
+
'::User.last_name',
359
+
'::PurchaseOrder.number',
360
+
'::PurchaseOrder.created_at',
361
+
'::Purchase::LineItem.quantity',
362
+
'::Purchase::LineItem.unit_price',
363
+
'::Purchase::LineItem.item_total'
364
+
]
365
+
end
366
+
```
367
+
368
+
Pretty much like you would do it, if you were inside a namespaced controller.
369
+
336
370
#### Searching on non text-based columns
337
371
338
372
It always comes the time when you need to add a non-string/non-text based column to the `@searchable_columns` array, so you can perform searches against these column types (example: numeric, date, time).
::AjaxDatatablesRails::Base.deprecated'[DEPRECATED] Using table_name.column_name notation is deprecated. Please refer to: https://github.com/antillas21/ajax-datatables-rails#searchable-and-sortable-columns-syntax'
::AjaxDatatablesRails::Base.deprecated'[DEPRECATED] Using table_name.column_name notation is deprecated. Please refer to: https://github.com/antillas21/ajax-datatables-rails#searchable-and-sortable-columns-syntax'
0 commit comments