teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

質問の内容を変更しました。

2020/12/21 08:38

投稿

soso0programmer
soso0programmer

スコア35

title CHANGED
File without changes
body CHANGED
@@ -1,117 +1,217 @@
1
- 現在、axiosを使ってJSONデータを取得して、その内容を一覧でテーブルで表示することまではできました。
2
- 次に、Material UIライブラリを使ってテーブルにページネーションを追加ようと考えています。
1
+ 現在、Material UIライブラリのドキュメントに載っているページネーションを使ったテーブルを作成たいと考えています。
3
- ただ、ドキュメントを見ても現在自分で作ったもののどこを修正すればドキュメント通りになるかわからない状況です。
4
- やり方がわかる方がいれば、ご教示お願いいたします。
5
- 参考サイト:https://material-ui.com/components/tables/
2
+ 具体的には、https://material-ui.com/components/tables/ に載っているCustom pagination actionsを実装したいです。
6
- 上記サイトのCustom pagination actionsを適用させたいのですがうまくいきませんでした。
7
- ろしくお願いいたします。
3
+ 下記の画像のうなものです。
8
- ```js
9
- import axios from 'axios'
4
+ ![イメージ説明](cfcbc76d34e61dc21be10696813d2bd3.png)
10
5
 
6
+ PagenationTableComponent.jsにCheckList.jsにあるようにaxiosで渡ってきたjsonデータを表示したいのですが、下記のようなエラーが出てしまいます。
7
+ このrows.sliceが機能していないというのがどうすれば修正できるかご教示お願いします。
8
+ jsonデータは以下のようなものです。
9
+ [{"listNo":1,
10
+ "saiyouDate":"2008-10-06 ",
11
+ "softwareName":"Symantec Endpoint Protection",
12
+ "version":"‐",
13
+ "shubetu":"有償",
14
+ "licenseManage":"○",
15
+ "youto":"ウイルス対策",
16
+ "bikou":"使用する場合はシステム管理まで連絡が必要",
17
+ "authorizer":"山田",
18
+ "approvalDate":"2008-10-06 ",
11
- const CHECKLIST_REST_API_URL = 'http://localhost:8080/api/checkList';
19
+ "url":"https://jp.broadcom.com/products/cyber-security/endpoint/end-user"}]
12
20
 
21
+ TypeError: rows.slice is not a function
13
- class CheckListService {
22
+ CustomPaginationActionsTable
23
+ C:/workspace/spring-backend-3/frontend/src/component/PagenationTableComponent.js:117
24
+ 114 | return (
25
+ 115 |
26
+ 116 |
14
27
 
28
+ 117 |
29
+ | ^ 118 | {(rowsPerPage > 0
30
+ 119 | ? rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
31
+ 120 | : rows
15
- getList() {
32
+ View compiled
16
- return axios.get(CHECKLIST_REST_API_URL);
33
+ 17 stack frames were collapsed.
17
- }
18
- }
19
34
 
20
-
21
- export default new CheckListService();
35
+ PagenationTableComponent.js
22
- ```
23
36
  ```js
24
37
  import React from 'react';
38
+ import PropTypes from 'prop-types';
25
- import CheckListService from '../services/CheckList';
39
+ import { makeStyles, useTheme } from '@material-ui/core/styles';
26
40
  import Table from '@material-ui/core/Table';
27
41
  import TableBody from '@material-ui/core/TableBody';
28
42
  import TableCell from '@material-ui/core/TableCell';
29
43
  import TableContainer from '@material-ui/core/TableContainer';
30
- import TableHead from '@material-ui/core/TableHead';
44
+ import TableFooter from '@material-ui/core/TableFooter';
45
+ import TablePagination from '@material-ui/core/TablePagination';
31
46
  import TableRow from '@material-ui/core/TableRow';
32
47
  import Paper from '@material-ui/core/Paper';
33
- import CssBaseline from '@material-ui/core/CssBaseline';
34
- import Container from '@material-ui/core/Container';
48
+ import IconButton from '@material-ui/core/IconButton';
49
+ import FirstPageIcon from '@material-ui/icons/FirstPage';
50
+ import KeyboardArrowLeft from '@material-ui/icons/KeyboardArrowLeft';
51
+ import KeyboardArrowRight from '@material-ui/icons/KeyboardArrowRight';
35
- import { makeStyles } from '@material-ui/core/styles';
52
+ import LastPageIcon from '@material-ui/icons/LastPage';
53
+ import CheckListService from '../services/CheckList';
36
54
 
37
- class CheckListComponent extends React.Component {
55
+ const useStyles1 = makeStyles((theme) => ({
38
- constructor(props) {
39
- super(props)
40
- this.state = {
41
- list: []
56
+ root: {
57
+ flexShrink: 0,
58
+ marginLeft: theme.spacing(2.5),
42
- }
59
+ },
43
- }
60
+ }));
44
61
 
45
- componentDidMount() {
46
- CheckListService.getList().then((response) => {
62
+ function TablePaginationActions(props) {
63
+ const classes = useStyles1();
47
- this.setState({ list: response.data })
64
+ const theme = useTheme();
65
+ const { count, page, rowsPerPage, onChangePage } = props;
48
66
 
67
+ const handleFirstPageButtonClick = (event) => {
68
+ onChangePage(event, 0);
49
- });
69
+ };
50
- }
51
70
 
71
+ const handleBackButtonClick = (event) => {
72
+ onChangePage(event, page - 1);
73
+ };
52
74
 
75
+ const handleNextButtonClick = (event) => {
76
+ onChangePage(event, page + 1);
77
+ };
53
78
 
79
+ const handleLastPageButtonClick = (event) => {
80
+ onChangePage(event, Math.max(0, Math.ceil(count / rowsPerPage) - 1));
81
+ };
54
82
 
55
- render() {
83
+ return (
84
+ <div className={classes.root}>
85
+ <IconButton
86
+ onClick={handleFirstPageButtonClick}
87
+ disabled={page === 0}
88
+ aria-label="first page"
89
+ >
90
+ {theme.direction === 'rtl' ? <LastPageIcon /> : <FirstPageIcon />}
91
+ </IconButton>
92
+ <IconButton onClick={handleBackButtonClick} disabled={page === 0} aria-label="previous page">
93
+ {theme.direction === 'rtl' ? <KeyboardArrowRight /> : <KeyboardArrowLeft />}
94
+ </IconButton>
95
+ <IconButton
96
+ onClick={handleNextButtonClick}
97
+ disabled={page >= Math.ceil(count / rowsPerPage) - 1}
98
+ aria-label="next page"
99
+ >
100
+ {theme.direction === 'rtl' ? <KeyboardArrowLeft /> : <KeyboardArrowRight />}
101
+ </IconButton>
102
+ <IconButton
103
+ onClick={handleLastPageButtonClick}
104
+ disabled={page >= Math.ceil(count / rowsPerPage) - 1}
105
+ aria-label="last page"
106
+ >
107
+ {theme.direction === 'rtl' ? <FirstPageIcon /> : <LastPageIcon />}
108
+ </IconButton>
109
+ </div>
110
+ );
111
+ }
56
112
 
113
+ TablePaginationActions.propTypes = {
114
+ count: PropTypes.number.isRequired,
115
+ onChangePage: PropTypes.func.isRequired,
116
+ page: PropTypes.number.isRequired,
117
+ rowsPerPage: PropTypes.number.isRequired,
118
+ };
57
119
 
120
+ let rows = [];
121
+ rows = CheckListService.getList().then((response) => {
122
+ return response.data
58
123
 
124
+ });
59
125
 
60
- return (
61
- <React.Fragment>
62
- <CssBaseline />
63
- <Container maxWidth="lg" className="mt-5">
64
- <h1>使用許可ソフトウェアリスト</h1>
65
- <TableContainer component={Paper}>
66
- <Table className="table text-nowrap" size="small" aria-label="a dense table">
67
- <TableHead>
68
- <TableRow>
69
- <TableCell>リスト番号</TableCell>
70
- <TableCell align="right">採用日</TableCell>
71
- <TableCell align="right">ソフトウェア名</TableCell>
72
- <TableCell align="right">バージョン</TableCell>
73
- <TableCell align="right">種別</TableCell>
74
- <TableCell align="right">ライセンス</TableCell>
75
- <TableCell align="right">用途</TableCell>
76
- <TableCell align="right">備考</TableCell>
77
- <TableCell align="right">承認者</TableCell>
78
- <TableCell align="right">承認日</TableCell>
79
- <TableCell align="right">URL</TableCell>
80
- </TableRow>
81
- </TableHead>
82
- <TableBody>
83
- {this.state.list.map((
84
- list => {
85
- return <TableRow key={list.listNo}>
86
- <TableCell component="th" scope="row">
87
- {list.listNo}
88
- </TableCell>
89
- <TableCell align="right">{list.saiyouDate}</TableCell>
90
- <TableCell align="right">{list.softwareName}</TableCell>
91
- <TableCell align="right">{list.version}</TableCell>
92
- <TableCell align="right">{list.shubetu}</TableCell>
93
- <TableCell align="right">{list.licenseManage}</TableCell>
94
- <TableCell align="right">{list.youto}</TableCell>
95
- <TableCell align="right">{list.bikou}</TableCell>
96
- <TableCell align="right">{list.authorizer}</TableCell>
97
- <TableCell align="right">{list.approvalDate}</TableCell>
98
- <TableCell align="right">{list.url}</TableCell>
99
- </TableRow>;
100
- }
101
- ))}
102
- </TableBody>
103
- </Table>
104
- </TableContainer>
105
- </Container>
106
- </React.Fragment>
107
126
 
108
127
 
128
+ const useStyles2 = makeStyles({
129
+ table: {
130
+ minWidth: 500,
131
+ },
132
+ });
109
133
 
134
+ export default function CustomPaginationActionsTable() {
110
- )
135
+ const classes = useStyles2();
111
- }
136
+ const [page, setPage] = React.useState(0);
137
+ const [rowsPerPage, setRowsPerPage] = React.useState(5);
112
138
 
139
+ const emptyRows = rowsPerPage - Math.min(rowsPerPage, rows.length - page * rowsPerPage);
113
140
 
141
+ const handleChangePage = (event, newPage) => {
142
+ setPage(newPage);
143
+ };
144
+
145
+ const handleChangeRowsPerPage = (event) => {
146
+ setRowsPerPage(parseInt(event.target.value, 10));
147
+ setPage(0);
148
+ };
149
+
150
+ return (
151
+ <TableContainer component={Paper}>
152
+ <Table className={classes.table} aria-label="custom pagination table">
153
+ <TableBody>
154
+ {(rowsPerPage > 0
155
+ ? rows.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage)
156
+ : rows
157
+ ).map((row) => (
158
+ <TableRow key={row.listNo}>
159
+ <TableCell component="th" scope="row">
160
+ {row.listNo}
161
+ </TableCell>
162
+ <TableCell style={{ width: 160 }} align="right">
163
+ {row.saiyouDate}
164
+ </TableCell>
165
+ <TableCell style={{ width: 160 }} align="right">
166
+ {row.softwareName}
167
+ </TableCell>
168
+ </TableRow>
169
+ ))}
170
+
171
+ {emptyRows > 0 && (
172
+ <TableRow style={{ height: 53 * emptyRows }}>
173
+ <TableCell colSpan={6} />
174
+ </TableRow>
175
+ )}
176
+ </TableBody>
177
+ <TableFooter>
178
+ <TableRow>
179
+ <TablePagination
180
+ rowsPerPageOptions={[5, 10, 25, { label: 'All', value: -1 }]}
181
+ colSpan={3}
182
+ count={rows.length}
183
+ rowsPerPage={rowsPerPage}
184
+ page={page}
185
+ SelectProps={{
186
+ inputProps: { 'aria-label': 'rows per page' },
187
+ native: true,
188
+ }}
189
+ onChangePage={handleChangePage}
190
+ onChangeRowsPerPage={handleChangeRowsPerPage}
191
+ ActionsComponent={TablePaginationActions}
192
+ />
193
+ </TableRow>
194
+ </TableFooter>
195
+ </Table>
196
+ </TableContainer>
197
+ );
114
198
  }
115
199
 
200
+
201
+ ```
202
+ CheckList.js
203
+ ```js
204
+ import axios from 'axios'
205
+
206
+ const CHECKLIST_REST_API_URL = 'http://localhost:8080/api/users';
207
+
208
+ class CheckListService {
209
+
210
+ getList() {
211
+ return axios.get(CHECKLIST_REST_API_URL);
212
+ }
213
+ }
214
+
215
+
116
- export default CheckListComponent
216
+ export default new CheckListService();
117
217
  ```