161161
162162#### Map data
163163
164+ ``` ruby
165+ def data
166+ records.map do |record |
167+ {
168+ # a hash of key value pairs
169+ }
170+ end
171+ end
172+ ```
173+
174+ ``` ruby
175+ def data
176+ records.map do |record |
177+ {
178+ first_name: record.first_name,
179+ last_name: record.last_name,
180+ bio: record.bio,
181+ # 'DT_RowId' => record.id, # This will set the id attribute on the corresponding <tr> in the datatable
182+ }
183+ end
184+ end
185+ ```
186+
187+ You can either use the v0.3 Array style for your columns :
188+
164189``` ruby
165190def data
166191 records.map do |record |
@@ -187,21 +212,6 @@ def data
187212end
188213```
189214
190- You can either use a Hash style for your columns :
191-
192- ``` ruby
193- def data
194- records.map do |record |
195- {
196- first_name: record.first_name,
197- last_name: record.last_name,
198- bio: record.bio,
199- # 'DT_RowId' => record.id, # This will set the id attribute on the corresponding <tr> in the datatable
200- }
201- end
202- end
203- ```
204-
205215[ See here] ( #using-view-helpers ) if you need to use view helpers like ` link_to ` , ` mail_to ` , ` resource_path ` , etc.
206216
207217
@@ -225,8 +235,17 @@ def get_raw_records
225235end
226236```
227237
228- Obviously, you can construct your query as required for the use case the
229- datatable is used. Example: ` User.active.with_recent_messages ` .
238+ Obviously, you can construct your query as required for the use case the datatable is used.
239+
240+ Example:
241+
242+ ``` ruby
243+ def get_raw_records
244+ User .active.with_recent_messages
245+ end
246+ ```
247+
248+ You can put any logic in ` get_raw_records ` [ based on any parameters you inject] ( #options ) in the ` Datatable ` object.
230249
231250> __ IMPORTANT:__ Make sure to return an ` ActiveRecord::Relation ` object
232251> as the end product of this method.
349368
350369Don't forget to make sure the proper route has been added to ` config/routes.rb ` .
351370
371+ [ See here] ( #options ) to inject params in the ` UserDatatable ` .
352372
353373### Wire up the Javascript
354374
@@ -519,11 +539,11 @@ class MyCustomDatatable < AjaxDatatablesRails::Base
519539 # example: mapping the 2d jsonified array returned.
520540 def data
521541 records.map do |record |
522- [
523- link_to(record.fname, edit_resource_path(record)),
524- mail_to(record.email),
542+ {
543+ first_name: link_to(record.fname, edit_resource_path(record)),
544+ email: mail_to(record.email),
525545 # other attributes
526- ]
546+ }
527547 end
528548 end
529549end
0 commit comments