動くか?
js
1
2 < style >
3 #tableMain . template { display : none ; }
4 . userName : after { content : "さん" ; }
5
6 < / style >
7
8 < h1 > 出退勤リスト < / h1 >
9 < h2 > < output class = "userName" > < / output > < / h2 >
10
11 < table border = "1" id = "tableMain" >
12 < thead >
13 < tr > < th > 日付 < th > 出社 < th > 退社 < th > 削除
14
15 < tbody >
16 < tr class = "template" >
17 < td >
18 < input type = "hidden" name = "uid" >
19 < output name = "outTimeValue" class = "id" >
20 < td > < input name = "inTime" >
21 < td > < input name = "outTime" >
22 < td > < input type = "button" value = "削除" class = "deleteBtn" >
23 < / table >
24
25
26 < script >
27
28 const TB = document . getElementById ( 'tableMain' ) ;
29
30 firebase . auth ( ) . onAuthStateChanged ( ( user ) => {
31 document . querySelectorAll ( 'h2 ouput.userName' ) [ 0 ] . textContent = user . displayName ;
32
33 database . collection ( user . uid ) . get ( ) . then ( ( snapshot ) => {
34 let recs = snapshot . map ( doc => Object . assign ( { } , user , doc . data ( ) ) ) ;
35 viewRecodes ( TB , recs ) ;
36 } ) ;
37 } ) ;
38
39
40 function viewRecodes ( root , recs ) {
41 const
42 orign = root . querySelector ( '.template' ) ,
43 template = orign . cloneNode ( true ) ,
44 parent = orign . parentNode ,
45 targets = [ ... template . querySelectorAll ( '*[name]' ) ] ;
46
47 template . classList . remove ( 'template' ) ;
48 template . classList . add ( 'rec' ) ;
49
50 for ( let rec of recs ) {
51 for ( let e of targets ) {
52 if ( rec . hasOwnProperty ( e . name ) )
53 e . value = rec [ e . name ] || '' ;
54 }
55 parent . appendChild ( template . cloneNode ( true ) ) ;
56 }
57 }
58
59 function getRec ( target ) {
60 return [ ... target . querySelectorAll ( '*[name]' ) ] . reduce ( ( a , b ) => ( a [ b . name ] = b . value , a ) , { } ) ;
61 }
62
63 function deleteRec ( rec ) {
64 console . log ( rec ) ;
65 // database.collection (rec.uid).doc (rec.outTimeValue).delete();
66 }
67
68 function updateRec ( rec ) {
69 console . log ( rec ) ;
70 }
71
72 function deleteRow ( target ) {
73 if ( target ) {
74 deleteRec ( getRec ( target ) ) ;
75 target . remove ( ) ;
76 }
77 }
78
79 function updateRow ( target ) {
80 if ( target )
81 updateRec ( getRec ( target ) ) ;
82 }
83
84
85 function handler ( event ) {
86 let e = event . target ;
87 switch ( event . type ) {
88 case 'click' :
89 if ( e . classList . contains ( 'deleteBtn' ) )
90 deleteRow ( e . closest ( '.rec' ) ) ;
91 break ;
92 case 'blur' :
93 updateRow ( e . closest ( '.rec' ) )
94 break ;
95 }
96 }
97
98
99 TB . addEventListener ( 'click' , handler , false ) ;
100 TB . addEventListener ( 'blur' , handler , true ) ;
101
102 < / script >
103
DOM構造を最初から構成するより、テンプレートをコピーして使っています。
編集されたくない場合は readOnly でもつけて切り替えてください
エラーが無いことを祈ります
--
イベントは個別に設定しない
「エリスの胸はパッド入り」のように復唱しましょう!
「イベント監視はお外から」
--
これを試してみる
js
1 firebase . auth ( ) . onAuthStateChanged ( ( user ) => {
2 document . querySelectorAll ( 'h2 ouput.userName' ) [ 0 ] . textContent = user . displayName ;
3
4 database . collection ( user . uid ) . get ( ) . then ( ( snapshot ) => {
5 let recs = [ ] ;
6 snapshot . forEach ( doc => recs . push ( Object . assign ( { } , user , doc . data ( ) ) ) ) ;
7 viewRecodes ( TB , recs ) ;
8 } ) ;
9 } ) ;
試してみる2
id, uid を行に記録した。対応したIDを使って削除してみる
update も、もう可能じゃね?
html
1 < td >
2 < input type = " hidden " name = " uid " >
3 < input type = " hidden " name = " id " >
js
1 firebase . auth ( ) . onAuthStateChanged ( ( user ) => {
2 document . querySelectorAll ( 'h2 ouput.userName' ) [ 0 ] . textContent = user . displayName ;
3
4 database . collection ( user . uid ) . get ( ) . then ( ( snapshot ) => {
5 let recs = [ ] , u = { id : user . uid , displayName : user . displayName } ;
6 snapshot . forEach ( doc => recs . push ( Object . assign ( { } , u , doc . data ( ) ) ) ) ;
7 viewRecodes ( TB , recs ) ;
8 } ) ;
9 } ) ;