@@ -545,7 +545,7 @@ a file inside the `config/initializers` directory.
545
545
#### Using view helpers
546
546
547
547
Sometimes you'll need to use view helper methods like ` link_to ` , ` h ` , ` mailto ` ,
548
- ` edit_resource_path ` in the returned JSON representation returned by the ` data `
548
+ ` edit_resource_path ` , ` check_box_tag ` in the returned JSON representation returned by the ` data `
549
549
method.
550
550
551
551
To have these methods available to be used, this is the way to go:
@@ -558,22 +558,46 @@ class MyCustomDatatable < AjaxDatatablesRails::Base
558
558
def_delegator :@view , :mail_to
559
559
560
560
# or define them in one pass
561
- def_delegators :@view , :link_to , :h , :mailto , :edit_resource_path , :other_method
561
+ def_delegators :@view , :link_to , :h , :mailto , :edit_resource_path , :check_box_tag , :other_method
562
+
563
+ # Define columns as described above for `id`, `first_name`, `email`, and others
564
+ def view_columns
565
+ ...
566
+ end
562
567
563
568
# now, you'll have these methods available to be used anywhere
564
569
# example: mapping the 2d jsonified array returned.
565
570
def data
566
571
records.map do |record |
567
572
{
573
+ id: check_box_tag(' users[]' , record.id),
568
574
first_name: link_to(record.fname, edit_resource_path(record)),
569
- email: mail_to(record.email),
575
+ email: mail_to(record.email)
570
576
# other attributes
571
577
}
572
578
end
573
579
end
574
580
end
575
581
```
576
582
583
+ If you want to keep things tidy in the data mapping method, you could use
584
+ [ Draper] ( https://github.com/drapergem/draper ) to define column mappings like below.
585
+
586
+ ``` ruby
587
+ ...
588
+ def data
589
+ records.map do |record |
590
+ {
591
+ id: record.decorate.id,
592
+ first_name: record.decorate.first_name,
593
+ email: record.decorate.email
594
+ # other attributes
595
+ }
596
+ end
597
+ end
598
+ ...
599
+ ```
600
+
577
601
578
602
#### Options
579
603
0 commit comments