-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathTable.js
37 lines (32 loc) · 791 Bytes
/
Table.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
import React from 'react';
const formatHeader = ({key, label}, sorting) => (sorting==key)?('+'+label):(
(sorting=='-'+key)?('-'+label):label
)
export default (props) => {
const headers = props.cols.map(col => <th key={col.key}>
{col.sorting?<a href='#' onClick={e => {
e.preventDefault();
col.sorting()
}}>
{formatHeader(col, props.sorting)}
</a>:col.label
}
</th>)
const rows = props.rows.map(row => <tr key={row.id}>
{
props.cols.map(col => <td key={col.key}>
{(col.format?col.format(row):row[col.key])}
</td>)
}
</tr>)
return <table>
<thead>
<tr>
{headers}
</tr>
</thead>
<tbody>
{rows}
</tbody>
</table>
}