当前位置:   article > 正文

React antd组件Table的使用_react中table组件的用法

react中table组件的用法

常用功能:搜索、下拉搜索、批量删除、时间筛选、添加按钮、页码跳转  查询和重置按钮

1、搜索功能

一般是Form表单结合Input两个一起使用,使用里面的Form表单的

表单的回填,使用就是利用下面这个方法回填

  1. const [Form] = Form.useForm();//表单添加这个属性
  2. Form.setFieldsValue({
  3. 字段名:值
  4. });

回填还要配合着initialValues默认值,第一次进来就渲染,

  1. <Form form={topForm} initialValues={formValues}>
  2. 内容
  3. <Form/>

当有Modal使用的时候,有确认按钮时,要使用validateFields来获取Modal的值

  1. const handleOK = () => {
  2. //获取提交的值
  3. addForm
  4. .validateFields()
  5. .then((values) => {
  6. const { externalUserId = '', addReason = '' } = values;
  7. onConfirm({
  8. externalUserId,
  9. addReason
  10. });
  11. })
  12. .catch((err) => {
  13. console.log(err);
  14. });
  15. };

也可以使用表单的getFieldsValue来获取

  1. const onOk = async () => {
  2. const { name, filterName, deptList, staffTagList } = form.getFieldsValue();
  3. const param = { name, filterName, deptList: deptList?.map((item: number) => ({ deptId: item })), staffTagList };
  4. const res = await requestAddGroupTag(param);
  5. if (res) {
  6. setAddTagParam({ visible: false });
  7. form.resetFields();
  8. updateList?.();
  9. }
  10. };

具体操作如下:有Modal弹出的使用

  1. const saveAutoConfig = () => {
  2. autoConfigForm
  3. .validateFields()
  4. .then(async ({ weekChainRatio, autoSend }) => {
  5. const res = await savePointsSendConfig({ weekChainRatio, autoSend: autoSend ? 1 : 0 }); //获取到Modal的参数,把参数传入进去,进行网络请求
  6. if (res) {
  7. setAutoSendSetVisible(false);
  8. message.success('设置成功');
  9. }
  10. })
  11. .catch((e) => {
  12. console.log(e);
  13. });
  14. };
  15. <Modal
  16. title="标题"
  17. visible={autoSendSetVisible}
  18. onOk={saveAutoConfig}//提交字段的信息
  19. width={400}
  20. onCancel={() => {
  21. setAutoSendSetVisible(false);//关闭弹窗
  22. }}
  23. >
  24. <Form className={style.autoSendForm} labelCol={{ span: 6 }} form={autoConfigForm}>
  25. <Form.Item
  26. label="周环比"
  27. name="weekChainRatio"
  28. rules={[{ required: true, message: '请设置周环比必须大于0' }]}
  29. >
  30. <InputNumber
  31. width={80}
  32. min={1}
  33. max={10000}
  34. controls={false}
  35. addonAfter={<span>%</span>}
  36. onChange={(value) => {
  37. console.log(typeof value);
  38. }}
  39. />
  40. </Form.Item>
  41. <Form.Item label="自动发放" valuePropName="checked" name={'autoSend'}>
  42. <Switch checkedChildren="开" unCheckedChildren="关" />
  43. </Form.Item>
  44. </Form>
  45. </Modal>

取消按钮

  1. const handleCancel = () => {
  2. displayForm.resetFields();
  3. onCancel();
  4. };

  1. import { Button, Drawer, Form, Input, Divider, Space, Avatar, List, PaginationProps } from 'antd';
  2. import React, { useState, useEffect } from 'react';
  3. import { getChatSearchList } from 'src/apis/orgManage';
  4. import moment from 'moment';
  5. import style from './style.module.less';
  6. interface CreateDrawerProps {
  7. visible: boolean;
  8. onClose: () => void;
  9. value?: any;
  10. chatProposalId?: string;
  11. }
  12. interface ChatListProps {
  13. avatar: string;
  14. content: string;
  15. dateCreated: string;
  16. dynamicId: string;
  17. name: string;
  18. source: string;
  19. staffName: string;
  20. }
  21. const CreateDrawer: React.FC<CreateDrawerProps> = ({ visible, onClose, value, chatProposalId }) => {
  22. const [list, setList] = useState<ChatListProps[]>([]);
  23. const [loading, setLoading] = useState(true);
  24. const [pagination, setPagination] = useState<PaginationProps>({
  25. current: 1,
  26. pageSize: 10,
  27. total: 0,
  28. showTotal: (total) => {
  29. return `共 ${total} 条记录`;
  30. }
  31. });
  32. const { Item } = Form;
  33. const [form] = Form.useForm();
  34. // 获取列表数据
  35. const getList = async (param: any) => {
  36. setLoading(true);
  37. const pageNum = param?.pageNum || pagination.current;
  38. const pageSize = param?.pageSize || pagination.pageSize;
  39. const params: any = {
  40. ...param,
  41. proposalId: chatProposalId,
  42. pageNum,
  43. pageSize
  44. };
  45. const res: any = await getChatSearchList({ ...params });
  46. if (res) {
  47. const { list, total } = res;
  48. setList(list || []);
  49. setPagination((pagination) => ({ ...pagination, current: pageNum, pageSize, total }));
  50. }
  51. setLoading(false);
  52. };
  53. const onSearch = (values: any) => {
  54. const param: any = {
  55. ...values,
  56. pageNum: 1,
  57. pageSize: 10
  58. };
  59. getList(param);
  60. };
  61. const paginationChange = (pageNum: number, pageSize?: number) => {
  62. getList({ pageNum, pageSize });
  63. };
  64. const showDrawer = () => {
  65. onClose();
  66. setPagination((pagination) => ({ ...pagination, current: 1, pageSize: 10 }));
  67. form.resetFields();
  68. };
  69. useEffect(() => {
  70. if (chatProposalId) {
  71. getList({ chatProposalId });
  72. }
  73. }, [chatProposalId, visible]);
  74. return (
  75. <>
  76. <Drawer
  77. title={`${moment(value?.dateCreated).format('YYYYMM-DD')}
  78. 经理${value?.staffName}${value?.externalName}的详细沟通记录`}
  79. placement="right"
  80. width={466}
  81. visible={visible}
  82. closable={true}
  83. onClose={showDrawer}
  84. >
  85. <div className={style.chatBox}>
  86. <div>
  87. <div className={style.chatTitle}>沟通内容</div>
  88. <div className={style.chatText}>
  89. <span className={style.chatWord}>车牌号:</span>
  90. <span className={style.chatNum}>{value?.carNumber}</span>
  91. <span className={style.chatWord} style={{ marginLeft: '8px' }}>
  92. 创建时间:
  93. </span>
  94. <span className={style.chatNumTime}>{value?.dateCreated}</span>
  95. </div>
  96. <div className={style.chatFootText}>{value?.content}</div>
  97. </div>
  98. <Divider />
  99. <div>
  100. <div className={style.chatTitle}>沟通记录</div>
  101. <div className={style.formBox}>
  102. <Form form={form} onFinish={onSearch}>
  103. <Space>
  104. <Item label="关键词" name={'sontent'}>
  105. <Input type="text" placeholder="请输入" allowClear />
  106. </Item>
  107. <Item>
  108. <Button type="primary" htmlType="submit" shape="round">
  109. 查询
  110. </Button>
  111. </Item>
  112. </Space>
  113. </Form>
  114. </div>
  115. <div className={style.formFootBox}>
  116. <List
  117. itemLayout="horizontal"
  118. loading={loading}
  119. dataSource={list}
  120. pagination={{
  121. ...pagination,
  122. hideOnSinglePage: true,
  123. onChange: paginationChange
  124. }}
  125. renderItem={(item) => (
  126. <List.Item key={item.dynamicId}>
  127. <List.Item.Meta
  128. avatar={<Avatar src={item.avatar} />}
  129. title={
  130. <>
  131. <div className={style.chatName}>
  132. <Space>
  133. <div className={style.chatName}>{item.name}</div>
  134. {item.source
  135. ? (
  136. <div className={style.chatcard1}>客户经理</div>
  137. )
  138. : (
  139. <div className={style.chatcard2}>客户</div>
  140. )}
  141. <div className={style.chatTime}>{item.dateCreated}</div>
  142. </Space>
  143. </div>
  144. </>
  145. }
  146. description={item.content}
  147. />
  148. </List.Item>
  149. )}
  150. />
  151. </div>
  152. </div>
  153. </div>
  154. </Drawer>
  155. </>
  156. );
  157. };
  158. export default CreateDrawer;
  159. import { PaginationProps, Divider, PageHeader, Drawer } from 'antd';
  160. import React, { useEffect, useState } from 'react';
  161. import { RouteComponentProps } from 'react-router-dom';
  162. import { getChatList } from 'src/apis/orgManage';
  163. import { AuthBtn, NgFormSearch, NgTable } from 'src/components';
  164. import { useDocumentTitle } from 'src/utils/base';
  165. import CreateDrawer from '../Components/CreateDrawer';
  166. import { SceneColumns, searchCols, tableColumnsFun } from './ListConfig';
  167. import { OperateType } from 'src/utils/interface';
  168. import ChatLog from 'src/pages/Exception/DeletionReminder/ChatLog/ChatLog';
  169. interface SearchParamsProps {
  170. carNumber: string;
  171. externalName: string;
  172. staffIds: string;
  173. startTime: string;
  174. endTime: string;
  175. }
  176. const ChatRecordList: React.FC<RouteComponentProps> = () => {
  177. const [tableSource, setTableSource] = useState<Partial<SceneColumns>[]>([]);
  178. const [visible, setVisible] = useState(false);
  179. const [visibleList, setVisibleList] = useState(false);
  180. const [chatValue, setChatValue] = useState<any>();
  181. const [drawerValue, setdrawerValue] = useState<any>();
  182. const [userId, setUserId] = useState<string>();
  183. const [externalUserId, setExternalUserId] = useState<string>();
  184. const [loading, setLoading] = useState(true);
  185. const [chatProposalId, setChatProposalId] = useState<string>();
  186. const [queryParams, setQueryParams] = useState<SearchParamsProps>({
  187. carNumber: '',
  188. externalName: '',
  189. staffIds: '',
  190. startTime: '',
  191. endTime: ''
  192. });
  193. useDocumentTitle('合规管理-聊天记录查询');
  194. const [pagination, setPagination] = useState<PaginationProps>({
  195. current: 1,
  196. pageSize: 10,
  197. total: 0,
  198. showTotal: (total) => {
  199. return `共 ${total} 条记录`;
  200. }
  201. });
  202. const getList = async (params?: any) => {
  203. setLoading(true);
  204. const pageNum = params?.pageNum || pagination.current;
  205. const pageSize = params?.pageSize || pagination.pageSize;
  206. const res: any = await getChatList({
  207. ...queryParams,
  208. ...params,
  209. pageNum,
  210. pageSize
  211. });
  212. if (res) {
  213. const { list, total } = res;
  214. setTableSource(list || []);
  215. setPagination((pagination) => ({ ...pagination, total, current: pageNum, pageSize }));
  216. }
  217. setLoading(false);
  218. };
  219. useEffect(() => {
  220. getList();
  221. }, []);
  222. const onSearch = (values: any) => {
  223. const { carNumber, externalName, staffList, rangePicker } = values;
  224. console.log(values, '--------------------values');
  225. const staffIds = staffList?.map((mapItem: { staffId: string }) => mapItem.staffId);
  226. let startTime = '';
  227. let endTime = '';
  228. if (rangePicker && rangePicker.length > 0) {
  229. startTime = rangePicker[0].format('YYYY-MM-DD HH:mm:ss');
  230. endTime = rangePicker[1].format('YYYY-MM-DD HH:mm:ss');
  231. }
  232. getList({ carNumber, externalName, staffIds, startTime, endTime, pageNum: 1 });
  233. setQueryParams((queryParams) => ({ ...queryParams, carNumber, externalName, staffIds, startTime, endTime }));
  234. };
  235. const paginationChange = (pageNum: number, pageSize?: number) => {
  236. getList({ pageNum, pageSize });
  237. };
  238. // 查询场景详情
  239. const chatDetail = async (operateType: OperateType, record: SceneColumns) => {
  240. if (operateType === 'view') {
  241. setChatProposalId(record.proposalId);
  242. setVisible(true);
  243. setChatValue({ ...record });
  244. } else if (operateType === 'edit') {
  245. setVisibleList(true);
  246. setUserId(record.userId);
  247. setExternalUserId(record.externalUserId);
  248. setdrawerValue({ ...record });
  249. }
  250. };
  251. return (
  252. <div className={'container'}>
  253. <PageHeader title="聊天记录查询" style={{ padding: '0' }}></PageHeader>
  254. <Divider style={{ marginTop: '21px' }} />
  255. <AuthBtn path="/query">
  256. <NgFormSearch searchCols={searchCols} isInline firstRowChildCount={3} onSearch={onSearch} />
  257. </AuthBtn>
  258. <div className="mt20">
  259. <NgTable
  260. columns={tableColumnsFun({
  261. onOperate: chatDetail
  262. })}
  263. loading={loading}
  264. dataSource={tableSource}
  265. pagination={pagination}
  266. paginationChange={paginationChange}
  267. setRowKey={(record: SceneColumns) => {
  268. return record.proposalId;
  269. }}
  270. />
  271. </div>
  272. <CreateDrawer
  273. visible={visible}
  274. value={chatValue}
  275. chatProposalId={chatProposalId}
  276. onClose={() => {
  277. setVisible(false);
  278. }}
  279. />
  280. <Drawer
  281. width={1200}
  282. visible={visibleList}
  283. onClose={() => {
  284. setVisibleList(false);
  285. }}
  286. >
  287. <ChatLog userId={userId} externalUserId={externalUserId} showDrawer={visibleList} drawerValue={drawerValue} />
  288. </Drawer>
  289. </div>
  290. );
  291. };
  292. export default ChatRecordList;
  293. import React from 'react';
  294. import { Button, Space, Form } from 'antd';
  295. import { ColumnsType } from 'antd/lib/table';
  296. import { SearchCol } from 'src/components/SearchComponent/SearchComponent';
  297. import { AuthBtn } from 'src/components';
  298. import { SelectStaff /* , TagModal */ } from 'src/pages/StaffManage/components';
  299. import { OperateType } from 'src/utils/interface';
  300. export const searchCols: SearchCol[] = [
  301. {
  302. name: 'carNumber',
  303. type: 'input',
  304. label: '号码',
  305. width: '160px',
  306. placeholder: '请输入'
  307. },
  308. {
  309. name: 'externalName',
  310. type: 'input',
  311. label: '名称',
  312. placeholder: '请输入',
  313. width: '160px'
  314. },
  315. {
  316. name: 'rangePicker',
  317. type: 'rangePicker',
  318. label: '时间',
  319. width: '160px'
  320. },
  321. {
  322. name: 'staffName',
  323. type: 'custom',
  324. width: '160px',
  325. label: '客户',
  326. customNode: (
  327. <Form.Item key={'staffList'} name="staffList" label="客户">
  328. <SelectStaff key={1} type="staff" />
  329. </Form.Item>
  330. )
  331. }
  332. ];
  333. export interface SceneColumns {
  334. proposalId: string;
  335. staffId: string;
  336. sceneName: string;
  337. carNumber: string;
  338. externalName: string;
  339. dateCreated: string;
  340. externalUserId: string;
  341. userId: string;
  342. staffName: string;
  343. isChatsyn: number;
  344. }
  345. interface OperateProps {
  346. onOperate: (operateType: OperateType, proposalId: SceneColumns) => void;
  347. }
  348. export const tableColumnsFun = (args: OperateProps): ColumnsType<SceneColumns> => {
  349. return [
  350. { title: '编号', dataIndex: 'proposalId', key: 'proposalId', width: 200 },
  351. {
  352. title: '客户',
  353. dataIndex: 'staffName',
  354. key: 'staffId',
  355. width: 200
  356. },
  357. {
  358. title: '客户',
  359. dataIndex: 'externalName',
  360. width: 260,
  361. align: 'center'
  362. },
  363. {
  364. title: '号码',
  365. dataIndex: 'carNumber',
  366. width: 260,
  367. align: 'center'
  368. },
  369. {
  370. title: '时间',
  371. dataIndex: 'dateCreated',
  372. width: 260,
  373. align: 'center'
  374. },
  375. {
  376. title: '操作',
  377. dataIndex: 'fromSource',
  378. width: 260,
  379. align: 'center',
  380. render: (value, record) => {
  381. return (
  382. <Space>
  383. <AuthBtn path="/view">
  384. <Button type="link" key={record.proposalId} onClick={() => args.onOperate('view', record)}>
  385. 查看
  386. </Button>
  387. </AuthBtn>
  388. <AuthBtn path="/query">
  389. <Button
  390. type="link"
  391. key={record.proposalId}
  392. disabled={record.isChatsyn === 0}
  393. onClick={() => args.onOperate('edit', record)}
  394. >
  395. 查看其他
  396. </Button>
  397. </AuthBtn>
  398. </Space>
  399. );
  400. }
  401. }
  402. ];
  403. };

表单的查看不能修改:readOnly

例如:

<Input type="text" placeholder="请输入" readOnly={true} />

单独使用Input和Button实现输入框

  1. const [inputValue, setInputValue] = useState<any>(null);
  2. const [queryParams, setQueryParams] = useState({
  3. name: '',
  4. typeId: '',
  5. pageNum: 1,
  6. pageSize: 12
  7. });
  8. const onChange = (e: any) => {
  9. setValue(e.target.value);
  10. };
  11. const inputChange = (e: any) => {
  12. console.log(e.target.value, '1111111111e');
  13. setInputValue(e.target.value);
  14. };
  15. const onSearch = () => {
  16. // setLoading(true);
  17. setData([]);
  18. setHasMore(true);
  19. const postData = {
  20. ...queryParams,
  21. name: inputValue,
  22. pageNum: 1
  23. };
  24. setQueryParams((queryParams) => ({ ...queryParams, name: inputValue, pageNum: 1 }));
  25. getList(postData);
  26. };
  27. <Input
  28. className={style.searchWrap}
  29. placeholder="请输入"
  30. allowClear
  31. size="large"
  32. value={inputValue}
  33. onChange={inputChange}
  34. style={{ width: 772 }}
  35. onPressEnter={() => onSearch()}
  36. suffix={suffix}
  37. />
  38. <Button type="primary" className={style.searchBtn} onClick={onSearch} shape="round" size="large">
  39. 搜索
  40. </Button>

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号