Skip to content

Commit

Permalink
4e81940e4ca7dcc42f9d5e7438a5011aa0016763 Dev: Update docs for key con…
Browse files Browse the repository at this point in the history
…trol being in v3

4308024722b5d1be81ba5194e988a6ff98949ef0 New: Improved support for server-side processing through new additive / subtractive modes

Sync to source repo @4308024722b5d1be81ba5194e988a6ff98949ef0
  • Loading branch information
dtbuild committed Nov 12, 2024
1 parent f9f931a commit e02e710
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 41 deletions.
2 changes: 1 addition & 1 deletion datatables.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
],
"src-repo": "http://github.com/DataTables/Select",
"last-tag": "2.1.0",
"last-sync": "4ec3a6891012f01409e740c68221473f80c78266"
"last-sync": "4308024722b5d1be81ba5194e988a6ff98949ef0"
}
103 changes: 84 additions & 19 deletions js/dataTables.select.js
Original file line number Diff line number Diff line change
Expand Up @@ -580,14 +580,18 @@ function info(api, node) {
return;
}

// If _select_set has any length, then ids are available and should be used
// as the counter. Otherwise use the API to workout how many rows are
// selected.
var rowSetLength = api.settings()[0]._select_set.length;
var ctx = api.settings()[0];
var rowSetLength = ctx._select_set.length;
var rows = rowSetLength ? rowSetLength : api.rows({ selected: true }).count();
var columns = api.columns({ selected: true }).count();
var cells = api.cells({ selected: true }).count();

// If subtractive selection, then we need to take the number of rows and
// subtract those that have been deselected
if (ctx._select_mode === 'subtractive') {
rows = api.page.info().recordsDisplay - rowSetLength;
}

var add = function (el, name, num) {
el.append(
$('<span class="select-item"/>').append(
Expand Down Expand Up @@ -652,7 +656,8 @@ function initCheckboxHeader( dt, headerCheckbox ) {
if (this.checked) {
if (headerCheckbox == 'select-page') {
dt.rows({page: 'current'}).select();
} else {
}
else {
dt.rows({search: 'applied'}).select();
}
}
Expand Down Expand Up @@ -699,26 +704,28 @@ function initCheckboxHeader( dt, headerCheckbox ) {
function keysSet(dt) {
var ctx = dt.settings()[0];
var flag = ctx._select.keys;
var namespace = 'dts-keys-' + ctx.sTableId;

if (flag) {
// Need a tabindex of the `tr` elements to make them focusable by the browser
$(dt.rows({page: 'current'}).nodes()).attr('tabindex', 0);

dt.on('draw.dts-keys', function () {
dt.on('draw.' + namespace, function () {
$(dt.rows({page: 'current'}).nodes()).attr('tabindex', 0);
});

// Listen on document for tab, up and down
$(document).on('keydown.dts-keys', function (e) {
$(document).on('keydown.' + namespace, function (e) {
var key = e.keyCode;
var active = document.activeElement;

// Can't use e.key as it wasn't widely supported until 2017
// 9 Tab
// 13 Return
// 32 Space
// 38 ArrowUp
// 40 ArrowDown
if (! [9, 32, 38, 40].includes(key)) {
if (! [9, 13, 32, 38, 40].includes(key)) {
return;
}

Expand All @@ -744,7 +751,7 @@ function keysSet(dt) {
preventDefault = false;
}
}
else if (key === 32) {
else if (key === 13 || key === 32) {
// Row selection / deselection
var row = dt.row(active);

Expand Down Expand Up @@ -775,6 +782,7 @@ function keysSet(dt) {
}

if (preventDefault) {
e.stopPropagation();
e.preventDefault();
}
});
Expand All @@ -784,8 +792,8 @@ function keysSet(dt) {
$(dt.rows().nodes()).removeAttr('tabindex');

// Nuke events
dt.off('draw.dts-keys');
$(document).off('keydown.dts-keys');
dt.off('draw.' + namespace);
$(document).off('keydown.' + namespace);
}
}

Expand Down Expand Up @@ -889,7 +897,10 @@ function init(ctx) {
var api = new DataTable.Api(ctx);
ctx._select_init = true;

// _select_set contains a list of the ids of all rows that are selected
// When `additive` then `_select_set` contains a list of the row ids that
// are selected. If `subtractive` then all rows are selected, except those
// in `_select_set`, which is a list of ids.
ctx._select_mode = 'additive';
ctx._select_set = [];

// Row callback so that classes can be added to rows and cells if the item
Expand All @@ -907,7 +918,8 @@ function init(ctx) {
// Row
if (
d._select_selected ||
(id !== 'undefined' && ctx._select_set.includes(id))
(ctx._select_mode === 'additive' && ctx._select_set.includes(id)) ||
(ctx._select_mode === 'subtractive' && ! ctx._select_set.includes(id))
) {
d._select_selected = true;

Expand Down Expand Up @@ -1117,7 +1129,16 @@ function _cumulativeEvents(api) {

var ctx = api.settings()[0];

_add(api, ctx._select_set, indexes);
if (ctx._select_mode === 'additive') {
// Add row to the selection list if it isn't already there
_add(api, ctx._select_set, indexes);
}
else {
// Subtractive - if a row is selected it should not in the list
// as in subtractive mode the list gives the rows which are not
// selected
_remove(api, ctx._select_set, indexes);
}
});

api.on('deselect', function (e, dt, type, indexes) {
Expand All @@ -1128,7 +1149,14 @@ function _cumulativeEvents(api) {

var ctx = api.settings()[0];

_remove(api, ctx._select_set, indexes);
if (ctx._select_mode === 'additive') {
// List is of those rows selected, so remove it
_remove(api, ctx._select_set, indexes);
}
else {
// List is of rows which are deselected, so add it!
_add(api, ctx._select_set, indexes);
}
});
}

Expand Down Expand Up @@ -1286,6 +1314,10 @@ apiRegister('select.keys()', function (flag) {
}

return this.iterator('table', function (ctx) {
if (!ctx._select) {
DataTable.select.init(new DataTable.Api(ctx));
}

ctx._select.keys = flag;

keysSet(new DataTable.Api(ctx));
Expand Down Expand Up @@ -1375,12 +1407,45 @@ apiRegister('select.last()', function (set) {
return ctx._select_lastCell;
});

apiRegister('select.cumulative()', function () {
apiRegister('select.cumulative()', function (mode) {
if (mode) {
return this.iterator('table', function (ctx) {
if (ctx._select_mode === mode) {
return;
}

var dt = new DataTable.Api(ctx);

// Convert from the current mode, to the new
if (mode === 'subtractive') {
// For subtractive mode we track the row ids which are not selected
var unselected = dt.rows({selected: false}).ids().toArray();

ctx._select_mode = mode;
ctx._select_set.length = 0;
ctx._select_set.push.apply(ctx._select_set, unselected);
}
else {
// Switching to additive, so selected rows are to be used
var selected = dt.rows({selected: true}).ids().toArray();

ctx._select_mode = mode;
ctx._select_set.length = 0;
ctx._select_set.push.apply(ctx._select_set, selected);
}
}).draw(false);
}

let ctx = this.context[0];

return ctx && ctx._select_set
? ctx._select_set
: [];
if (ctx && ctx._select_set) {
return {
mode: ctx._select_mode,
rows: ctx._select_set
};
}

return null;
});

apiRegisterPlural('rows().select()', 'row().select()', function (select) {
Expand Down
Loading

0 comments on commit e02e710

Please sign in to comment.