Skip to content

Commit b11086e

Browse files
committed
selection.grid.RowModel: switch to store record ids instead of row DOM ids #6671
1 parent fa78109 commit b11086e

File tree

3 files changed

+121
-38
lines changed

3 files changed

+121
-38
lines changed

src/grid/View.mjs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,6 @@ class GridView extends Component {
181181
get selectedRows() {
182182
let {selectionModel} = this;
183183

184-
if (selectionModel.ntype === 'selection-grid-rowmodel') {
185-
return selectionModel.items
186-
}
187-
188184
if (selectionModel.ntype?.includes('row')) {
189185
return selectionModel.selectedRows
190186
}
@@ -578,6 +574,7 @@ class GridView extends Component {
578574
gridContainer = me.parent,
579575
{columns} = gridContainer,
580576
id = me.getRowId(rowIndex),
577+
recordId = record[me.store.getKeyProperty()],
581578
rowCls = me.getRowClass(record, rowIndex),
582579
config, column, columnPosition, gridRow, i;
583580

@@ -586,20 +583,15 @@ class GridView extends Component {
586583
}
587584

588585
if (selectedRows && record[me.selectedRecordField]) {
589-
NeoArray.add(selectedRows, id)
590-
}
591-
592-
if (selectedRows?.includes(id)) {
593-
rowCls.push('neo-selected');
594-
gridContainer.fire('select', {record})
586+
NeoArray.add(selectedRows, recordId)
595587
}
596588

597589
gridRow = {
598590
id,
599591
'aria-rowindex': rowIndex + 2, // header row => 1, first body row => 2
600592
cls : rowCls,
601593
cn : [],
602-
data : {recordId: record[me.store.getKeyProperty()]},
594+
data : {recordId},
603595
role : 'row',
604596

605597
style: {
@@ -608,6 +600,12 @@ class GridView extends Component {
608600
}
609601
};
610602

603+
if (selectedRows?.includes(recordId)) {
604+
rowCls.push('neo-selected');
605+
gridRow['aria-selected'] = true;
606+
gridContainer.fire('select', {record})
607+
}
608+
611609
for (i=mountedColumns[0]; i <= mountedColumns[1]; i++) {
612610
column = columns.getAt(i);
613611
config = me.applyRendererOutput({column, columnIndex: i, record, rowIndex});

src/selection/grid/BaseModel.mjs

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import Model from '../Model.mjs';
1+
import Model from '../Model.mjs';
2+
import NeoArray from '../../util/Array.mjs';
23

34
/**
45
* Abstract base class for all grid related selection models
@@ -12,7 +13,13 @@ class BaseModel extends Model {
1213
* @member {String} className='Neo.selection.grid.BaseModel'
1314
* @protected
1415
*/
15-
className: 'Neo.selection.grid.BaseModel'
16+
className: 'Neo.selection.grid.BaseModel',
17+
/**
18+
* Storing the record ids
19+
* @member {Number[]|String[]} selectedRows=[]
20+
* @protected
21+
*/
22+
selectedRows: []
1623
}
1724

1825
/**
@@ -23,6 +30,53 @@ class BaseModel extends Model {
2330
return this.view.parent.columns.items.map(column => column.dataField)
2431
}
2532

33+
/**
34+
* @param {Boolean} [silent=false] true to prevent a vdom update
35+
*/
36+
deselectAllRows(silent=false) {
37+
let me = this,
38+
items = [...me.selectedRows],
39+
{view} = me;
40+
41+
if (items.length) {
42+
items.forEach(item => {
43+
me.deselectRow(item, true)
44+
});
45+
46+
if (!silent && items.length > 0) {
47+
view.update()
48+
}
49+
50+
me.fire('selectionChange', {
51+
selection: me.selectedRows
52+
})
53+
} else if (!silent) {
54+
me.fire('noChange')
55+
}
56+
}
57+
58+
/**
59+
* @param {Number|String} recordId
60+
* @param {Boolean} [silent=false]
61+
*/
62+
deselectRow(recordId, silent=false) {
63+
let me = this,
64+
{view} = me,
65+
{store} = view,
66+
record = store.get(recordId),
67+
rowId = view.getRowId(store.indexOf(record)),
68+
node = view.getVdomChild(rowId);
69+
70+
if (node) {
71+
node.cls = NeoArray.remove(node.cls || [], me.selectedCls);
72+
delete node['aria-selected']
73+
}
74+
75+
me.selectedRows = [recordId];
76+
77+
!silent && view.update()
78+
}
79+
2680
/**
2781
* Get the record for a given event path
2882
* @param {Object[]} path
@@ -61,6 +115,48 @@ class BaseModel extends Model {
61115

62116
return false
63117
}
118+
119+
/**
120+
* @param {Number|String} recordId
121+
* @returns {Boolean} true in case the row is selected
122+
*/
123+
isSelectedRow(recordId) {
124+
return this.selectedRows.includes(recordId)
125+
}
126+
127+
/**
128+
* @param {Number|String} recordId
129+
* @param {Boolean} [silent=false]
130+
*/
131+
selectRow(recordId, silent=false) {
132+
let me = this,
133+
{view} = me,
134+
{store} = view,
135+
record = store.get(recordId),
136+
rowId = view.getRowId(store.indexOf(record)),
137+
node = view.getVdomChild(rowId);
138+
139+
if (me.singleSelect) {
140+
me.deselectAllRows(true)
141+
}
142+
143+
if (node) {
144+
node.cls = NeoArray.add(node.cls || [], me.selectedCls);
145+
node['aria-selected'] = true
146+
}
147+
148+
me.selectedRows = [recordId];
149+
150+
!silent && view.update()
151+
}
152+
153+
/**
154+
* @param {Number|String} recordId
155+
* @param {Boolean} [silent=false]
156+
*/
157+
toggleRowSelection(recordId, silent=false) {
158+
this[this.isSelectedRow(recordId) ? 'deselectRow' : 'selectRow'](recordId, silent)
159+
}
64160
}
65161

66162
export default Neo.setupClass(BaseModel);

src/selection/grid/RowModel.mjs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,7 @@ class RowModel extends BaseModel {
2020
* @member {String} cls='neo-selection-rowmodel'
2121
* @protected
2222
*/
23-
cls: 'neo-selection-rowmodel',
24-
/**
25-
* Storing the record ids
26-
* @member {Number[]|String[]} selectedRows=[]
27-
* @protected
28-
*/
29-
selectedRows: []
23+
cls: 'neo-selection-rowmodel'
3024
}
3125

3226
/**
@@ -79,14 +73,11 @@ class RowModel extends BaseModel {
7973
{view} = me,
8074
{store} = view,
8175
countRecords = store.getCount(),
82-
currentIndex = 0,
83-
newIndex, record, rowId;
84-
85-
if (me.hasSelection()) {
86-
currentIndex = store.indexOf(view.getRecordByRowId(me.items[0]))
87-
}
88-
89-
newIndex = (currentIndex + step) % countRecords;
76+
keyProperty = store.getKeyProperty(),
77+
recordId = me.selectedRows[0] || store.getAt(0)[keyProperty],
78+
record = store.get(recordId),
79+
index = store.indexOf(record),
80+
newIndex = (index + step) % countRecords;
9081

9182
while (newIndex < 0) {
9283
newIndex += countRecords
@@ -97,12 +88,12 @@ class RowModel extends BaseModel {
9788
if (me.hasAnnotations(record)) {
9889
me.updateAnnotations(record)
9990
} else {
100-
rowId = view.getRowId(record);
91+
recordId = record[keyProperty];
10192

102-
if (rowId) {
103-
me.select(rowId);
93+
if (recordId) {
94+
me.selectRow(recordId);
10495

105-
view.scrollByRows(currentIndex, step);
96+
view.scrollByRows(index, step);
10697
view.fire('select', {record})
10798
}
10899
}
@@ -116,19 +107,17 @@ class RowModel extends BaseModel {
116107
id = data.currentTarget,
117108
{view} = me,
118109
record = id && me.getRecord(data.path, id),
119-
isSelected;
110+
recordId;
120111

121112
if (record) {
122113
if (me.hasAnnotations(record)) {
123114
me.updateAnnotations(record)
124115
} else {
125-
me.toggleSelection(id);
126-
127-
isSelected = me.isSelected(id);
116+
recordId = record[view.store.getKeyProperty()];
128117

129-
!isSelected && view.onDeselect?.(record);
118+
me.toggleRowSelection(recordId);
130119

131-
view.fire(isSelected ? 'select' : 'deselect', {record})
120+
view.fire(me.isSelectedRow(recordId) ? 'select' : 'deselect', {record})
132121
}
133122
}
134123
}

0 commit comments

Comments
 (0)