effective_datatables 4.7.16 → 4.8.16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/MIT-LICENSE +1 -1
- data/README.md +215 -24
- data/app/assets/javascripts/effective_datatables/flash.js.coffee +1 -1
- data/app/assets/javascripts/effective_datatables/initialize.js.coffee +18 -5
- data/app/assets/javascripts/effective_datatables/inline_crud.js.coffee +42 -13
- data/app/assets/javascripts/effective_datatables/reorder.js.coffee +8 -2
- data/app/assets/javascripts/effective_datatables/reset.js.coffee +23 -2
- data/app/assets/javascripts/vendor/jquery.delayedChange.js +1 -2
- data/app/assets/stylesheets/dataTables/dataTables.bootstrap4.scss +1 -3
- data/app/helpers/effective_datatables_helper.rb +4 -1
- data/app/helpers/effective_datatables_private_helper.rb +12 -8
- data/app/models/effective/datatable.rb +34 -3
- data/app/models/effective/datatable_value_tool.rb +0 -6
- data/app/models/effective/effective_datatable/attributes.rb +1 -0
- data/app/models/effective/effective_datatable/compute.rb +6 -5
- data/app/models/effective/effective_datatable/cookie.rb +2 -0
- data/app/models/effective/effective_datatable/dsl/filters.rb +1 -1
- data/app/models/effective/effective_datatable/format.rb +9 -9
- data/app/models/effective/effective_datatable/params.rb +5 -0
- data/app/models/effective/effective_datatable/resource.rb +16 -9
- data/app/models/effective/effective_datatable/state.rb +1 -1
- data/app/views/effective/datatables/_active_storage_column.html.haml +4 -0
- data/app/views/effective/datatables/_bulk_actions_dropdown.html.haml +3 -2
- data/config/effective_datatables.rb +5 -1
- data/config/locales/en.yml +3 -1
- data/config/locales/es.yml +3 -1
- data/config/locales/nl.yml +3 -1
- data/lib/effective_datatables/version.rb +1 -1
- data/lib/effective_datatables.rb +4 -0
- metadata +7 -9
- data/app/datatables/effective_style_guide_datatable.rb +0 -47
- data/app/models/effective/access_denied.rb +0 -17
- data/app/views/effective/style_guide/_effective_datatables.html.haml +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb1dc8f3ac21b885c37e56552192568b059350efcdcd7bd79784d87d91a4bee3
|
4
|
+
data.tar.gz: 46094ee0a00e9246b8dde73c2c9ab4f531c84a287f2cd7db7da467cc0dab6d92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51f91cf414121df572748ce03da5732b53011ad113db667b95284276f9ec628ec12ff0f8f614567ab88c770f9fa674059c6b8ca9a1ab1a5701e4dd8dbd509c2f
|
7
|
+
data.tar.gz: 1fbacd63b25ba9ba0556ccfec605a07ff5a3c6e5ddfc8da6dc723e66bfc4a72cb0d9c97d796a4acdb419e7fa5d1a2c8b8ec760bb4df77dbcee9b79708fce235d
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -8,7 +8,7 @@ Does the right thing with searching sql columns as well as computed values from
|
|
8
8
|
|
9
9
|
Displays links to associated edit/show/destroy actions based on `current_user` authorized actions.
|
10
10
|
|
11
|
-
Other features include aggregate (total/average) footer rows, bulk actions, show/hide columns, responsive collapsing columns and
|
11
|
+
Other features include aggregate (total/average) footer rows, bulk actions, show/hide columns, responsive collapsing columns, google charts, and inline crud.
|
12
12
|
|
13
13
|
This gem includes the jQuery DataTables assets.
|
14
14
|
|
@@ -54,7 +54,8 @@ Please check out [Effective Datatables 3.x](https://github.com/code-and-effect/e
|
|
54
54
|
* [bulk_action](#bulk_action_divider)
|
55
55
|
* [bulk_download](#bulk_download)
|
56
56
|
* [bulk_action_content](#bulk_action_content)
|
57
|
-
* [
|
57
|
+
* [Charts](#charts)
|
58
|
+
* [Inline](#inline)
|
58
59
|
* [Extras](#extras)
|
59
60
|
* [Advanced Search and Sort](#advanced-search-and-sort)
|
60
61
|
* [Addtional Functionality](#additional-functionality)
|
@@ -453,7 +454,7 @@ The `datatable do ... end` block configures a table of data.
|
|
453
454
|
|
454
455
|
Initialize the datatable in your controller or view, `@datatable = PostsDatatable.new(self)`, and render it in your view `<%= render_datatable(@datatable) %>`
|
455
456
|
|
456
|
-
|
457
|
+
## col
|
457
458
|
|
458
459
|
This is the main DSL method that you will interact with.
|
459
460
|
|
@@ -526,7 +527,7 @@ You can also use the joined syntax, `col 'user.email'` to create a column for ju
|
|
526
527
|
|
527
528
|
This feature is only working with `belongs_to` and you need to add the `.joins(:user)` to the collection do ... end block yourself.
|
528
529
|
|
529
|
-
|
530
|
+
## val
|
530
531
|
|
531
532
|
Shorthand for value, this command also creates a column on the datatable.
|
532
533
|
|
@@ -548,7 +549,7 @@ This is implemented as a full Array search/sort and is much slower for large dat
|
|
548
549
|
|
549
550
|
The `.format do ... end` block can then be used to apply custom formatting.
|
550
551
|
|
551
|
-
|
552
|
+
## bulk_actions_col
|
552
553
|
|
553
554
|
Creates a column of checkboxes for use with the `bulk_actions` section.
|
554
555
|
|
@@ -558,7 +559,7 @@ Use these checkboxes to select all / none / one or more rows for the `bulk_actio
|
|
558
559
|
|
559
560
|
You can only have one `bulk_actions_col` per datatable.
|
560
561
|
|
561
|
-
|
562
|
+
## actions_col
|
562
563
|
|
563
564
|
When working with an ActiveRecord based collection, this column will consider the `current_user`'s authorization, and generate links to edit, show and destroy actions for any collection class.
|
564
565
|
|
@@ -596,7 +597,7 @@ Any `data-remote` actions will be hijacked and performed as inline ajax by datat
|
|
596
597
|
|
597
598
|
If you'd like to opt-out of this behavior, use `actions_col(inline: false)` or add `data-inline: false` to your action link.
|
598
599
|
|
599
|
-
|
600
|
+
## length
|
600
601
|
|
601
602
|
Sets the default number of rows per page. Valid lengths are `5`, `10`, `25`, `50`, `100`, `250`, `500`, `:all`
|
602
603
|
|
@@ -606,7 +607,7 @@ When not specified, effective_datatables uses the default as per the `config/ini
|
|
606
607
|
length 100
|
607
608
|
```
|
608
609
|
|
609
|
-
|
610
|
+
## order
|
610
611
|
|
611
612
|
Sets the default order of table rows. The first argument is the column, the second the direction.
|
612
613
|
|
@@ -618,7 +619,7 @@ When not specified, effective_datatables will sort by the first defined column.
|
|
618
619
|
order :created_at, :asc|:desc
|
619
620
|
```
|
620
621
|
|
621
|
-
|
622
|
+
## reorder
|
622
623
|
|
623
624
|
Enables drag-and-drop row re-ordering.
|
624
625
|
|
@@ -636,7 +637,7 @@ reorder :position
|
|
636
637
|
|
637
638
|
Using `reorder` will sort the collection by this field and disable all other column sorting.
|
638
639
|
|
639
|
-
|
640
|
+
## aggregate
|
640
641
|
|
641
642
|
The `aggregate` command inserts a row in the table's `tfoot`.
|
642
643
|
|
@@ -681,7 +682,7 @@ The form is submitted by an AJAX POST action, or, in some advanced circumstances
|
|
681
682
|
|
682
683
|
Initialize the datatable in your controller or view, `@datatable = PostsDatatable.new(self)`, and render its filters anywhere with `<%= render_datatable_filters(@datatable) %>`.
|
683
684
|
|
684
|
-
|
685
|
+
## scope
|
685
686
|
|
686
687
|
All defined scopes are rendered as a single radio button form field. Works great with the [effective_form_inputs](https://github.com/code-and-effect/effective_form_inputs) gem.
|
687
688
|
|
@@ -705,7 +706,7 @@ class Post < ApplicationRecord | ActiveRecord::Base
|
|
705
706
|
end
|
706
707
|
```
|
707
708
|
|
708
|
-
|
709
|
+
## filter
|
709
710
|
|
710
711
|
Each filter has a name and a default/fallback value. If the form is submitted blank, the default values are used.
|
711
712
|
|
@@ -758,7 +759,7 @@ Creates a single dropdown menu with a link to each action, download or content.
|
|
758
759
|
|
759
760
|
Along with this section, you must put a `bulk_actions_col` somewhere in your `datatable do ... end` section.
|
760
761
|
|
761
|
-
|
762
|
+
## bulk_action
|
762
763
|
|
763
764
|
Creates a link that becomes clickable when one or more checkbox/rows are selected as per the `bulk_actions_col` column.
|
764
765
|
|
@@ -814,11 +815,11 @@ def approve!
|
|
814
815
|
end
|
815
816
|
```
|
816
817
|
|
817
|
-
|
818
|
+
## bulk_action_divider
|
818
819
|
|
819
820
|
Inserts a menu divider `<li class='divider' role='separator'></li>`
|
820
821
|
|
821
|
-
|
822
|
+
## bulk_download
|
822
823
|
|
823
824
|
So it turns out there are some http issues with using an AJAX action to download a file.
|
824
825
|
|
@@ -859,7 +860,7 @@ def bulk_export_report
|
|
859
860
|
end
|
860
861
|
```
|
861
862
|
|
862
|
-
|
863
|
+
## bulk_action_content
|
863
864
|
|
864
865
|
Blindly inserts content into the dropdown.
|
865
866
|
|
@@ -873,12 +874,16 @@ end
|
|
873
874
|
|
874
875
|
Don't actually use this.
|
875
876
|
|
876
|
-
|
877
|
+
# Charts
|
877
878
|
|
878
879
|
Create a [Google Chart](https://developers.google.com/chart/interactive/docs/quick_start) based on your searched collection, filters and attributes.
|
879
880
|
|
880
881
|
No javascript required. Just use the `chart do ... end` block and return an Array of Arrays.
|
881
882
|
|
883
|
+
The first collection, `collection` is the raw results as returned from the `collection do` block.
|
884
|
+
|
885
|
+
The second collection, `searched_collection` is the results after the table's search columns have been applied, but irregardless of pagination.
|
886
|
+
|
882
887
|
```ruby
|
883
888
|
charts do
|
884
889
|
chart :breakfast, 'BarChart' do |collection|
|
@@ -894,6 +899,18 @@ charts do
|
|
894
899
|
[date.strftime('%F'), posts.length]
|
895
900
|
end
|
896
901
|
end
|
902
|
+
|
903
|
+
chart :posts_per_user, 'ColumnChart' do |collection, searched_collection|
|
904
|
+
measured_posts = if search.present?
|
905
|
+
["Posts with #{search.map { |k, v| k.to_s + ' ' + v.to_s }.join(',')}", searched_collection.length]
|
906
|
+
else
|
907
|
+
['All Posts', collection.length]
|
908
|
+
end
|
909
|
+
|
910
|
+
[['Posts', 'Count'], measured_posts] +
|
911
|
+
searched_collection.group_by(&:user).map { |user, posts| [user.last_name, posts.length] }
|
912
|
+
end
|
913
|
+
|
897
914
|
end
|
898
915
|
```
|
899
916
|
|
@@ -914,11 +931,169 @@ All options passed to `chart` are used to initialize the chart javascript.
|
|
914
931
|
|
915
932
|
By default, the only package that is loaded is `corechart`, see the `config/initializers/effective_datatables.rb` file to add more packages.
|
916
933
|
|
917
|
-
|
934
|
+
# Inline
|
935
|
+
|
936
|
+
Any datatable can be used as an inline datatable, to create, update and destroy resources without leaving the current page.
|
937
|
+
|
938
|
+
If your datatable is already working with `actions_col` and being rendered from an `Effective::CrudController` controller, all you need to do is change your view from `render_datatable(@datatable)` to `render_datatable(@datatable, inline: true)`.
|
939
|
+
|
940
|
+
Click here for a [Inline Live Demo](https://effective-datatables-demo.herokuapp.com/things) and here for an [Inline Code Example](https://github.com/code-and-effect/effective_datatables_demo)
|
941
|
+
(only the `thing` data model and `things_datatable` are being used inline)
|
942
|
+
|
943
|
+
To use effective_datatables as an inline CRUD builder, you will be relying heavily on [effective_resources](https://github.com/code-and-effect/effective_resources) which is a dependency of this gem. I would also recommend you install [effective_developer](https://github.com/code-and-effect/effective_developer) to get access to some scaffolds and generators. It's not required but I'm gonna use them in this example.
|
944
|
+
|
945
|
+
Here is how I build rails models for inline datatable CRUD operations:
|
946
|
+
|
947
|
+
1. Create a new model file `app/models/thing.rb`:
|
948
|
+
|
949
|
+
```ruby
|
950
|
+
class Thing < ApplicationRecord
|
951
|
+
belongs_to :user
|
952
|
+
|
953
|
+
effective_resource do
|
954
|
+
title :string
|
955
|
+
description :text
|
956
|
+
timestamps
|
957
|
+
end
|
958
|
+
|
959
|
+
scope :deep, -> { includes(:user) }
|
960
|
+
scope :sorted, -> { order(:title) }
|
961
|
+
|
962
|
+
def to_s
|
963
|
+
title
|
964
|
+
end
|
965
|
+
end
|
966
|
+
```
|
967
|
+
|
968
|
+
The `effective_resource do` block comes from the [effective_resources](https://github.com/code-and-effect/effective_resources) gem and is used to build any permitted_params.
|
969
|
+
|
970
|
+
2. Generate a migration. Run `rails generate effective:migration things` to create a migration based off the model file then `rails db:migrate`.
|
971
|
+
|
972
|
+
3. Scaffold the rest. Run `rails generate effective:scaffold_controller things` which will create:
|
973
|
+
|
974
|
+
- A controller `app/controllers/things_controller.rb`:
|
975
|
+
|
976
|
+
```ruby
|
977
|
+
class ThingsController < ApplicationController
|
978
|
+
include Effective::CrudController
|
979
|
+
end
|
980
|
+
```
|
981
|
+
|
982
|
+
The Effective::CrudController comes from [effective_resources](https://github.com/code-and-effect/effective_resources) gem and handles the standard 7 CRUD actions and member and collection actions. It is opinionated code that follows rails conventions. It considers the `routes.rb` and `ability.rb` or other authorization, to find all available actions.
|
983
|
+
|
984
|
+
- A datatable `app/datatables/things_datatable.rb`:
|
985
|
+
|
986
|
+
```ruby
|
987
|
+
class ThingsDatatable < Effective::Datatable
|
988
|
+
datatable do
|
989
|
+
col :title
|
990
|
+
col :description
|
991
|
+
actions_col
|
992
|
+
end
|
993
|
+
|
994
|
+
collection do
|
995
|
+
Thing.deep.all
|
996
|
+
end
|
997
|
+
end
|
998
|
+
```
|
999
|
+
|
1000
|
+
This is an ordinary datatable. As long as it's an ActiveRecord collection, inline crud will work.
|
1001
|
+
|
1002
|
+
- A view partial `app/views/things/_thing.html.haml`:
|
1003
|
+
|
1004
|
+
```ruby
|
1005
|
+
%table.table
|
1006
|
+
%tbody
|
1007
|
+
%tr
|
1008
|
+
%th Title
|
1009
|
+
%td= thing.title
|
1010
|
+
%tr
|
1011
|
+
%th Description
|
1012
|
+
%td= thing.description
|
1013
|
+
```
|
1014
|
+
|
1015
|
+
This file is what rails uses when you call `render(thing)` and what datatables uses for the inline `show` action. It's important that its called `_thing.html`.
|
1016
|
+
|
1017
|
+
- A form partial `app/views/things/_form.html.haml`:
|
1018
|
+
|
1019
|
+
```ruby
|
1020
|
+
= effective_form_with(model: thing) do |f|
|
1021
|
+
= f.text_field :title
|
1022
|
+
= f.text_area :description
|
1023
|
+
= f.submit
|
1024
|
+
```
|
1025
|
+
|
1026
|
+
The `effective_form_with` comes from [effective_bootstrap](https://github.com/code-and-effect/effective_bootstrap) gem and is a drop-in replacement for the newer `form_with` syntax. It's really good, you should use it, but an ordinary `form_with` will work here just fine.
|
1027
|
+
|
1028
|
+
This `_form.html` is an effective gems convention. This file must exist for your resource.
|
1029
|
+
|
1030
|
+
- A resources entry in `config/routes.rb`:
|
1031
|
+
|
1032
|
+
```ruby
|
1033
|
+
Rails.application.routes.draw do
|
1034
|
+
resources :things do
|
1035
|
+
post :approve, on: :member
|
1036
|
+
post :reject, on: :member
|
1037
|
+
end
|
1038
|
+
end
|
1039
|
+
```
|
1040
|
+
|
1041
|
+
Above we have `resources :things` for the 7 crud actions. And we add two more member actions, which datatables will call `approve!` or `reject!` on thing.
|
1042
|
+
|
1043
|
+
|
1044
|
+
4. Render in the view. Create an `app/views/things/index.html.haml` and call `render_datatable(@datatable, inline: true)` or `render_inline_datatable(@datatable).
|
1045
|
+
|
1046
|
+
```ruby
|
1047
|
+
= render_datatable(@datatable, inline: true)
|
1048
|
+
```
|
1049
|
+
|
1050
|
+
Your datatable should now have New, Show, Edit, Approve and Reject buttons. Click them for inline functionality.
|
1051
|
+
|
1052
|
+
## Troubleshooting Inline
|
1053
|
+
|
1054
|
+
If things aren't working, try the following:
|
1055
|
+
|
1056
|
+
- Double check your javascripts:
|
1057
|
+
|
1058
|
+
```ruby
|
1059
|
+
//= require jquery3
|
1060
|
+
//= require popper
|
1061
|
+
//= require bootstrap
|
1062
|
+
//= require effective_bootstrap
|
1063
|
+
//= require effective_datatables
|
1064
|
+
//= require jquery_ujs
|
1065
|
+
```
|
1066
|
+
|
1067
|
+
The inline functionality requires one of sprockets jquery_ujs, sprockets rails_ujs or webpack @rails/ujs libraries.
|
1068
|
+
|
1069
|
+
- Double check your stylesheets:
|
1070
|
+
|
1071
|
+
```ruby
|
1072
|
+
@import 'bootstrap';
|
1073
|
+
@import 'effective_bootstrap';
|
1074
|
+
@import 'effective_datatables';
|
1075
|
+
```
|
1076
|
+
|
1077
|
+
- Make sure your datatable is not being rendered inside a `<form>...</form>` tag. It will display a javascript console error and won't work.
|
1078
|
+
|
1079
|
+
- Double check your `resources :things` are in `routes.rb` in the same namespace as the controller, and that you have authorization for those actions in `ability.rb` or whatever your `config/initializers/effective_datatables.rb` `config.authorization_method` returns.
|
1080
|
+
|
1081
|
+
## A note on how it works
|
1082
|
+
|
1083
|
+
We use good old `rails_ujs` for all inline actions.
|
1084
|
+
|
1085
|
+
When inline, any of the actions_col actions, as well as the New button, will be changed into `data-remote: true` actions.
|
1086
|
+
|
1087
|
+
The [inline_crud javascript](https://github.com/code-and-effect/effective_datatables/blob/master/app/assets/javascripts/effective_datatables/inline_crud.js.coffee) handles fetching the form, or view partial and expanding/collapsing the appropriate row of the datatable.
|
1088
|
+
|
1089
|
+
When an inline action is clicked, effective_datatables will make an AJAX request to the server, which could be received by an `Effective::CrudController` that will handle the `.js` format, and respond_with the appropriate [rails_ujs .js.erb views](https://github.com/code-and-effect/effective_resources/tree/master/app/views/application).
|
1090
|
+
|
1091
|
+
|
1092
|
+
# Extras
|
918
1093
|
|
919
1094
|
The following commands don't quite fit into the DSL, but are present nonetheless.
|
920
1095
|
|
921
|
-
|
1096
|
+
## simple
|
922
1097
|
|
923
1098
|
To render a simple table, without pagination, sorting, filtering, export buttons, per page, and default visibility:
|
924
1099
|
|
@@ -926,7 +1101,7 @@ To render a simple table, without pagination, sorting, filtering, export buttons
|
|
926
1101
|
<%= render_datatable(@datatable, simple: true) %>
|
927
1102
|
```
|
928
1103
|
|
929
|
-
|
1104
|
+
## index
|
930
1105
|
|
931
1106
|
If you just want to render a datatable and nothing else, there is a quick way to skip creating a view:
|
932
1107
|
|
@@ -940,13 +1115,13 @@ end
|
|
940
1115
|
|
941
1116
|
will render `views/effective/datatables/index` with the assigned datatable.
|
942
1117
|
|
943
|
-
|
1118
|
+
# Advanced Search and Sort
|
944
1119
|
|
945
1120
|
The built-in search and ordering can be overridden on a per-column basis.
|
946
1121
|
|
947
1122
|
The only gotcha here is that you must be aware of the type of collection.
|
948
1123
|
|
949
|
-
|
1124
|
+
## With ActiveRecord collection
|
950
1125
|
|
951
1126
|
In the case of a `col` and an ActiveRecord collection:
|
952
1127
|
|
@@ -978,7 +1153,7 @@ If `column[:sql_column].blank?` then this `col` has fallen back to being a `val`
|
|
978
1153
|
|
979
1154
|
Try adding `col :post_category, sql_column: 'post_categories.title'`
|
980
1155
|
|
981
|
-
|
1156
|
+
## With Array collection
|
982
1157
|
|
983
1158
|
And in the case of a `col` with an Array collection, or any `val`:
|
984
1159
|
|
@@ -1017,7 +1192,7 @@ end
|
|
1017
1192
|
|
1018
1193
|
The search and sort for each column will be merged together to form the final results.
|
1019
1194
|
|
1020
|
-
|
1195
|
+
## Default search collection
|
1021
1196
|
|
1022
1197
|
When using a `col :comments` type belongs_to or has_many column, a search collection for that class will be loaded.
|
1023
1198
|
|
@@ -1139,6 +1314,22 @@ def finalize(collection)
|
|
1139
1314
|
end
|
1140
1315
|
```
|
1141
1316
|
|
1317
|
+
## Render outside of view
|
1318
|
+
|
1319
|
+
You can render a datatable outside the view.
|
1320
|
+
|
1321
|
+
Anything you pass to the `rendered` method is treated as view/request params.
|
1322
|
+
|
1323
|
+
You can test filters and scopes by passing them here.
|
1324
|
+
|
1325
|
+
```
|
1326
|
+
post = Post.create!
|
1327
|
+
datatable = PostsDatatable.new.rendered(end_date: Time.zone.now+2.days, current_user_id: 1)
|
1328
|
+
|
1329
|
+
assert_equal 1, datatable.collection.count
|
1330
|
+
assert_equal [post], datatable.collection
|
1331
|
+
```
|
1332
|
+
|
1142
1333
|
## Authorization
|
1143
1334
|
|
1144
1335
|
All authorization checks are handled via the config.authorization_method found in the `config/initializers/effective_datatables.rb` file.
|
@@ -16,7 +16,7 @@ flash = (message, status = '') ->
|
|
16
16
|
timeout = $processing.data('timeout')
|
17
17
|
clearTimeout(timeout) if timeout
|
18
18
|
|
19
|
-
delay = (if status == 'danger' then 4000 else
|
19
|
+
delay = (if status == 'danger' then 4000 else 1000)
|
20
20
|
|
21
21
|
$processing.html(message).data('timeout', setTimeout( =>
|
22
22
|
$processing.html('Processing...')
|
@@ -5,6 +5,9 @@ initializeDataTables = (target) ->
|
|
5
5
|
buttons_export_columns = options['buttons_export_columns'] || ':not(.col-actions)'
|
6
6
|
reorder = datatable.data('reorder')
|
7
7
|
|
8
|
+
if datatable.data('inline') && datatable.closest('form').length > 0
|
9
|
+
console.error('inline datatable cannot work inside a form')
|
10
|
+
|
8
11
|
if options['buttons'] == false
|
9
12
|
options['buttons'] = []
|
10
13
|
|
@@ -49,7 +52,7 @@ initializeDataTables = (target) ->
|
|
49
52
|
displayStart: datatable.data('display-start')
|
50
53
|
iDisplayLength: datatable.data('display-length')
|
51
54
|
language: datatable.data('language')
|
52
|
-
lengthMenu: [[5, 10, 25, 50, 100, 250, 500, 9999999], ['5', '10', '25', '50', '100', '250', '500', '
|
55
|
+
lengthMenu: [[5, 10, 25, 50, 100, 250, 500, 9999999], ['5', '10', '25', '50', '100', '250', '500', datatable.data('all-label')]]
|
53
56
|
order: datatable.data('display-order')
|
54
57
|
processing: true
|
55
58
|
responsive: true
|
@@ -77,7 +80,7 @@ initializeDataTables = (target) ->
|
|
77
80
|
filter_name = name.replace('filters[', '').substring(0, name.length-9)
|
78
81
|
|
79
82
|
params['filter'][filter_name] = $form.find("input[name='#{name}']:checked").val()
|
80
|
-
|
83
|
+
|
81
84
|
else if $input.attr('id')
|
82
85
|
filter_name = $input.attr('id').replace('filters_', '')
|
83
86
|
params['filter'][filter_name] = $input.val()
|
@@ -131,7 +134,7 @@ initializeDataTables = (target) ->
|
|
131
134
|
if typeof(google) != 'undefined' && typeof(google.visualization) != 'undefined'
|
132
135
|
$.each charts, (name, data) =>
|
133
136
|
$(".effective-datatables-chart[data-name='#{name}']").each (_, obj) =>
|
134
|
-
chart = new google.visualization[data['
|
137
|
+
chart = new google.visualization[data['as']](obj)
|
135
138
|
chart.draw(google.visualization.arrayToDataTable(data['data']), data['options'])
|
136
139
|
|
137
140
|
# Appends the search html, stored in the column definitions, into each column header
|
@@ -162,14 +165,23 @@ initializeDataTables = (target) ->
|
|
162
165
|
$input.on 'change', (event) -> dataTableSearch($(event.currentTarget))
|
163
166
|
else if $input.is('input')
|
164
167
|
$input.delayedChange ($input) -> dataTableSearch($input)
|
165
|
-
$input.on('paste', -> dataTableSearch($input))
|
166
168
|
|
167
169
|
# Do the actual search
|
168
170
|
dataTableSearch = ($input) -> # This is the function called by a select or input to run the search
|
169
171
|
return if $input.is(':invalid')
|
170
172
|
|
171
173
|
table = $input.closest('table.dataTable')
|
172
|
-
|
174
|
+
|
175
|
+
value = $input.val()
|
176
|
+
|
177
|
+
if Array.isArray(value)
|
178
|
+
# Nothing
|
179
|
+
else if value.startsWith('"') && value.endsWith('"')
|
180
|
+
value = value.substring(1, value.length-1)
|
181
|
+
else
|
182
|
+
value = $.trim(value)
|
183
|
+
|
184
|
+
table.DataTable().column("#{$input.data('column-name')}:name").search(value).draw()
|
173
185
|
|
174
186
|
if reorder
|
175
187
|
init_options['rowReorder'] = { selector: 'td.col-_reorder', snapX: true, dataSrc: datatable.data('reorder-index') }
|
@@ -188,6 +200,7 @@ initializeDataTables = (target) ->
|
|
188
200
|
|
189
201
|
table.addClass('initialized')
|
190
202
|
table.children('thead').trigger('effective-bootstrap:initialize')
|
203
|
+
table.children('thead').find('input[autofocus]').first().focus()
|
191
204
|
true
|
192
205
|
|
193
206
|
destroyDataTables = ->
|
@@ -1,15 +1,34 @@
|
|
1
1
|
# To achieve inline crud, we use rails' data-remote links, and override their behaviour when inside a datatable
|
2
2
|
# This works with EffectiveForm.remote_form which is part of the effective_bootstrap gem.
|
3
3
|
|
4
|
-
#
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
# https://github.com/rails/jquery-ujs/wiki/ajax
|
5
|
+
# https://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html#rails-ujs-event-handlers
|
6
|
+
|
7
|
+
$(document).on 'ajax:before', '.dataTables_wrapper .col-actions', (event) ->
|
8
|
+
$action = $(event.target)
|
9
|
+
$table = $(event.target).closest('table')
|
8
10
|
|
9
11
|
return true if ('' + $action.data('inline')) == 'false'
|
10
12
|
|
11
|
-
$params = $.param(
|
12
|
-
|
13
|
+
$params = $.param(
|
14
|
+
{
|
15
|
+
_datatable_id: $table.attr('id'),
|
16
|
+
_datatable_attributes: $table.data('attributes'),
|
17
|
+
_datatable_action: true
|
18
|
+
}
|
19
|
+
)
|
20
|
+
|
21
|
+
$action.attr('data-params', $params)
|
22
|
+
true
|
23
|
+
|
24
|
+
# We click the New/Edit/Action button from the col-actions
|
25
|
+
$(document).on 'ajax:beforeSend', '.dataTables_wrapper .col-actions', (event, xhr, settings) ->
|
26
|
+
[xhr, settings] = event.detail if event.detail # rails/ujs
|
27
|
+
|
28
|
+
$action = $(event.target)
|
29
|
+
$table = $(event.target).closest('table')
|
30
|
+
|
31
|
+
return true if ('' + $action.data('inline')) == 'false'
|
13
32
|
|
14
33
|
if $action.closest('.effective-datatables-inline-row,table.dataTable').hasClass('effective-datatables-inline-row')
|
15
34
|
# Nothing.
|
@@ -22,6 +41,8 @@ $(document).on 'ajax:beforeSend', '.dataTables_wrapper .col-actions', (e, xhr, s
|
|
22
41
|
|
23
42
|
# We have either completed the resource action, or fetched the inline form to load.
|
24
43
|
$(document).on 'ajax:success', '.dataTables_wrapper .col-actions', (event, data) ->
|
44
|
+
[data, status, xhr] = event.detail if event.detail # rails/ujs
|
45
|
+
|
25
46
|
$action = $(event.target)
|
26
47
|
|
27
48
|
return true if ('' + $action.data('inline')) == 'false'
|
@@ -54,12 +75,22 @@ $(document).on 'ajax:error', '.dataTables_wrapper', (event) ->
|
|
54
75
|
EffectiveForm.remote_form_flash = ''
|
55
76
|
true
|
56
77
|
|
57
|
-
|
58
|
-
|
59
|
-
|
78
|
+
## Now for the fetched form. We add the datatables params attributes
|
79
|
+
|
80
|
+
$(document).on 'ajax:before', '.dataTables_wrapper .col-inline-form', (event) ->
|
81
|
+
$action = $(event.target)
|
82
|
+
$form = $action.closest('form')
|
83
|
+
$table = $action.closest('table')
|
84
|
+
|
85
|
+
if $form.find('input[name=_datatable_id]').length == 0
|
86
|
+
$('<input>').attr(
|
87
|
+
{type: 'hidden', name: '_datatable_id', value: $table.attr('id')}
|
88
|
+
).appendTo($form)
|
60
89
|
|
61
|
-
|
62
|
-
|
90
|
+
if $form.find('input[name=_datatable_attributes]').length == 0
|
91
|
+
$('<input>').attr(
|
92
|
+
{type: 'hidden', name: '_datatable_attributes', value: $table.data('attributes')}
|
93
|
+
).appendTo($form)
|
63
94
|
|
64
95
|
true
|
65
96
|
|
@@ -92,7 +123,6 @@ beforeNew = ($action) ->
|
|
92
123
|
|
93
124
|
# Append spinner and show Processing
|
94
125
|
$th.append($table.data('spinner'))
|
95
|
-
$table.DataTable().flash()
|
96
126
|
$table.one 'draw.dt', (event) ->
|
97
127
|
$th.find('a').show().siblings('svg').remove() if event.target == event.currentTarget
|
98
128
|
|
@@ -121,7 +151,6 @@ beforeEdit = ($action) ->
|
|
121
151
|
|
122
152
|
# Append spinner and show Processing
|
123
153
|
$td.append($table.data('spinner'))
|
124
|
-
$table.DataTable().flash()
|
125
154
|
|
126
155
|
afterEdit = ($action) ->
|
127
156
|
$tr = $action.closest('tr')
|
@@ -8,7 +8,14 @@ reorder = (event, diff, edit) ->
|
|
8
8
|
return unless oldNode? && newNode?
|
9
9
|
|
10
10
|
url = @context[0].ajax.url.replace('.json', '/reorder.json')
|
11
|
-
|
11
|
+
|
12
|
+
data = {
|
13
|
+
'authenticity_token': $('head').find("meta[name='csrf-token']").attr('content'),
|
14
|
+
'reorder[id]': oldNode.data('reorder-resource'),
|
15
|
+
'reorder[old]': oldNode.val(),
|
16
|
+
'reorder[new]': newNode.val(),
|
17
|
+
'attributes': $table.data('attributes')
|
18
|
+
}
|
12
19
|
|
13
20
|
@context[0].rowreorder.c.enable = false
|
14
21
|
|
@@ -40,4 +47,3 @@ $(document).on 'click', '.dataTables_wrapper a.buttons-reorder', (event) ->
|
|
40
47
|
$table.addClass('reordering')
|
41
48
|
|
42
49
|
column.visible(!column.visible())
|
43
|
-
|
@@ -1,11 +1,32 @@
|
|
1
1
|
$(document).on 'click', '.dataTables_wrapper a.buttons-reset-search', (event) ->
|
2
2
|
event.preventDefault() # prevent the click
|
3
3
|
|
4
|
+
# Reset the HTML
|
4
5
|
$table = $(event.currentTarget).closest('.dataTables_wrapper').find('table.dataTable').first()
|
5
6
|
$thead = $table.children('thead').first()
|
6
7
|
|
7
|
-
|
8
|
+
# Reset all inputs
|
8
9
|
$thead.find('select').val('').trigger('change.select2')
|
9
10
|
|
10
|
-
$
|
11
|
+
$inputs = $thead.find('input')
|
12
|
+
$inputs.val('').removeAttr('checked').removeAttr('selected')
|
13
|
+
|
14
|
+
# Reset delayedChange
|
15
|
+
$.each $inputs, (input) =>
|
16
|
+
$input = $(input)
|
17
|
+
if ($input.delayedChange.oldVal)
|
18
|
+
$input.delayedChange.oldVal = undefined
|
19
|
+
|
20
|
+
# Reset the datatable
|
21
|
+
datatable = $table.DataTable()
|
22
|
+
|
23
|
+
# Reset search
|
24
|
+
datatable.search('').columns().search('')
|
25
|
+
|
26
|
+
# Reset to default visibility
|
27
|
+
$.each $table.data('default-visibility'), (index, visible) =>
|
28
|
+
datatable.column(index).visible(visible, false)
|
29
|
+
|
30
|
+
# Don't pass up the click
|
31
|
+
false
|
11
32
|
|
@@ -14,11 +14,10 @@
|
|
14
14
|
|
15
15
|
return this.each(function() {
|
16
16
|
var element = $(this);
|
17
|
-
element.keyup
|
17
|
+
element.on('keyup paste', function() {
|
18
18
|
clearTimeout(timer);
|
19
19
|
timer = setTimeout(function() {
|
20
20
|
var newVal = element.val();
|
21
|
-
newVal = $.trim(newVal);
|
22
21
|
if (element.delayedChange.oldVal != newVal) {
|
23
22
|
element.delayedChange.oldVal = newVal;
|
24
23
|
o.onChange.call(this, element);
|