-
Notifications
You must be signed in to change notification settings - Fork 0
/
datagridasapplication.html
166 lines (165 loc) · 4.8 KB
/
datagridasapplication.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<html><head><title>Grid with keyboard navigation</title>
<script>
let keyboardhandler = function (event, table, row, rowCount, columnCount) {
let currentTarget = event.currentTarget;
let ariaRowindex = row.ariaRowIndex;
let ariaColindex = currentTarget.ariaColIndex;
//let rows =
if (event.keyCode == 40) {
//console.log('down');
if (Number(ariaRowindex)+1 < 21) {
//if ((Number(aria-rowindex)+1) < rowCount) {
let downRow = table.querySelector('[aria-rowindex = "'+(Number(ariaRowindex)+1)+'"]');
let downCell = downRow.querySelector('[aria-colindex = "'+ariaColindex+'"]');
downCell.setAttribute('tabindex', 0);
downCell.focus();
downCell.scrollIntoView(true); // Top
currentTarget.setAttribute('tabindex', -1);
}
} else if (event.keyCode == 38) {
if ((Number(ariaRowindex)-1) > 0) {
let upRow = table.querySelector('[aria-rowindex = "'+(Number(ariaRowindex)-1)+'"]');
let upCell = upRow.querySelector('[aria-colindex = "'+(Number(ariaColindex))+'"]');
upCell.setAttribute('tabindex', 0);
upCell.focus();
upCell.scrollIntoView(true); // Top
currentTarget.setAttribute('tabindex', -1);
}
//console.log('up');
} else if (event.keyCode == 39) {
if (Number(ariaColindex)+1 < columnCount) {
let nextCell = row.querySelector('[aria-colindex = "'+(Number(ariaColindex)+1)+'"]');
nextCell.setAttribute('tabindex', 0);
nextCell.focus();
currentTarget.setAttribute('tabindex', -1);
}
//console.log('right');
} else if (event.keyCode == 37) {
if (Number(ariaColindex)-1 > -1) {
let prevCell = row.querySelector('[aria-colindex = "'+(Number(ariaColindex)-1)+'"]');
prevCell.setAttribute('tabindex', 0);
prevCell.focus();
currentTarget.setAttribute('tabindex', -1);
}
//console.log('left');
}
}
window.onload = function() {
let table = document.querySelector('table, [role="grid"]');
let rows = table.querySelectorAll('tr, [role="row"]');
rows.forEach(row => {
let ariaColindex = 0;
let columns = row.querySelectorAll('th, [role="columheader"], [role="rowheader"], td, [role="gridcell"]');
columns.forEach(th => {
row.setAttribute('aria-rowindex', row.rowIndex+1);
th.setAttribute('aria-colindex', ariaColindex++);
th.addEventListener('keyup', event => {
keyboardhandler(event, table, row, rows.length, columns.length);
});
});
});
}
</script>
<style>
th[role=rowheader] {
font-weight: normal;
text-align: start;
}
</style>
</head><body>
<h1 id="demo2label1">Student grades</h1>
<table role="application" tabindex="-1" id="table1" aria-labelledby="demo2label1" border="1">
<tr>
<th tabindex="0" >Student Name</th>
<th tabindex="-1" >Biology</th>
<th tabindex="-1" >Chemistry</th>
<th tabindex="-1" >Physics</th>
<th tabindex="-1" role="columnheader">History</th>
</tr>
<tr>
<th tabindex="-1" >John Doe</th>
<td tabindex="-1">A</td>
<td tabindex="-1" >A+</td>
<td tabindex="-1" >B</td>
<td tabindex="-1" >B</td>
</tr>
<tr>
<th tabindex="-1" >Miriam Luther</th>
<td tabindex="-1" >A</td>
<td tabindex="-1" >A</td>
<td tabindex="-1" >C</td>
<td tabindex="-1" >B</td>
</tr>
<tr>
<th tabindex="-1">Arthur Long</th>
<td tabindex="-1" >A</td>
<td tabindex="-1">A</td>
<td tabindex="-1" >C</td>
<td tabindex="-1">B</td>
</tr>
<tr>
<th tabindex="-1">Jill</th>
<td tabindex="-1" >A</td>
<td tabindex="-1">A+</td>
<td tabindex="-1">B</td>
<td tabindex="-1">B</td>
</tr>
<tr>
<th tabindex="-1">Joe</th>
<td tabindex="-1">A</td>
<td tabindex="-1">A</td>
<td tabindex="-1" >C</td>
<td tabindex="-1">B</td>
</tr>
<tr>
<th tabindex="-1" >Sarah</th>
<td tabindex="-1">A</td>
<td tabindex="-1">A</td>
<td tabindex="-1">C</td>
<td tabindex="-1">B</td>
</tr>
<tr>
<th tabindex="-1" >Nathan</th>
<td tabindex="-1">A</td>
<td tabindex="-1">A+</td>
<td tabindex="-1">B</td>
<td tabindex="-1">B</td>
</tr>
<tr>
<th tabindex="-1" >Nick</th>
<td tabindex="-1">A</td>
<td tabindex="-1" >A</td>
<td tabindex="-1">C</td>
<td tabindex="-1" >B</td>
</tr>
<tr>
<th tabindex="-1">Keren</th>
<td tabindex="-1">A</td>
<td tabindex="-1">A</td>
<td tabindex="-1">C</td>
<td tabindex="-1">B</td>
</tr>
<tr>
<th tabindex="-1">Theresa</th>
<td tabindex="-1">A</td>
<td tabindex="-1" >A+</td>
<td tabindex="-1">B</td>
<td tabindex="-1" >B</td>
</tr>
<tr>
<th tabindex="-1">Gabby</th>
<td tabindex="-1" >A</td>
<td tabindex="-1">A</td>
<td tabindex="-1">C</td>
<td tabindex="-1">B</td>
</tr>
<tr>
<th tabindex="-1">Hunter</th>
<td tabindex="-1" >A</td>
<td tabindex="-1">A</td>
<td tabindex="-1">C</td>
<td tabindex="-1">B</td>
</tr>
</table>
</body>
</html>