1 var SelectRow =
function(table){
3 this.selecting =
false;
4 this.lastClickedRow =
false;
6 this.selectedRows = [];
7 this.headerCheckboxElement = null;
10 SelectRow.prototype.clearSelectionData =
function(silent){
11 this.selecting =
false;
12 this.lastClickedRow =
false;
14 this.selectedRows = [];
17 this._rowSelectionChanged();
21 SelectRow.prototype.initializeRow =
function(row){
23 element = row.getElement();
26 var endSelect =
function(){
28 setTimeout(
function(){
29 self.selecting =
false;
32 document.body.removeEventListener(
"mouseup", endSelect);
36 row.modules.select = {selected:
false};
39 if(
self.table.options.selectableCheck.call(
this.table, row.getComponent())){
40 element.classList.add(
"tabulator-selectable");
41 element.classList.remove(
"tabulator-unselectable");
43 if(
self.table.options.selectable &&
self.table.options.selectable !=
"highlight"){
44 if(
self.table.options.selectableRangeMode ===
"click"){
45 element.addEventListener(
"click",
function(e){
47 self.table._clearSelection();
48 self.lastClickedRow =
self.lastClickedRow || row;
50 var lastClickedRowIdx =
self.table.rowManager.getDisplayRowIndex(
self.lastClickedRow);
51 var rowIdx =
self.table.rowManager.getDisplayRowIndex(row);
53 var fromRowIdx = lastClickedRowIdx <= rowIdx ? lastClickedRowIdx : rowIdx;
54 var toRowIdx = lastClickedRowIdx >= rowIdx ? lastClickedRowIdx : rowIdx;
56 var rows =
self.table.rowManager.getDisplayRows().slice(0);
57 var toggledRows = rows.splice(fromRowIdx, toRowIdx - fromRowIdx + 1);
59 if(e.ctrlKey || e.metaKey){
60 toggledRows.forEach(
function(toggledRow){
61 if(toggledRow !==
self.lastClickedRow){
63 if(
self.table.options.selectable !==
true && !
self.isRowSelected(row)){
64 if(
self.selectedRows.length <
self.table.options.selectable){
65 self.toggleRow(toggledRow);
68 self.toggleRow(toggledRow);
72 self.lastClickedRow = row;
76 if(
self.table.options.selectable !==
true){
77 if(toggledRows.length >
self.table.options.selectable){
78 toggledRows = toggledRows.slice(0,
self.table.options.selectable)
82 self.selectRows(toggledRows);
84 self.table._clearSelection();
86 else if(e.ctrlKey || e.metaKey){
88 self.lastClickedRow = row;
92 self.lastClickedRow = row;
96 element.addEventListener(
"click",
function(e){
97 if(!
self.table.modExists(
"edit") || !
self.table.modules.edit.getCurrentCell()){
98 self.table._clearSelection();
106 element.addEventListener(
"mousedown",
function(e){
108 self.table._clearSelection();
110 self.selecting =
true;
112 self.selectPrev = [];
114 document.body.addEventListener(
"mouseup", endSelect);
115 document.body.addEventListener(
"keyup", endSelect);
123 element.addEventListener(
"mouseenter",
function(e){
125 self.table._clearSelection();
128 if(
self.selectPrev[1] == row){
129 self.toggleRow(
self.selectPrev[0]);
134 element.addEventListener(
"mouseout",
function(e){
136 self.table._clearSelection();
137 self.selectPrev.unshift(row);
144 element.classList.add(
"tabulator-unselectable");
145 element.classList.remove(
"tabulator-selectable");
150 SelectRow.prototype.toggleRow =
function(row){
151 if(this.table.options.selectableCheck.call(
this.table, row.getComponent())){
152 if(row.modules.select && row.modules.select.selected){
153 this._deselectRow(row);
155 this._selectRow(row);
161 SelectRow.prototype.selectRows =
function(rows){
166 this.table.rowManager.rows.forEach((row) => {
167 this._selectRow(row,
true,
true);
170 this._rowSelectionChanged();
175 rowMatch = this.table.rowManager.findRow(rows);
178 this._selectRow(rowMatch,
true,
true);
180 this.table.rowManager.getRows(rows).forEach((row) => {
181 this._selectRow(row,
true,
true);
185 this._rowSelectionChanged();
189 if(Array.isArray(rows)){
190 rows.forEach((row) => {
191 this._selectRow(row,
true,
true);
194 this._rowSelectionChanged();
196 this._selectRow(rows,
false,
true);
203 SelectRow.prototype._selectRow =
function(rowInfo, silent, force){
207 if(!isNaN(this.table.options.selectable) && this.table.options.selectable !==
true && !force){
208 if(this.selectedRows.length >=
this.table.options.selectable){
209 if(this.table.options.selectableRollingSelection){
210 this._deselectRow(this.selectedRows[0]);
217 var row = this.table.rowManager.findRow(rowInfo);
220 if(this.selectedRows.indexOf(row) == -1){
221 if(!row.modules.select){
222 row.modules.select = {};
225 row.modules.select.selected =
true;
226 if(row.modules.select.checkboxEl){
227 row.modules.select.checkboxEl.checked =
true;
229 row.getElement().classList.add(
"tabulator-selected");
231 this.selectedRows.push(row);
235 this.table.options.rowSelected.call(this.table, row.getComponent());
236 this._rowSelectionChanged();
241 console.warn(
"Selection Error - No such row found, ignoring selection:" + rowInfo);
246 SelectRow.prototype.isRowSelected =
function(row){
247 return this.selectedRows.indexOf(row) !== -1;
251 SelectRow.prototype.deselectRows =
function(rows){
255 if(typeof rows ==
"undefined"){
257 rowCount =
self.selectedRows.length;
259 for(let i = 0; i < rowCount; i++){
260 self._deselectRow(
self.selectedRows[0],
true);
263 self._rowSelectionChanged();
265 if(Array.isArray(rows)){
266 rows.forEach(
function(row){
267 self._deselectRow(row,
true);
270 self._rowSelectionChanged();
272 self._deselectRow(rows);
278 SelectRow.prototype._deselectRow =
function(rowInfo, silent){
280 row =
self.table.rowManager.findRow(rowInfo),
284 index =
self.selectedRows.findIndex(
function(selectedRow){
285 return selectedRow == row;
290 if(!row.modules.select){
291 row.modules.select = {};
294 row.modules.select.selected =
false;
295 if(row.modules.select.checkboxEl){
296 row.modules.select.checkboxEl.checked =
false;
298 row.getElement().classList.remove(
"tabulator-selected");
299 self.selectedRows.splice(index, 1);
302 self.table.options.rowDeselected.call(this.table, row.getComponent());
303 self._rowSelectionChanged();
308 console.warn(
"Deselection Error - No such row found, ignoring selection:" + rowInfo);
313 SelectRow.prototype.getSelectedData =
function(){
316 this.selectedRows.forEach(
function(row){
317 data.push(row.getData());
323 SelectRow.prototype.getSelectedRows =
function(){
327 this.selectedRows.forEach(
function(row){
328 rows.push(row.getComponent());
334 SelectRow.prototype._rowSelectionChanged =
function(){
335 if(this.headerCheckboxElement){
336 if(this.selectedRows.length === 0){
337 this.headerCheckboxElement.checked =
false;
338 this.headerCheckboxElement.indeterminate =
false;
339 }
else if(this.table.rowManager.rows.length ===
this.selectedRows.length){
340 this.headerCheckboxElement.checked =
true;
341 this.headerCheckboxElement.indeterminate =
false;
343 this.headerCheckboxElement.indeterminate =
true;
344 this.headerCheckboxElement.checked =
false;
348 this.table.options.rowSelectionChanged.call(this.table, this.getSelectedData(), this.getSelectedRows());
351 SelectRow.prototype.registerRowSelectCheckbox =
function (row, element) {
352 if(!row._row.modules.select){
353 row._row.modules.select = {};
356 row._row.modules.select.checkboxEl = element;
359 SelectRow.prototype.registerHeaderSelectCheckbox =
function (element) {
360 this.headerCheckboxElement = element;
363 Tabulator.prototype.registerModule(
"selectRow", SelectRow);