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
| import React, { useState } from "react"; import { Table, Card, Select, Input, Space } from "antd"; import { getBooks } from "../books"; import "antd/dist/antd.css"; const { Option } = Select; const { Search } = Input;
const columns = [ { title: "書名", dataIndex: "title", key: "title", }, { title: "價格", dataIndex: "price", key: "price", sorter: true, }, { title: "分類", dataIndex: "category", key: "category", }, { title: "出版日期", dataIndex: "publishedAt", key: "publishedAt", }, ];
const DEFAULT_PAGINATION = { position: ["bottomCenter"], pageSize: 2, pageSizeOptions: [2, 4, 6], showSizeChanger: true, };
function App() { const [pagination, setPagination] = useState(DEFAULT_PAGINATION); const [category, setCategory] = useState("0"); const [keyword, setKeyword] = useState(""); const [sort, setSort] = useState(""); const [filter, setFilter] = useState({ category, keyword, sort, }); const books = getBooks(filter);
function handleChangeCategory(value) { setKeyword(""); setCategory(value); setFilter({ ...filter, category: value }); setPagination({ ...pagination, current: 1 }); } function handleChangeKeyword(e) { setKeyword(e.target.value); } function handelSearchKeyword(value) { setCategory("0"); setFilter({ ...filter, keyword: keyword }); setPagination({ ...pagination, current: 1 }); } function handelChange(_pagination, filters, sorter, extra) { if (extra.action === "paginate") { setPagination(_pagination); } else if (extra.action === "sort") { setSort(sorter.order); setFilter({ ...filter, sort: sorter.order }); setPagination({ ...pagination, current: 1 }); }
} return ( <div> <Card style={{ margin: 10 }}> <Space> <Select placeholder="書籍分類" defaultValue="0" value={category} onChange={handleChangeCategory} > <Option value="0">全部分類</Option> <Option value="1">社會科學</Option> <Option value="2">商業理財</Option> </Select> <Search placeholder="搜尋書名、分類" value={keyword} onSearch={handelSearchKeyword} onChange={handleChangeKeyword} /> </Space> <Table dataSource={books} columns={columns} pagination={{ ...pagination }} onChange={handelChange} /> </Card> </div> ); }
export default App;
|