Skip to content

Commit

Permalink
New property to prevent the user from clearing the selection
Browse files Browse the repository at this point in the history
  • Loading branch information
mgroeneweg committed Nov 7, 2016
1 parent 27fac5f commit 35d2c87
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ The widget does not display search filter inputs. To provide search filters, def
- _Use infinite scroll_ - Use infinite scroll rather than the default paging buttons. Set nowrap on the table class!
- _Horizontal scrolling_ - When true, horizontal scrolling is used. Set nowrap on the table class!
- _Vertical scrolling_ - Optional, any CSS unit, default 200px. When specified, vertical scrolling is used and height of the rows is constrained to the specified height
- _State save name_ - Optional. When specified, grid layout state is saved to and loaded from browser local storage using the specied name. Make sure this name is unique across your application!
- _State save name_ - Optional. When specified, grid layout state is saved to and loaded from browser local storage using the specied name. Make sure this name is unique across your application! It is also advisable to put your project name first in each statesaving name, to prevent issues where multiple apps have an order overview grid.

#### Column visibility

Expand Down Expand Up @@ -181,6 +181,7 @@ The _Selection changed callback microflow_ can be used to create functionality s

- _Selection_ - Selection type
- _Select first row_ - Select first row after displaying data
- _Allow deselect_ - Allow deselect. When off, at least one row must remain selected.
- _Selection changed callback microflow_ - The name of the microflow (Module.Microflow) that is called when the selection has been changed.

### Buttons
Expand Down
5 changes: 5 additions & 0 deletions src/DataTables/DataTables.xml
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@
<category>Selection</category>
<description>Select first row after displaying data</description>
</property>
<property key="allowDeselect" type="boolean" defaultValue="true">
<caption>Allow deselect</caption>
<category>Selection</category>
<description>Allow deselect. When off, at least one row must remain selected.</description>
</property>
<property key="selectionMicroflowName" type="string" required="false">
<caption>Selection changed callback microflow</caption>
<category>Selection</category>
Expand Down
48 changes: 36 additions & 12 deletions src/DataTables/widget/DataTables.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ define([
scrollY: null,
selectionType: null,
selectFirst: false,
allowDeselect: true,
selectionMicroflowName: "",
columnList: null,
attrSearchFilterList: null,
Expand Down Expand Up @@ -113,6 +114,7 @@ define([
_sortDir: "",
_exportButton: null,
_scrollerPage: null,
_previousSelection: null,

// I18N file names object at the end, out of sight!

Expand Down Expand Up @@ -344,7 +346,8 @@ define([
cellNode = dataTablesThisObj.api().cell({row : rowIdx, column : colIdx}).node();
cellNode.setAttribute("data-columnName", tdData.name);
}, this);
if (thisObj.selectFirst && rowLoop === 0) {
// Select the first row, unless there is already a selection. The draw callback can be called multiple times.
if (thisObj.selectFirst && rowLoop === 0 && !thisObj._table.rows({selected: true}).any()) {
this.select();
}
});
Expand Down Expand Up @@ -475,15 +478,15 @@ define([
this._table
.on("select", function (e, dt, type, indexes) {
if (thisObj.selectionType !== "none") {
thisObj._setButtonEnabledStatus();
thisObj._setButtonEnabledStatus("select");
}
if (thisObj.selectionMicroflowName) {
thisObj._callSelectionMicroflow();
}
})
.on("deselect", function (e, dt, type, indexes) {
if (thisObj.selectionType !== "none") {
thisObj._setButtonEnabledStatus();
thisObj._setButtonEnabledStatus("deselect");
}
if (thisObj.selectionMicroflowName) {
thisObj._callSelectionMicroflow();
Expand Down Expand Up @@ -793,15 +796,10 @@ define([
});
},

// Get selected rows
_getSelectedRowData: function () {
return this._table.rows({selected: true}).data().toArray();
},

// Get selected rows
_getSelectedRows: function () {
var guids = [],
rowDataArray = this._getSelectedRowData();
rowDataArray = this._table.rows({selected: true}).data().toArray();
dojoArray.forEach(rowDataArray, function (rowData) {
guids.push(rowData.guid);
});
Expand Down Expand Up @@ -1237,14 +1235,17 @@ define([
},

// Enable/Disable buttons when selection changes
_setButtonEnabledStatus: function () {
_setButtonEnabledStatus: function (eventName) {
logger.debug(this.id + "._setButtonEnabledStatus");
var attrValue,
buttonDefinition,
buttonEnabled,
hasSelection,
rowDataArray;
rowDataArray = this._getSelectedRowData();
rowDataArray,
selection,
thisObj = this;
selection = this._table.rows({selected: true});
rowDataArray = selection.data().toArray();
hasSelection = (rowDataArray.length > 0);
dojoArray.forEach(this._buttonList, function (button, i) {
if (hasSelection) {
Expand Down Expand Up @@ -1278,6 +1279,29 @@ define([
button.setAttribute("disabled", "");
}
}, this);
switch (eventName) {
case "select":
this._previousSelection = selection;
break;

case "deselect":
// If there is still a selection or deselection is allowed, just save the selection
if (selection.any() || this.allowDeselect) {
this._previousSelection = selection;
} else {
console.log("No selection anymore");
setTimeout(function () {
if (!thisObj._table.rows({selected: true}).any() && thisObj._previousSelection) {
console.log("Reset selection");
thisObj._previousSelection.select();
}
}, 100);
}
break;

default:
break;
}
},

// Reset subscriptions.
Expand Down
Binary file modified test/DataTablesTest.mpr
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,19 @@ public static void oCh_Person_ShowSelectedPerson(IContext context, datatablestes
throw new MendixRuntimeException(e);
}
}
public static void oCh_Person_ShowSelectedPerson_NoEmptySelection(IContext context, datatablestestmodule.proxies.Person _person)
{
try
{
Map<String, Object> params = new HashMap<String, Object>();
params.put("Person", _person == null ? null : _person.getMendixObject());
Core.execute(context, "DataTablesTestModule.OCh_Person_ShowSelectedPerson_NoEmptySelection", params);
}
catch (CoreException e)
{
throw new MendixRuntimeException(e);
}
}
public static datatablestestmodule.proxies.DataTableContextEntity sUB_DataTableContextEntity_GetFromSession(IContext context)
{
try
Expand Down
Binary file modified test/widgets/DataTables.mpk
Binary file not shown.

0 comments on commit 35d2c87

Please sign in to comment.