3 Drupal
.behaviors
.tableSelect
= {
4 attach: function (context
, settings
) {
5 $('table:has(th.select-all)', context
).once('table-select', Drupal
.tableSelect
);
9 Drupal
.tableSelect = function () {
10 // Do not add a "Select all" checkbox if there are no rows with checkboxes in the table
11 if ($('td input:checkbox', this).size() == 0) {
15 // Keep track of the table, which checkbox is checked and alias the settings.
16 var table
= this, checkboxes
, lastChecked
;
17 var strings
= { 'selectAll': Drupal
.t('Select all rows in this table'), 'selectNone': Drupal
.t('Deselect all rows in this table') };
18 var updateSelectAll = function (state
) {
19 $('th.select-all input:checkbox', table
).each(function () {
20 $(this).attr('title', state
? strings
.selectNone
: strings
.selectAll
);
25 // Find all <th> with class select-all, and insert the check all checkbox.
26 $('th.select-all', table
).prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings
.selectAll
)).click(function (event
) {
27 if ($(event
.target
).is('input:checkbox')) {
28 // Loop through all checkboxes and set their state to the select all checkbox' state.
29 checkboxes
.each(function () {
30 this.checked
= event
.target
.checked
;
31 // Either add or remove the selected class based on the state of the check all checkbox.
32 $(this).parents('tr:first')[ this.checked
? 'addClass' : 'removeClass' ]('selected');
34 // Update the title and the state of the check all box.
35 updateSelectAll(event
.target
.checked
);
39 // For each of the checkboxes within the table that are not disabled.
40 checkboxes
= $('td input:checkbox:enabled', table
).click(function (e
) {
41 // Either add or remove the selected class based on the state of the check all checkbox.
42 $(this).parents('tr:first')[ this.checked
? 'addClass' : 'removeClass' ]('selected');
44 // If this is a shift click, we need to highlight everything in the range.
45 // Also make sure that we are actually checking checkboxes over a range and
46 // that a checkbox has been checked or unchecked before.
47 if (e
.shiftKey
&& lastChecked
&& lastChecked
!= e
.target
) {
48 // We use the checkbox's parent TR to do our range searching.
49 Drupal
.tableSelectRange($(e
.target
).parents('tr')[0], $(lastChecked
).parents('tr')[0], e
.target
.checked
);
52 // If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked.
53 updateSelectAll((checkboxes
.length
== $(checkboxes
).filter(':checked').length
));
55 // Keep track of the last checked checkbox.
56 lastChecked
= e
.target
;
60 Drupal
.tableSelectRange = function (from, to
, state
) {
61 // We determine the looping mode based on the the order of from and to.
62 var mode
= from.rowIndex
> to
.rowIndex
? 'previousSibling' : 'nextSibling';
64 // Traverse through the sibling nodes.
65 for (var i
= from[mode
]; i
; i
= i
[mode
]) {
66 // Make sure that we're only dealing with elements.
67 if (i
.nodeType
!= 1) {
71 // Either add or remove the selected class based on the state of the target checkbox.
72 $(i
)[ state
? 'addClass' : 'removeClass' ]('selected');
73 $('input:checkbox', i
).each(function () {
78 // If we are at the end of the range, stop.
83 // A faster alternative to doing $(i).filter(to).length.
84 else if ($.filter(to
, [i
]).r
.length
) {