经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
antd通过 filterDropdown 自定义--按某天时间搜索
来源:cnblogs  作者:别样青春  时间:2019/8/15 12:11:59  对本文有异议
  1. import React, { Component } from 'react';
  2. import { Table, Input, Button, Icon, DatePicker } from 'antd';
  3. import moment from 'moment';
  4. import Highlighter from 'react-highlight-words';
  5. export default class RpoliceRecord extends Component {
  6. constructor(props) {
  7. super(props);
  8. this.state = {
  9. searchText: '',
  10. }
  11. }
  12. render() {
  13. // 添加搜索
  14. this.getColumnSearchProps = (dataIndex, title) => ({
  15. filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
  16. <div style={{ padding: 8 }}>
  17. <Input
  18. ref={node => {
  19. this.searchInput = node;
  20. }}
  21. placeholder={`搜索 ${title}`}
  22. value={selectedKeys[0]}
  23. onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
  24. onPressEnter={() => this.handleSearch(selectedKeys, confirm)}
  25. style={{ width: 188, marginBottom: 8, display: 'block' }}
  26. />
  27. <Button
  28. type="primary"
  29. onClick={() => this.handleSearch(selectedKeys, confirm)}
  30. icon="search"
  31. size="small"
  32. style={{ width: 90, marginRight: 8 }}>
  33. 搜索
  34. </Button>
  35. <Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 90 }}>重置</Button>
  36. </div>
  37. ),
  38. filterIcon: filtered => (
  39. <Icon type="search" style={{ color: filtered ? '#1890ff' : undefined }} />
  40. ),
  41. onFilter: (value, record) =>
  42. record[dataIndex]
  43. .toString()
  44. .toLowerCase()
  45. .includes(value.toLowerCase()),
  46. onFilterDropdownVisibleChange: visible => {
  47. if (visible) {
  48. setTimeout(() => this.searchInput.select());
  49. }
  50. },
  51. render: text => (
  52. <Highlighter
  53. highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
  54. searchWords={[this.state.searchText]}
  55. autoEscape
  56. textToHighlight={text.toString()}
  57. />
  58. ),
  59. });
  60. //搜索
  61. this.handleSearch = (selectedKeys, confirm) => {
  62. confirm();
  63. console.log(selectedKeys,confirm);
  64. this.setState({ searchText: selectedKeys[0] });
  65. };
  66. this.handleSearchtime = (selectedKeys, confirm) => {
  67. confirm();
  68. this.setState({ searchText: selectedKeys });
  69. };
  70. //重置
  71. this.handleReset = clearFilters => {
  72. clearFilters();
  73. this.setState({ searchText: '' });
  74. };
  75. const columns = [
  76. { title: '报警时间', dataIndex: 'time', key: 'time',
  77. filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
  78. <div style={{ padding: 8 }}>
  79. <DatePicker placeholder={`搜索时间`}
  80. value={selectedKeys[0]}
  81. onChange={dateString => setSelectedKeys(dateString ? [dateString] : [])}
  82. onPressEnter={() => this.handleSearch(selectedKeys, confirm)}
  83. style={{ width: 188, marginBottom: 8, display: 'block' }}/>
  84. <Button
  85. type="primary"
  86. onClick={() => this.handleSearchtime(moment(selectedKeys[0]._d).format('YYYY-MM-DD'), confirm)}
  87. icon="search"
  88. size="small"
  89. style={{ width: 90, marginRight: 8 }}>
  90. 搜索
  91. </Button>
  92. <Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 90 }}>重置</Button>
  93. </div>
  94. ),
  95. filterIcon: filtered => (
  96. <Icon type="search" style={{ color: filtered ? '#1890ff' : undefined }} />
  97. ),
  98. onFilter: (value, record) => {
  99. return record.time.indexOf(moment(value).format('YYYY-MM-DD')) != -1},
  100. render: text => (
  101. <Highlighter
  102. highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
  103. searchWords={[this.state.searchText]}
  104. autoEscape
  105. textToHighlight={text.toString()}
  106. />
  107. ),
  108. },
  109. { title: '来电', key: 'callnum', dataIndex: 'callnum', ...this.getColumnSearchProps('callnum', '来电'), },
  110. { title: '时长', key: 'longtime', dataIndex: 'longtime', }
  111. ];
  112. const data = [
  113. { key: '1', time: '2019-07-30 16:31:05', callnum: '13546540218', longtime: '37秒' },
  114. { key: '2', time: '2019-06-24 22:08:05', callnum: '13546540218', longtime: '1分12秒' },
  115. { key: '3', time: '2017-08-15 12:31:05', callnum: '13546540218', longtime: '1分10秒' },
  116. { key: '4', time: '2016-12-30 06:15:00', callnum: '13546540218', longtime: '20秒' }
  117. ];
  118. return (
  119. <Table className="accidentTable" columns={columns} dataSource={data} bordered size="small" />
  120. )
  121. }
  122. }

原文链接:http://www.cnblogs.com/takenotes-MUZI/p/11337926.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号