@@ -10,9 +10,14 @@ import debounce from "@ui5/webcomponents-base/dist/util/debounce.js";
10
10
import isElementInView from "@ui5/webcomponents-base/dist/util/isElementInView.js" ;
11
11
import TableGrowingMode from "./types/TableGrowingMode.js" ;
12
12
import BusyIndicator from "./BusyIndicator.js" ;
13
+ import TableMode from "./types/TableMode.js" ;
13
14
14
15
// Texts
15
- import { LOAD_MORE_TEXT } from "./generated/i18n/i18n-defaults.js" ;
16
+ import {
17
+ LOAD_MORE_TEXT ,
18
+ ARIA_LABEL_SELECT_ALL_CHECKBOX ,
19
+ TABLE_HEADER_ROW_TEXT ,
20
+ } from "./generated/i18n/i18n-defaults.js" ;
16
21
17
22
// Template
18
23
import TableTemplate from "./generated/templates/TableTemplate.lit.js" ;
@@ -191,6 +196,25 @@ const metadata = {
191
196
type : Boolean ,
192
197
} ,
193
198
199
+ /**
200
+ * Defines the mode of the <code>ui5-table</code>.
201
+ * <br><br>
202
+ * Available options are:
203
+ * <ul>
204
+ * <li><code>MultiSelect</code></li>
205
+ * <li><code>SingleSelect</code></li>
206
+ * <li><code>None</code></li>
207
+ * <ul>
208
+ * @type {TableMode }
209
+ * @defaultvalue "None"
210
+ * @since 1.0.0-rc.15
211
+ * @public
212
+ */
213
+ mode : {
214
+ type : TableMode ,
215
+ defaultValue : TableMode . None ,
216
+ } ,
217
+
194
218
_hiddenColumns : {
195
219
type : Object ,
196
220
multiple : true ,
@@ -223,13 +247,24 @@ const metadata = {
223
247
_inViewport : {
224
248
type : Boolean ,
225
249
} ,
250
+
251
+ /**
252
+ * Defines whether all rows are selected or not when table is in MultiSelect mode.
253
+ * @type {Boolean }
254
+ * @defaultvalue false
255
+ * @since 1.0.0-rc.15
256
+ * @private
257
+ */
258
+ _allRowsSelected : {
259
+ type : Boolean ,
260
+ } ,
226
261
} ,
227
262
events : /** @lends sap.ui.webcomponents.main.Table.prototype */ {
228
263
/**
229
- * Fired when a row is clicked.
264
+ * Fired when a row in <code>Active</code> mode is clicked or <code>Enter</code> key is pressed .
230
265
*
231
266
* @event sap.ui.webcomponents.main.Table#row-click
232
- * @param {HTMLElement } row the clicked row.
267
+ * @param {HTMLElement } row the activated row.
233
268
* @public
234
269
*/
235
270
"row-click" : {
@@ -262,6 +297,23 @@ const metadata = {
262
297
* @since 1.0.0-rc.11
263
298
*/
264
299
"load-more" : { } ,
300
+
301
+ /**
302
+ * Fired when selection is changed by user interaction
303
+ * in <code>SingleSelect</code> and <code>MultiSelect</code> modes.
304
+ *
305
+ * @event sap.ui.webcomponents.main.Table#selection-change
306
+ * @param {Array } selectedRows An array of the selected rows.
307
+ * @param {Array } previouslySelectedRows An array of the previously selected rows.
308
+ * @public
309
+ * @since 1.0.0-rc.15
310
+ */
311
+ "selection-change" : {
312
+ detail : {
313
+ selectedRows : { type : Array } ,
314
+ previouslySelectedRows : { type : Array } ,
315
+ } ,
316
+ } ,
265
317
} ,
266
318
} ;
267
319
@@ -345,6 +397,7 @@ class Table extends UI5Element {
345
397
this . i18nBundle = getI18nBundle ( "@ui5/webcomponents" ) ;
346
398
347
399
this . tableEndObserved = false ;
400
+ this . addEventListener ( "ui5-selection-requested" , this . _handleSelect . bind ( this ) ) ;
348
401
}
349
402
350
403
onBeforeRendering ( ) {
@@ -360,6 +413,7 @@ class Table extends UI5Element {
360
413
row . _busy = this . busy ;
361
414
row . removeEventListener ( "ui5-_focused" , this . fnOnRowFocused ) ;
362
415
row . addEventListener ( "ui5-_focused" , this . fnOnRowFocused ) ;
416
+ row . mode = this . mode ;
363
417
} ) ;
364
418
365
419
this . visibleColumns = this . columns . filter ( ( column , index ) => {
@@ -446,6 +500,79 @@ class Table extends UI5Element {
446
500
this . fireEvent ( "load-more" ) ;
447
501
}
448
502
503
+ _handleSingleSelect ( event ) {
504
+ const row = this . getRowParent ( event . target ) ;
505
+ if ( ! row . selected ) {
506
+ const previouslySelectedRows = this . selectedRows ;
507
+ this . rows . forEach ( item => {
508
+ if ( item . selected ) {
509
+ item . selected = false ;
510
+ }
511
+ } ) ;
512
+ row . selected = true ;
513
+ this . fireEvent ( "selection-change" , {
514
+ selectedRows : [ row ] ,
515
+ previouslySelectedRows,
516
+ } ) ;
517
+ }
518
+ }
519
+
520
+ _handleMultiSelect ( event ) {
521
+ const row = this . getRowParent ( event . target ) ;
522
+ const previouslySelectedRows = this . selectedRows ;
523
+
524
+ row . selected = ! row . selected ;
525
+
526
+ const selectedRows = this . selectedRows ;
527
+
528
+ if ( selectedRows . length === this . rows . length ) {
529
+ this . _allRowsSelected = true ;
530
+ } else {
531
+ this . _allRowsSelected = false ;
532
+ }
533
+
534
+ this . fireEvent ( "selection-change" , {
535
+ selectedRows,
536
+ previouslySelectedRows,
537
+ } ) ;
538
+ }
539
+
540
+ _handleSelect ( event ) {
541
+ this [ `_handle${ this . mode } ` ] ( event ) ;
542
+ }
543
+
544
+ _selectAll ( event ) {
545
+ const bAllSelected = event . target . checked ;
546
+ const previouslySelectedRows = this . rows . filter ( row => row . selected ) ;
547
+
548
+ this . _allRowsSelected = bAllSelected ;
549
+
550
+ this . rows . forEach ( row => {
551
+ row . selected = bAllSelected ;
552
+ } ) ;
553
+
554
+ const selectedRows = bAllSelected ? this . rows : [ ] ;
555
+
556
+ this . fireEvent ( "selection-change" , {
557
+ selectedRows,
558
+ previouslySelectedRows,
559
+ } ) ;
560
+ }
561
+
562
+ getRowParent ( child ) {
563
+ const parent = child . parentElement ;
564
+
565
+ if ( child . hasAttribute ( "ui5-table-row" ) ) {
566
+ return child ;
567
+ }
568
+
569
+ if ( parent && parent . hasAttribute ( "ui5-table-row" ) ) {
570
+ return parent ;
571
+ }
572
+
573
+ this . getRowParent ( parent ) ;
574
+ }
575
+
449
576
getColumnHeader ( ) {
450
577
return this . getDomRef ( ) && this . getDomRef ( ) . querySelector ( `#${ this . _id } -columnHeader` ) ;
451
578
}
@@ -479,7 +606,9 @@ class Table extends UI5Element {
479
606
} ) ;
480
607
481
608
if ( visibleColumnsIndexes . length ) {
482
- this . columns [ visibleColumnsIndexes [ 0 ] ] . first = true ;
609
+ if ( ! this . isMultiSelect ) {
610
+ this . columns [ visibleColumnsIndexes [ 0 ] ] . first = true ;
611
+ }
483
612
this . columns [ visibleColumnsIndexes [ visibleColumnsIndexes . length - 1 ] ] . last = true ;
484
613
}
485
614
@@ -550,6 +679,19 @@ class Table extends UI5Element {
550
679
return this . moreText || this . i18nBundle . getText ( LOAD_MORE_TEXT ) ;
551
680
}
552
681
682
+ get ariaLabelText ( ) {
683
+ const headerRowText = this . i18nBundle . getText ( TABLE_HEADER_ROW_TEXT ) ;
684
+ const columnsTitle = this . columns . map ( column => {
685
+ return column . textContent . trim ( ) ;
686
+ } ) . join ( " " ) ;
687
+
688
+ return `${ headerRowText } ${ columnsTitle } ` ;
689
+ }
690
+
691
+ get ariaLabelSelectAllText ( ) {
692
+ return this . i18nBundle . getText ( ARIA_LABEL_SELECT_ALL_CHECKBOX ) ;
693
+ }
694
+
553
695
get loadMoreAriaLabelledBy ( ) {
554
696
if ( this . moreDataText ) {
555
697
return `${ this . _id } -showMore-text ${ this . _id } -showMore-desc` ;
@@ -569,6 +711,14 @@ class Table extends UI5Element {
569
711
570
712
return this . _inViewport ? "absolute" : "sticky" ;
571
713
}
714
+
715
+ get isMultiSelect ( ) {
716
+ return this . mode === "MultiSelect" ;
717
+ }
718
+
719
+ get selectedRows ( ) {
720
+ return this . rows . filter ( row => row . selected ) ;
721
+ }
572
722
}
573
723
574
724
Table . define ( ) ;
0 commit comments