Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional events when selecting/deselecting rows #1457

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 76 additions & 5 deletions dev/jquery.jtable.selecting.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
selectOnRowClick: true,

//Events
selectionChanged: function (event, data) { }
selectionChanged: function (event, data) { },
beforeRowSelected: function (event, $row) { },
rowSelected: function (event, $row) { },
beforeRowDeselected: function (event, $row) { },
rowDeselected: function (event, $row) { }
},

/************************************************************************
Expand Down Expand Up @@ -94,6 +98,13 @@
this._onSelectionChanged(); //TODO: trigger only if selected rows changes?
},

/* Deselect row/rows
*************************************************************************/
deselectRows: function ($rows) {
this._deselectRows($rows);
this._onSelectionChanged(); //TODO: trigger only if selected rows changes?
},

/************************************************************************
* OVERRIDED METHODS *
*************************************************************************/
Expand Down Expand Up @@ -322,6 +333,22 @@
/* Makes row/rows 'selected'.
*************************************************************************/
_selectRows: function ($rows) {
$allRows = $rows;
$rows = $rows.filter(function (key, row) {
var $row = $(row);
var isSelected = $row.hasClass('jtable-row-selected');
if (
this._onBeforeRowSelected($row) === false
|| isSelected
) {
if (!isSelected) {
$row.find('>td.jtable-selecting-column >input').prop('checked', false);
}
return false;
}
return true;
}.bind(this));

if (!this.options.multiselect) {
this._deselectRows(this._getSelectedRows());
}
Expand All @@ -334,17 +361,45 @@
}

this._refreshSelectAllCheckboxState();

$rows.each(function (key, row) {
this._onRowSelected($(row));
}.bind(this));

return $rows;
},

/* Makes row/rows 'non selected'.
*************************************************************************/
_deselectRows: function ($rows) {
$allRows = $rows;
$rows = $rows.filter(function (key, row) {
var $row = $(row);
var isSelected = $row.hasClass('jtable-row-selected');
if (
this._onBeforeRowDeselected($(row)) === false
|| !isSelected
) {
if (isSelected) {
$row.find('>td.jtable-selecting-column >input').prop('checked', true);
}
return false;
}
return true;
}.bind(this));

$rows.removeClass('jtable-row-selected ui-state-highlight');
if (this.options.selectingCheckboxes) {
$rows.find('>td.jtable-selecting-column >input').prop('checked', false);
}

this._refreshSelectAllCheckboxState();

$rows.each(function (key, row) {
this._onRowDeselected($(row));
}.bind(this));

return $rows;
},

/* Updates state of the 'select/deselect' all checkbox according to count of selected rows.
Expand All @@ -359,12 +414,12 @@

if (selectedRowCount == 0) {
this._$selectAllCheckbox.prop('indeterminate', false);
this._$selectAllCheckbox.attr('checked', false);
this._$selectAllCheckbox.prop('checked', false);
} else if (selectedRowCount == totalRowCount) {
this._$selectAllCheckbox.prop('indeterminate', false);
this._$selectAllCheckbox.attr('checked', true);
this._$selectAllCheckbox.prop('indeterminate', false);
this._$selectAllCheckbox.prop('checked', true);
} else {
this._$selectAllCheckbox.attr('checked', false);
this._$selectAllCheckbox.prop('checked', false);
this._$selectAllCheckbox.prop('indeterminate', true);
}
},
Expand All @@ -375,6 +430,22 @@

_onSelectionChanged: function () {
this._trigger("selectionChanged", null, {});
},

_onBeforeRowSelected: function ($row) {
return this._trigger("beforeRowSelected", null, $row);
},

_onRowSelected: function ($row) {
return this._trigger("rowSelected", null, $row);
},

_onBeforeRowDeselected: function ($row) {
return this._trigger("beforeRowDeselected", null, $row);
},

_onRowDeselected: function ($row) {
return this._trigger("rowDeselected", null, $row);
}

});
Expand Down