-
Notifications
You must be signed in to change notification settings - Fork 7
feat(oui-datagrid): add selectable rows #242
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ export default { | |
}, | ||
bindings: { | ||
row: "<", | ||
column: "<" | ||
column: "<", | ||
index: "<?" | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,8 @@ export default class DatagridController { | |
this.columnElements = []; | ||
this.actionColumnElements = []; | ||
this.extraTopElements = []; | ||
this.selectedRows = []; | ||
this.selectAllRows = false; | ||
|
||
this.config = ouiDatagridConfiguration; | ||
|
||
|
@@ -69,6 +71,8 @@ export default class DatagridController { | |
this.filterableColumns = []; | ||
this.criteria = []; | ||
|
||
addBooleanParameter(this, "selectableRows"); | ||
|
||
if (this.id) { | ||
this.ouiDatagridService.registerDatagrid(this); | ||
} | ||
|
@@ -103,7 +107,7 @@ export default class DatagridController { | |
} | ||
|
||
// Manage responsiveness | ||
if (this.hasActionMenu || this.customizable) { | ||
if (this.hasActionMenu || this.customizable || this.selectableRows) { | ||
this.scrollablePanel = this.$element[0].querySelector(".oui-datagrid-responsive-container__overflow-container"); | ||
if (this.scrollablePanel) { | ||
angular.element(this.$window).on("resize", this.checkScroll); | ||
|
@@ -271,6 +275,9 @@ export default class DatagridController { | |
this.displayedRows = DatagridController.createEmptyRows(this.paging.getCurrentPageSize()); | ||
} | ||
|
||
this.selectedRows = this.selectedRows.map(() => false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't this contain the actual rows instead of an array of booleans ? Or something to identify the rows ?
If we keep the array of booleans system, then it should be names something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since every checkbox needs to have an ng-model i still have to created N booleans for binding ; that's why it made sense to use an array of booleans (i reuse the array) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could react to the checkbox's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think your suggestion would make the code "faster" : no reduce in getSelectedRows since you access array directly ; downside is that you need an additionnal array to store rows which is less clean (you still have to give an ngModel to checkbox so you also need an array of booleans). |
||
this.selectAllRows = false; | ||
|
||
this.refreshDatagridPromise = this.$q.when((callback || angular.noop)()) | ||
.then(() => this.paging.loadData(skipSortAndFilter, forceLoadRows)) | ||
.then(result => { | ||
|
@@ -313,6 +320,37 @@ export default class DatagridController { | |
}; | ||
} | ||
|
||
getSelectedRows () { | ||
return this.selectedRows.reduce((result, isSelected, index) => { | ||
if (isSelected) { | ||
result.push(this.displayedRows[index]); | ||
} | ||
return result; | ||
}, []); | ||
} | ||
|
||
toggleRowSelection (index, isSelected) { | ||
const rowCount = this.displayedRows.length; | ||
this.selectedRows[index] = isSelected; | ||
const selectedRowsCount = this.getSelectedRows().length; | ||
|
||
if (selectedRowsCount === rowCount) { | ||
this.selectAllRows = true; | ||
} else if (selectedRowsCount === 0) { | ||
this.selectAllRows = false; | ||
} else { | ||
this.selectAllRows = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the global comportement for tri-select box ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep checkbox component currently use "null" |
||
} | ||
} | ||
|
||
toggleAllRowsSelection (modelValue) { | ||
if (modelValue === null) { | ||
this.selectedRows = this.displayedRows.map(() => true); | ||
} else { | ||
this.selectedRows = this.displayedRows.map(() => modelValue); | ||
} | ||
} | ||
|
||
static createEmptyRows (pageSize) { | ||
return Array(...{ length: pageSize }) | ||
.map(() => undefined); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,8 @@ export default { | |
controller, | ||
require: { | ||
datagridCtrl: "^^ouiDatagrid" | ||
}, | ||
bindings: { | ||
selectedItems: "<" | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this.cellScope.$isSelected = !!isSelected
is possible no ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!!
is harder to read by someone not used to this js "trick" but yes it would fit the jobThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok :)