Skip to content

Commit fb5a1a4

Browse files
Merge pull request #8 from SourceLabsLLC/disable-button-when-no-selection
Update JS to toggle action buttons based on selections
2 parents eb5aaf3 + b842ac2 commit fb5a1a4

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

app/assets/javascripts/administrate_batch_actions/script.js

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,29 @@ var buttons = document.querySelectorAll("[data-batch-action-option='button']");
22
var checkboxes = document.querySelectorAll("[data-batch-action-option='checkbox']");
33
var selectAllCheckboxes = document.querySelector("[data-batch-action-option='select_all']");
44

5-
if (selectAllCheckboxes) {
5+
if (selectAllCheckboxes && checkboxes && buttons) {
6+
7+
window.onpageshow = function(event) {
8+
if (selectedItemIds()) {
9+
checkboxes.forEach(function(checkbox) {
10+
checkbox.checked = false;
11+
});
12+
13+
selectAllCheckboxes.checked = false;
14+
}
15+
};
16+
617
selectAllCheckboxes.addEventListener('click', function(){
718
checkboxes.forEach(function(checkbox) {
819
checkbox.checked = selectAllCheckboxes.checked;
920
});
21+
22+
checkAndToggleActionButtons();
1023
});
1124

1225
buttons.forEach(function(button){
1326
button.addEventListener('click', function(event){
14-
var ids = Array.prototype.filter.call(checkboxes, function(checkbox) {
15-
if (checkbox.checked) { return checkbox }
16-
}).map(function(checkbox) {
17-
return 'batch_action_ids[]=' + checkbox.value
18-
}).join('&');
19-
20-
button.href += '?' + ids
27+
button.href += '?' + selectedItemIds()
2128
});
2229
});
2330

@@ -28,6 +35,29 @@ if (selectAllCheckboxes) {
2835

2936
checkbox.addEventListener('click', function(event) {
3037
event.stopImmediatePropagation();
38+
39+
checkAndToggleActionButtons();
3140
})
3241
})
3342
}
43+
44+
function selectedItemIds() {
45+
var ids = Array.prototype.filter.call(checkboxes, function(checkbox) {
46+
if (checkbox.checked) { return checkbox }
47+
}).map(function(checkbox) {
48+
return 'batch_action_ids[]=' + checkbox.value
49+
}).join('&');
50+
return ids;
51+
}
52+
53+
function checkAndToggleActionButtons() {
54+
if (selectedItemIds()) {
55+
buttons.forEach(function(button){
56+
button.classList.remove('disabled');
57+
});
58+
} else {
59+
buttons.forEach(function(button){
60+
button.classList.add('disabled');
61+
});
62+
}
63+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<%= link_to name, path, class: html_options[:class], data: { batch_action_option: 'button', confirm: html_options[:confirm] }, method: :post %>
1+
<%= link_to name, path, class: "btn disabled #{html_options[:class]}", data: { batch_action_option: 'button', confirm: html_options[:confirm] }, method: :post %>

0 commit comments

Comments
 (0)