Maintain internal checked rows state, update DOM state while scrolling

This commit is contained in:
Faris Ansari 2017-10-07 20:14:38 +05:30
parent 0ae98564e2
commit 8de87e25d7
3 changed files with 125 additions and 20 deletions

View File

@ -208,10 +208,17 @@ var ReGrid = function () {
// defer remaining rows // defer remaining rows
this.data.rows.slice(this.start, this.end)); this.data.rows.slice(this.start, this.end));
var self = this;
this.clusterize = new Clusterize({ this.clusterize = new Clusterize({
rows: initialData, rows: initialData,
scrollElem: this.bodyScrollable.get(0), scrollElem: this.bodyScrollable.get(0),
contentElem: this.bodyScrollable.find('tbody').get(0) contentElem: this.bodyScrollable.find('tbody').get(0),
callbacks: {
clusterChanged: function clusterChanged() {
self.highlightCheckedRows();
}
}
}); });
this.log('dataAppended', this.pageLength); this.log('dataAppended', this.pageLength);
this.appendRemainingData(); this.appendRemainingData();
@ -666,6 +673,9 @@ var ReGrid = function () {
if (!this.options.addCheckbox) return; if (!this.options.addCheckbox) return;
var self = this; var self = this;
// map of checked rows
this.checkMap = [];
this.wrapper.on('click', '.data-table-col[data-col-index="0"] [type="checkbox"]', function () { this.wrapper.on('click', '.data-table-col[data-col-index="0"] [type="checkbox"]', function () {
var $checkbox = $(this); var $checkbox = $(this);
var $cell = $checkbox.closest('.data-table-col'); var $cell = $checkbox.closest('.data-table-col');
@ -677,19 +687,57 @@ var ReGrid = function () {
var checked = $checkbox.is(':checked'); var checked = $checkbox.is(':checked');
if (isHeader) { if (isHeader) {
self.highlightAll(checked); self.checkAll(checked);
} else { } else {
self.highlightRow(rowIndex, checked); self.checkRow(rowIndex, checked);
} }
}); });
} }
}, { }, {
key: 'highlightAll', key: 'getCheckedRows',
value: function highlightAll() { value: function getCheckedRows() {
var toggle = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; return this.checkMap.map(function (c, rowIndex) {
if (c) {
return rowIndex;
}
return null;
}).filter(function (c) {
return c !== null || c !== undefined;
});
}
}, {
key: 'highlightCheckedRows',
value: function highlightCheckedRows() {
var _this4 = this;
this.getCheckedRows().map(function (rowIndex) {
return _this4.checkRow(rowIndex, true);
});
}
}, {
key: 'checkRow',
value: function checkRow(rowIndex, toggle) {
var value = toggle ? 1 : 0;
// update internal map
this.checkMap[rowIndex] = value;
// set checkbox value explicitly
this.bodyScrollable.find('.data-table-col[data-row-index="' + rowIndex + '"][data-col-index="0"] [type="checkbox"]').prop('checked', toggle);
// highlight row
this.highlightRow(rowIndex, toggle);
}
}, {
key: 'checkAll',
value: function checkAll(toggle) {
var value = toggle ? 1 : 0;
this.checkMap.map(function (c) {
return value;
});
// set checkbox value
this.bodyScrollable.find('.data-table-col[data-col-index="0"] [type="checkbox"]').prop('checked', toggle); this.bodyScrollable.find('.data-table-col[data-col-index="0"] [type="checkbox"]').prop('checked', toggle);
this.bodyScrollable.find('.data-table-row').toggleClass('row-highlight', toggle); // highlight all
this.highlightAll(toggle);
} }
}, { }, {
key: 'highlightRow', key: 'highlightRow',
@ -704,6 +752,13 @@ var ReGrid = function () {
$row.removeClass('row-highlight'); $row.removeClass('row-highlight');
} }
} }
}, {
key: 'highlightAll',
value: function highlightAll() {
var toggle = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
this.bodyScrollable.find('.data-table-row').toggleClass('row-highlight', toggle);
}
}, { }, {
key: 'setColumnWidth', key: 'setColumnWidth',
value: function setColumnWidth(colIndex, width) { value: function setColumnWidth(colIndex, width) {
@ -740,7 +795,7 @@ var ReGrid = function () {
}, { }, {
key: 'setColumnWidths', key: 'setColumnWidths',
value: function setColumnWidths() { value: function setColumnWidths() {
var _this4 = this; var _this5 = this;
var availableWidth = this.wrapper.width(); var availableWidth = this.wrapper.width();
var headerWidth = this.header.width(); var headerWidth = this.header.width();
@ -753,15 +808,15 @@ var ReGrid = function () {
var deltaWidth = (availableWidth - headerWidth) / this.data.columns.length; var deltaWidth = (availableWidth - headerWidth) / this.data.columns.length;
this.data.columns.map(function (col) { this.data.columns.map(function (col) {
var width = _this4.getColumnHeaderElement(col.colIndex).width(); var width = _this5.getColumnHeaderElement(col.colIndex).width();
var finalWidth = width + deltaWidth - 16; var finalWidth = width + deltaWidth - 16;
if (_this4.options.addSerialNoColumn && col.colIndex === 0) { if (_this5.options.addSerialNoColumn && col.colIndex === 0) {
return; return;
} }
_this4.setColumnHeaderWidth(col.colIndex, finalWidth); _this5.setColumnHeaderWidth(col.colIndex, finalWidth);
_this4.setColumnWidth(col.colIndex, finalWidth); _this5.setColumnWidth(col.colIndex, finalWidth);
}); });
this.setBodyWidth(); this.setBodyWidth();
} }

File diff suppressed because one or more lines are too long

View File

@ -119,10 +119,17 @@ export default class ReGrid {
this.data.rows.slice(this.start, this.end) this.data.rows.slice(this.start, this.end)
); );
const self = this;
this.clusterize = new Clusterize({ this.clusterize = new Clusterize({
rows: initialData, rows: initialData,
scrollElem: this.bodyScrollable.get(0), scrollElem: this.bodyScrollable.get(0),
contentElem: this.bodyScrollable.find('tbody').get(0) contentElem: this.bodyScrollable.find('tbody').get(0),
callbacks: {
clusterChanged() {
self.highlightCheckedRows();
}
}
}); });
this.log('dataAppended', this.pageLength); this.log('dataAppended', this.pageLength);
this.appendRemainingData(); this.appendRemainingData();
@ -534,6 +541,9 @@ export default class ReGrid {
if (!this.options.addCheckbox) return; if (!this.options.addCheckbox) return;
const self = this; const self = this;
// map of checked rows
this.checkMap = [];
this.wrapper.on('click', '.data-table-col[data-col-index="0"] [type="checkbox"]', function () { this.wrapper.on('click', '.data-table-col[data-col-index="0"] [type="checkbox"]', function () {
const $checkbox = $(this); const $checkbox = $(this);
const $cell = $checkbox.closest('.data-table-col'); const $cell = $checkbox.closest('.data-table-col');
@ -541,20 +551,54 @@ export default class ReGrid {
const checked = $checkbox.is(':checked'); const checked = $checkbox.is(':checked');
if (isHeader) { if (isHeader) {
self.highlightAll(checked); self.checkAll(checked);
} else { } else {
self.highlightRow(rowIndex, checked); self.checkRow(rowIndex, checked);
} }
}); });
} }
highlightAll(toggle = true) { getCheckedRows() {
return this.checkMap
.map((c, rowIndex) => {
if (c) {
return rowIndex;
}
return null;
})
.filter(c => {
return c !== null || c !== undefined;
});
}
highlightCheckedRows() {
this.getCheckedRows()
.map(rowIndex => this.checkRow(rowIndex, true));
}
checkRow(rowIndex, toggle) {
const value = toggle ? 1 : 0;
// update internal map
this.checkMap[rowIndex] = value;
// set checkbox value explicitly
this.bodyScrollable
.find(`.data-table-col[data-row-index="${rowIndex}"][data-col-index="0"] [type="checkbox"]`)
.prop('checked', toggle);
// highlight row
this.highlightRow(rowIndex, toggle);
}
checkAll(toggle) {
const value = toggle ? 1 : 0;
this.checkMap.map(c => value);
// set checkbox value
this.bodyScrollable this.bodyScrollable
.find('.data-table-col[data-col-index="0"] [type="checkbox"]') .find('.data-table-col[data-col-index="0"] [type="checkbox"]')
.prop('checked', toggle); .prop('checked', toggle);
this.bodyScrollable // highlight all
.find('.data-table-row') this.highlightAll(toggle);
.toggleClass('row-highlight', toggle);
} }
highlightRow(rowIndex, toggle = true) { highlightRow(rowIndex, toggle = true) {
@ -567,6 +611,12 @@ export default class ReGrid {
} }
} }
highlightAll(toggle = true) {
this.bodyScrollable
.find('.data-table-row')
.toggleClass('row-highlight', toggle);
}
setColumnWidth(colIndex, width) { setColumnWidth(colIndex, width) {
// set width for content // set width for content
this.setStyle(`[data-col-index="${colIndex}"] .content`, { this.setStyle(`[data-col-index="${colIndex}"] .content`, {