-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2_20_2025_react.js
50 lines (47 loc) · 1.53 KB
/
2_20_2025_react.js
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
import { useState } from 'react';
import users from './data/users.json';
export default function DataTable() {
const [message, setMessage] = useState('Data Table');
const [startIdx, setStartIdx] = useState(0);
const [numItemsToRender, setNumItemsToRender] = useState(10)
const adjustIdx = (direction) => {
if (direction == 'lower') {
setStartIdx(Math.max(0, startIdx - numItemsToRender))
} else if (direction == 'raise') {
setStartIdx(Math.min(users.length - 1, startIdx + numItemsToRender))
}
}
return (
<div>
<h1>{message}</h1>
<button onClick={() => adjustIdx('lower')}> Prev </button>
<button onClick={() => adjustIdx('raise')}> Next </button>
{startIdx}
<input type='number' value={numItemsToRender} onChange={(e)=> setNumItemsToRender(Number(e.target.value))}/>
<table>
<thead>
<tr>
{[
{ label: 'ID', key: 'id' },
{ label: 'Name', key: 'name' },
{ label: 'Age', key: 'age' },
{ label: 'Occupation', key: 'occupation' },
].map(({ label, key }) => (
<th key={key}>{label}</th>
))}
</tr>
</thead>
<tbody>
{users.slice(startIdx, startIdx + numItemsToRender).map(({ id, name, age, occupation }) => (
<tr key={id}>
<td>{id}</td>
<td>{name}</td>
<td>{age}</td>
<td>{occupation}</td>
</tr>
))}
</tbody>
</table>
</div>
);
}