@@ -2,18 +2,18 @@ import { customElement, FASTElement, html, repeat, observable } from '@microsoft
2
2
import { RowItem } from 'src/utils/build-dummy-data' ;
3
3
4
4
const template = html < Table > `
5
- <table class="table table-hover table-striped test-data">
5
+ <table class="table table-hover table-striped test-data" @click= ${ ( x , c ) => x . handleClick ( c . event ) } >
6
6
<tbody id="tbody">
7
7
${ repeat (
8
8
x => x . rows ,
9
9
html `
10
- < tr data-id ="${ row => row . id } ">
10
+ < tr data-id ="${ row => row . id } " class =" ${ ( row , c ) => ( c . parent . selectedRowId === row . id ? 'danger' : '' ) } " >
11
11
< td class ="col-md-1 "> ${ row => row . id } </ td >
12
12
< td class ="col-md-4 ">
13
- < a class ="lbl "> ${ row => row . label } </ a >
13
+ < a class ="lbl " data-row-id =" ${ row => row . id } " } > ${ row => row . label } </ a >
14
14
</ td >
15
15
< td class ="col-md-1 ">
16
- < a class ="remove " data-row-id ="${ row => row . id } " @click = ${ ( x , c ) => c . parent . handleClick ( c . event ) } >
16
+ < a class ="remove " data-row-id ="${ row => row . id } ">
17
17
< span class ="remove glyphicon glyphicon-remove " aria-hidden ="true "> </ span
18
18
> </ a >
19
19
</ td >
@@ -23,13 +23,13 @@ const template = html<Table>`
23
23
/**
24
24
* List Rendering without view recycling
25
25
*
26
- * With positioning set to true, and resycle set to false,
27
- * Fast wil re-render when internal properties of
26
+ * With recycle set to false,
27
+ * Fast will re-render when internal properties of
28
28
* RowItem[] change (e.g. id or label)
29
29
*
30
30
* https://www.fast.design/docs/fast-element/using-directives
31
31
*/
32
- { positioning : true , recycle : false }
32
+ { recycle : false }
33
33
) }
34
34
</tbody>
35
35
</table>
@@ -49,13 +49,28 @@ const template = html<Table>`
49
49
} )
50
50
export class Table extends FASTElement {
51
51
@observable rows ! : RowItem [ ] ;
52
+ @observable selectedRowId = - 1 ;
52
53
53
54
handleClick ( event : Event ) {
54
- const currentTarget = event . currentTarget as HTMLElement ;
55
+ const target = event . target as HTMLElement ;
55
56
56
- if ( currentTarget . classList . contains ( 'remove' ) ) {
57
- const rowId = currentTarget . dataset [ 'rowId' ] ;
58
- this . $emit ( 'action' , { name : 'deleteRow' , data : Number ( rowId ) } ) ;
57
+ if ( target . classList . contains ( 'remove' ) ) {
58
+ const rowId = target . parentElement ?. dataset [ 'rowId' ] ;
59
+ this . $emit ( 'action' , { name : 'deleteSingleRow' , data : Number ( rowId ) } ) ;
60
+ return ;
61
+ }
62
+
63
+ if ( target . classList . contains ( 'lbl' ) ) {
64
+ const rowId = target . dataset [ 'rowId' ] ;
65
+ this . toggleSelectRow ( Number ( rowId ) ) ;
66
+ return ;
67
+ }
68
+ }
69
+
70
+ toggleSelectRow ( rowId : number ) {
71
+ const rowIndex = this . rows . findIndex ( row => row . id === rowId ) ;
72
+ if ( rowIndex > - 1 ) {
73
+ this . selectedRowId = rowIndex + 1 ;
59
74
}
60
75
}
61
76
}
0 commit comments