ajax_table_rails 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +60 -28
- data/app/assets/javascripts/ajaxtable.js +44 -11
- data/app/controllers/concerns/ajax_table_actions.rb +3 -3
- data/lib/ajax_table_rails/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5d01e5c8e5273ed807ac6ccb4fa53c2e18744e4
|
4
|
+
data.tar.gz: 2aed6fc52f83751cf394a05746a4765fc172a538
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 825b22093e803c527b8ee61e59b41cc0158d756f5ababfd5c00860214a6a6905d82463d32e15d264c808a9c92d7ad6b127d2dfeb7a83d72a9035de6de364fd80
|
7
|
+
data.tar.gz: 2bd8ddcbae62e2bde5b792a262c86274a03eda4b64ae084da05848c408e05827f742d911a366f5421b4210ade3625417a7ff82406fde08f56c8f84e38f883103
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -24,12 +24,14 @@ Add the following to your `app/assets/javascripts/application.js`:
|
|
24
24
|
//= require ajaxtable
|
25
25
|
````
|
26
26
|
|
27
|
-
###
|
27
|
+
### Basic usage
|
28
|
+
|
29
|
+
#### Build your table
|
28
30
|
|
29
31
|
In your view (using Users as an example):
|
30
32
|
|
31
33
|
````
|
32
|
-
<table class="ajax-table" data-source="<%= users_path(format: :json)%>">
|
34
|
+
<table class="ajax-table" data-source="<%= users_path(format: :json)%>" id="users-table">
|
33
35
|
<thead>
|
34
36
|
<tr>
|
35
37
|
<th data-sort-column="name">Name</th>
|
@@ -60,30 +62,7 @@ In this example, the table will automatically initialize, and the Name and Email
|
|
60
62
|
| table class | "ajax-table" required for ajax tables to auto-init. Exclude this if you wish to init manually with custom settings (see below). |
|
61
63
|
| th data-sort-column | Matches database column you'd like to sort against. |
|
62
64
|
|
63
|
-
|
64
|
-
|
65
|
-
AjaxTableRails is built with Bootstrap and FontAwesome in mind, as well as some other defaults that may make you unhappy. You may want to override the classes used for pagination and sorting, as well as some other bits and bops. Here's what a full customization looks like (default values shown):
|
66
|
-
|
67
|
-
````
|
68
|
-
$(function() {
|
69
|
-
ajaxTable.init($('#some-table'), {
|
70
|
-
cssClasses: {
|
71
|
-
count: 'at-count', // "Showing xx records out of xx" span
|
72
|
-
pagination: 'pagination', // Pagination ul, defaults to match Bootstrap
|
73
|
-
sort: 'at-sort', // Sort icon base class
|
74
|
-
sortAsc: 'fa fa-sort-up', // Sort icon ascending indicator, defaults to use FontAwesome
|
75
|
-
sortDesc: 'fa fa-sort-down' // Sort icon descending indicator, defaults to use FontAwesome
|
76
|
-
},
|
77
|
-
text: {
|
78
|
-
count: 'Showing {count} records out of {total_count}', // Pass null to skip rendering of this element
|
79
|
-
nextPage: '»',
|
80
|
-
previousPage: '«'
|
81
|
-
}
|
82
|
-
});
|
83
|
-
});
|
84
|
-
````
|
85
|
-
|
86
|
-
### Build your controller
|
65
|
+
#### Build your controller
|
87
66
|
|
88
67
|
Call `set_ajax_table` in a `before_action` to set your sorting criteria, then setup the query in a JSON response block.
|
89
68
|
|
@@ -112,7 +91,60 @@ end
|
|
112
91
|
| default_column | Your default sort column (if unspecified, defaults to `id`) |
|
113
92
|
| default_direction | Your default sort direction (if unspecified, deaults to `asc`) |
|
114
93
|
|
115
|
-
|
94
|
+
#### Build your JSON
|
95
|
+
|
96
|
+
You need to render two JSON nodes: `rows` for your records, and `pagination` for your pagination details.
|
97
|
+
|
98
|
+
````
|
99
|
+
json.rows(@users) do |user|
|
100
|
+
json.extract!(user, :name, :email)
|
101
|
+
json.action link_to("Edit", edit_user_path(user), class: "btn")
|
102
|
+
end
|
103
|
+
json.pagination do
|
104
|
+
json.per_page User.default_per_page
|
105
|
+
json.count @users.size
|
106
|
+
json.total_count User.count
|
107
|
+
end
|
108
|
+
````
|
109
|
+
|
110
|
+
### Advanced usage
|
111
|
+
|
112
|
+
#### Filtering results
|
113
|
+
|
114
|
+
**Under construction.** Adding the following anywhere on your page will currently work.
|
115
|
+
|
116
|
+
````
|
117
|
+
<form class="ajax-table-search" data-ajax-table-id="users-table">
|
118
|
+
<input class="ajax-table-search-input">
|
119
|
+
<a href="#" class="ajax-table-reset">x</a>
|
120
|
+
<input type="submit" value="Search">
|
121
|
+
</form>
|
122
|
+
````
|
123
|
+
|
124
|
+
#### Customize your table
|
125
|
+
|
126
|
+
AjaxTableRails is built with Bootstrap and FontAwesome in mind, as well as some other defaults that may make you unhappy. You may want to override the classes used for pagination and sorting, as well as some other bits and bops. Here's what a full customization looks like (default values shown):
|
127
|
+
|
128
|
+
````
|
129
|
+
$(function() {
|
130
|
+
ajaxTable.init($('#some-table'), {
|
131
|
+
cssClasses: {
|
132
|
+
count: 'at-count', // "Showing xx records out of xx" span
|
133
|
+
pagination: 'pagination', // Pagination ul, defaults to match Bootstrap
|
134
|
+
sort: 'at-sort', // Sort icon base class
|
135
|
+
sortAsc: 'fa fa-sort-up', // Sort icon ascending indicator, defaults to use FontAwesome
|
136
|
+
sortDesc: 'fa fa-sort-down' // Sort icon descending indicator, defaults to use FontAwesome
|
137
|
+
},
|
138
|
+
text: {
|
139
|
+
count: 'Showing {count} records out of {total_count}', // Pass null to skip rendering of this element
|
140
|
+
nextPage: '»',
|
141
|
+
previousPage: '«'
|
142
|
+
}
|
143
|
+
});
|
144
|
+
});
|
145
|
+
````
|
146
|
+
|
147
|
+
#### Make it shiny
|
116
148
|
|
117
149
|
Use whatever CSS you like. Here's a rudimentary example of some things you may want to do.
|
118
150
|
|
@@ -144,7 +176,7 @@ Copyright © 2014 Yuval Kordov. See MIT-LICENSE for further details.
|
|
144
176
|
|
145
177
|
## TODO
|
146
178
|
|
147
|
-
* Windowed pagination
|
148
179
|
* Result filtering
|
180
|
+
* Windowed pagination
|
149
181
|
* Show default sort
|
150
182
|
* Allow customization via data attributes
|
@@ -1,8 +1,10 @@
|
|
1
1
|
var ajaxTable = (function($) {
|
2
2
|
|
3
|
-
// On document ready
|
3
|
+
// On document ready:
|
4
|
+
// ajaxify all tables with the `ajax-table` class
|
5
|
+
// bind search/reset via forms with the `ajax-table-search` class
|
4
6
|
//
|
5
|
-
// If you want to use custom settings, forego the `ajax-table` class and call init manually:
|
7
|
+
// If you want to use custom settings for your tables, forego the `ajax-table` class and call init manually:
|
6
8
|
// $(function() {
|
7
9
|
// ajaxTable.init($('#some-table'), {
|
8
10
|
// cssClasses: { pagination: 'some-class' },
|
@@ -16,6 +18,19 @@ var ajaxTable = (function($) {
|
|
16
18
|
$('table.ajax-table').each(function() {
|
17
19
|
init($(this));
|
18
20
|
});
|
21
|
+
|
22
|
+
$('body').on('submit', 'form.ajax-table-search', function(e) {
|
23
|
+
var $form = $(this);
|
24
|
+
search($('#'+$form.data('ajax-table-id')), $form.find('input.ajax-table-search-input').val());
|
25
|
+
e.preventDefault();
|
26
|
+
});
|
27
|
+
|
28
|
+
$('body').on('click', '.ajax-table-reset', function(e) {
|
29
|
+
var $form = $(this).closest('form.ajax-table-search');
|
30
|
+
$form.find('input.ajax-table-search-input').val('');
|
31
|
+
resetTable($('#'+$form.data('ajax-table-id')));
|
32
|
+
e.preventDefault();
|
33
|
+
});
|
19
34
|
});
|
20
35
|
|
21
36
|
/* public */
|
@@ -27,6 +42,20 @@ var ajaxTable = (function($) {
|
|
27
42
|
loadTable($table);
|
28
43
|
initSorting($table);
|
29
44
|
};
|
45
|
+
|
46
|
+
// Filter table records against submitted keywords
|
47
|
+
var search = function($table, val) {
|
48
|
+
$table.data('page', 1);
|
49
|
+
$table.data('search', val);
|
50
|
+
loadTable($table);
|
51
|
+
};
|
52
|
+
|
53
|
+
// Reset table to an unpaginated and unfiltered state
|
54
|
+
var resetTable = function($table) {
|
55
|
+
$table.data('page', 1);
|
56
|
+
$table.removeData('search');
|
57
|
+
loadTable($table);
|
58
|
+
};
|
30
59
|
|
31
60
|
/* private */
|
32
61
|
|
@@ -45,6 +74,7 @@ var ajaxTable = (function($) {
|
|
45
74
|
// @note Querystring param keys match up with those used by ajax_table.rb
|
46
75
|
params: {
|
47
76
|
page: 'page',
|
77
|
+
search: 'search',
|
48
78
|
sortColumn: 'sort',
|
49
79
|
sortDirection: 'direction'
|
50
80
|
},
|
@@ -56,12 +86,13 @@ var ajaxTable = (function($) {
|
|
56
86
|
}
|
57
87
|
};
|
58
88
|
|
59
|
-
// Load and render table, based on current sort and
|
89
|
+
// Load and render table, based on current page, sort, and search filter
|
60
90
|
var loadTable = function($table) {
|
61
91
|
params = {};
|
62
|
-
params[config.params.page]
|
63
|
-
params[config.params.sortColumn]
|
92
|
+
params[config.params.page] = $table.data('page');
|
93
|
+
params[config.params.sortColumn] = $table.data('sort-column');
|
64
94
|
params[config.params.sortDirection] = $table.data('sort-direction');
|
95
|
+
params[config.params.search] = $table.data('search');
|
65
96
|
|
66
97
|
var request = $.ajax({
|
67
98
|
url: $table.data('source'),
|
@@ -80,14 +111,14 @@ var ajaxTable = (function($) {
|
|
80
111
|
// @example `<th data-sort-column="email">Email</th>`
|
81
112
|
var initSorting = function($table) {
|
82
113
|
$table.find('th[data-sort-column]').on('click', function() {
|
83
|
-
//
|
114
|
+
// Reset pagination
|
115
|
+
$table.data('page', 1);
|
116
|
+
// Set direction based on prior and just-clicked sort column
|
84
117
|
var sortColumn = $(this).data('sort-column');
|
85
118
|
var direction = ($table.data('sort-column') == sortColumn && $table.data('sort-direction') == 'asc') ? 'desc' : 'asc';
|
86
119
|
$table.data('sort-direction', direction);
|
87
120
|
// Set new sort column
|
88
121
|
$table.data('sort-column', sortColumn);
|
89
|
-
// Always reset page to 1 when re-sorting
|
90
|
-
$table.data('page', 1);
|
91
122
|
// Remove and re-insert sort icon
|
92
123
|
$table.find('th i.' + config.cssClasses.sort).remove();
|
93
124
|
var $i = $('<i/>', { class: config.cssClasses.sort + ' ' + (direction == 'asc' ? config.cssClasses.sortAsc : config.cssClasses.sortDesc) });
|
@@ -121,7 +152,7 @@ var ajaxTable = (function($) {
|
|
121
152
|
$td.append($count);
|
122
153
|
$count.html(config.text.count.replace('{count}', pagination.count).replace('{total_count}', pagination.total_count));
|
123
154
|
}
|
124
|
-
//
|
155
|
+
// Build pagination controls
|
125
156
|
var pageCount = Math.ceil(pagination.total_count / pagination.per_page);
|
126
157
|
var currentPage = $table.data('page');
|
127
158
|
var $ul = $('<ul/>', { class: config.cssClasses.pagination });
|
@@ -140,7 +171,7 @@ var ajaxTable = (function($) {
|
|
140
171
|
var nextPage = currentPage+1;
|
141
172
|
$ul.append('<li><a href="#" data-page=' + nextPage + '>' + config.text.nextPage + '</a></li>');
|
142
173
|
}
|
143
|
-
// Bind
|
174
|
+
// Bind pagination controls
|
144
175
|
$table.find('ul.' + config.cssClasses.pagination.split(' ').join('.') + ' a').on('click', function(e) {
|
145
176
|
$table.data('page', $(this).data('page'));
|
146
177
|
loadTable($table);
|
@@ -150,7 +181,9 @@ var ajaxTable = (function($) {
|
|
150
181
|
};
|
151
182
|
|
152
183
|
return {
|
153
|
-
init: init
|
184
|
+
init: init,
|
185
|
+
search: search,
|
186
|
+
resetTable: resetTable
|
154
187
|
}
|
155
188
|
|
156
189
|
})(jQuery);
|
@@ -3,7 +3,7 @@ module AjaxTableActions
|
|
3
3
|
|
4
4
|
private
|
5
5
|
|
6
|
-
# before_action that sets @page and @order instance variables based off ajax_table params `sort` and `direction`
|
6
|
+
# before_action that sets @search, @page and @order instance variables based off ajax_table params `search`, sort` and `direction`
|
7
7
|
#
|
8
8
|
# @example With sortable columns
|
9
9
|
# before_action -> { set_ajax_table(columns: %w[name email], default_column: "name", default_direction: "asc") }
|
@@ -16,8 +16,6 @@ module AjaxTableActions
|
|
16
16
|
# @option options [String] default_column Default sort column
|
17
17
|
# @option options [String] default_direction Default sort direction
|
18
18
|
def set_ajax_table(options = {})
|
19
|
-
@page = [params[:page].to_i, 1].max
|
20
|
-
|
21
19
|
unless options.empty?
|
22
20
|
column = (options[:columns] && options[:columns].detect {|column| column == params[:sort]}) || options[:default_column]
|
23
21
|
direction = %w[asc desc].include?(params[:direction]) ? params[:direction] : options[:default_direction]
|
@@ -25,6 +23,8 @@ module AjaxTableActions
|
|
25
23
|
column ||= 'id'
|
26
24
|
direction ||= 'asc'
|
27
25
|
@order = "#{column} #{direction}"
|
26
|
+
@page = [params[:page].to_i, 1].max
|
27
|
+
@search = params[:search]
|
28
28
|
end
|
29
29
|
|
30
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ajax_table_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuval Kordov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|