index.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import { Form, Input, Button, Table, Select, Pagination, Space, Menu, Dropdown, Modal, Breadcrumb, message, Row, Col } from 'antd';
  3. import { DownOutlined, PlusOutlined, ExclamationCircleOutlined } from '@ant-design/icons';
  4. import AddDict from './addDict'
  5. import '@common/common.less';
  6. import { useSelector } from 'react-redux'
  7. import apiObj from '@api/index';
  8. import DictContext from './Dict-context';
  9. const { post, api, xPost } = apiObj;
  10. const { Option } = Select;
  11. //获取列表
  12. function DictManager() {
  13. useEffect(() => {
  14. getDictPage();
  15. }, []);
  16. const [DictList, setDictList] = useState([]);//当前页列表数据
  17. const [Dictid, setDictid] = useState([]);//当前列表id
  18. const [title, setTitle] = useState("");//数据总量
  19. const [visible, setVisible] = useState(false);//弹窗
  20. const [msvisible, setMsvisible] = useState(false);//删除的弹窗
  21. const [modalType, setModalType] = useState("");
  22. const [groupTypeList, setgroupTypeList] = useState([]);//代码类别模糊查找
  23. const [type, setType] = useState("");
  24. const [formData, setFormData] = useState(null);//当前行数据
  25. const [size, setSize] = useState(15);//每页显示条数
  26. const [total, setTotal] = useState(0);
  27. const [current, setCurrent] = useState(1);//当前页
  28. const [params, setParams] = useState({
  29. pages: 1,
  30. current: 1,
  31. size: 15
  32. });
  33. const [form] = Form.useForm();
  34. let list = []
  35. let data = {
  36. pages: 1,
  37. current: 1,
  38. size: size
  39. }
  40. //新增弹窗
  41. const showModal = (name, type, flag, Dictrow) => {
  42. setVisible(type);
  43. setTitle(name);
  44. setType(flag)
  45. if (flag == 1) {
  46. setFormData({
  47. status: '1'
  48. })
  49. }
  50. if (flag == 3) {
  51. setFormData(Dictrow)
  52. getDictPage()
  53. }
  54. }
  55. //表格数据
  56. function getDictPage(param) {
  57. post(api.getDictTree, param || params).then((res) => {
  58. if (res.data.code === 200) {
  59. const data = res.data.data;
  60. console.log(data.records)
  61. setDictList(data.records);
  62. setTotal(data.total)
  63. }
  64. })
  65. }
  66. //模糊查询
  67. function getCodeCategory(val){
  68. xPost(api.getCodeCategory,{groupType:val}).then((res) => {
  69. if (res.data.code === 200) {
  70. const data = res.data.data;
  71. setgroupTypeList(data)
  72. }
  73. })
  74. }
  75. function onSearch(value) {
  76. getCodeCategory(value)
  77. }
  78. function onFocus() {
  79. getCodeCategory("")
  80. }
  81. //删除
  82. function deleteDict() {
  83. xPost(api.deleteDict,{id:Dictid}).then((res) => {
  84. if (res.data.code === 200) {
  85. getDictPage();
  86. setMsvisible(false);
  87. setDictid(null)
  88. message.success("删除成功");
  89. } else {
  90. message.warning(res.data.msg || '操作失败');
  91. }
  92. }).catch(() => {
  93. message.error("接口出错");
  94. });
  95. }
  96. //每页显示条数切换
  97. function onSizeChange(current, pageSize) {
  98. params.current = current
  99. params.size = pageSize
  100. setSize(pageSize)
  101. setCurrent(current)
  102. setParams(params)
  103. getDictPage()
  104. }
  105. //翻页
  106. function changePage(page, pageSize) {
  107. params.current = page
  108. params.size = pageSize
  109. setCurrent(page)
  110. setParams(params)
  111. getDictPage()
  112. }
  113. const onFinish = (value) => {
  114. const param = {
  115. ...data,
  116. ...value
  117. }
  118. setCurrent(1)
  119. setParams(param)
  120. getDictPage(param);
  121. };
  122. const onReset = () => {
  123. setCurrent(1)
  124. setParams(data)
  125. form.resetFields();
  126. getDictPage(data);
  127. };
  128. const messageBox = (id) => {
  129. setDictid(id)
  130. setMsvisible(true)
  131. }
  132. //提示框取消
  133. function handleCancel() {
  134. setMsvisible(false);
  135. }
  136. function cancel() {
  137. setVisible(false)
  138. setFormData(null)
  139. }
  140. function DictChange() {
  141. setVisible(false)
  142. getDictPage();
  143. }
  144. const columns = [
  145. { title: '代码类别', dataIndex: 'groupType', key: 'index' },
  146. { title: '字典编码', dataIndex: 'val', key: 'index' },
  147. { title: '代码名称', dataIndex: 'name', key: 'index' },
  148. { title: '字典说明', dataIndex: 'remark', key: 'index' },
  149. { title: '状态', dataIndex: 'status', key: 'status'},
  150. {
  151. title: '操作', dataIndex: 'key', render: (text, record) => (
  152. <Space size="middle">
  153. <a onClick={e => showModal('修改字典', true, 3, record)}>修改</a>
  154. <a onClick={e => messageBox(record.id)}>删除</a>
  155. </Space>
  156. )
  157. }
  158. ]
  159. return (
  160. <div className="wrapper">
  161. <div className="filter-box">
  162. <Form
  163. form={form}
  164. name="normal_login"
  165. onFinish={onFinish}
  166. initialValues={{ status: '' }}
  167. >
  168. <Row gutter={24}>
  169. <Col span={5} key={0}>
  170. <Form.Item id="groupTypeval" label="代码类别" name="groupType">
  171. <Select
  172. showSearch
  173. optionFilterProp="children"
  174. onSearch={onSearch}
  175. onFocus={onFocus}
  176. >
  177. {groupTypeList.map((item) => {
  178. return (
  179. <Option value={item} key={item}>{item}</Option>
  180. )
  181. })}
  182. </Select>
  183. </Form.Item>
  184. </Col>
  185. <Col span={5} key={1}>
  186. <Form.Item label="代码名称" name="name">
  187. <Input placeholder="代码名称" autoComplete='off'/>
  188. </Form.Item>
  189. </Col>
  190. <Col span={6} key={3}>
  191. <Form.Item>
  192. <Button type="primary" htmlType="submit">
  193. 查询
  194. </Button>
  195. <Button onClick={onReset}>
  196. 重置
  197. </Button>
  198. </Form.Item>
  199. </Col>
  200. </Row>
  201. </Form>
  202. </div>
  203. <div className="table">
  204. <div className="table-header">
  205. <h2 className="table-title">字典管理</h2>
  206. <Button type="primary" icon={<PlusOutlined />} onClick={e => showModal('新增字典', true, 1)}>新增字典</Button>
  207. </div>
  208. <Table
  209. columns={columns}
  210. scroll={{ y: 'calc(100vh - 320px)' }}
  211. dataSource={DictList}
  212. rowKey={record => record.id}
  213. pagination={{
  214. current: current,
  215. pageSize: size,
  216. size: 'small',
  217. showSizeChanger: true,
  218. pageSizeOptions: ['15', '30', '60', '120'],
  219. showTotal: (total, range) => `第${range[0]}-${range[1]} 条/共 ${total} 条数据`,
  220. onShowSizeChange: (current, pageSize) => onSizeChange(current, pageSize), // 改变每页数量时更新显示
  221. onChange: (page, pageSize) => changePage(page, pageSize),//点击页码事件
  222. total: total
  223. }} />
  224. </div>
  225. {visible && formData ?
  226. <Modal
  227. title={title}
  228. okText='确定'
  229. cancelText='取消'
  230. width={'45%'}
  231. visible={visible}
  232. onCancel={cancel}
  233. footer={null}
  234. forceRender={true}
  235. >
  236. <DictContext.Provider value={{type, formData}}>
  237. <AddDict DictChange={DictChange} />
  238. </DictContext.Provider>
  239. </Modal>
  240. : ''}
  241. <Modal
  242. title="提示"
  243. okText='确定'
  244. cancelText='取消'
  245. width={400}
  246. visible={msvisible}
  247. onOk={deleteDict}
  248. onCancel={handleCancel}
  249. >
  250. <p>确定要删除该字典?</p>
  251. </Modal>
  252. </div >
  253. )
  254. }
  255. export default DictManager;