antd通过 filterDropdown 自定义按某天时间搜索功能

所属分类: 网络编程 / 其它综合 阅读数: 1240
收藏 0 赞 0 分享

import React, { Component } from 'react';
import { Table, Input, Button, Icon, DatePicker } from 'antd';
import moment from 'moment';
import Highlighter from 'react-highlight-words';
export default class RpoliceRecord extends Component {
 constructor(props) {
  super(props);
  this.state = {
   searchText: '',
  }
 }
 render() {
  // 添加搜索
  this.getColumnSearchProps = (dataIndex, title) => ({
   filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
    <div style={{ padding: 8 }}>
     <Input
      ref={node => {
       this.searchInput = node;
      }}
      placeholder={`搜索 ${title}`}
      value={selectedKeys[0]}
      onChange={e => setSelectedKeys(e.target.value ? [e.target.value] : [])}
      onPressEnter={() => this.handleSearch(selectedKeys, confirm)}
      style={{ width: 188, marginBottom: 8, display: 'block' }}
     /> 
     <Button
      type="primary"
      onClick={() => this.handleSearch(selectedKeys, confirm)}
      icon="search"
      size="small"
      style={{ width: 90, marginRight: 8 }}>
      搜索
     </Button>
     <Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 90 }}>重置</Button>
    </div>
   ),
   filterIcon: filtered => (
    <Icon type="search" style={{ color: filtered ? '#1890ff' : undefined }} />
   ),
   onFilter: (value, record) =>
    record[dataIndex]
     .toString()
     .toLowerCase()
     .includes(value.toLowerCase()),
   onFilterDropdownVisibleChange: visible => {
    if (visible) {
     setTimeout(() => this.searchInput.select());
    }
   },
   render: text => (
    <Highlighter
     highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
     searchWords={[this.state.searchText]}
     autoEscape
     textToHighlight={text.toString()}
    />
   ),
  });
  //搜索
  this.handleSearch = (selectedKeys, confirm) => {
   confirm();
   console.log(selectedKeys,confirm);
   this.setState({ searchText: selectedKeys[0] });
  };
  this.handleSearchtime = (selectedKeys, confirm) => {
   confirm();
   this.setState({ searchText: selectedKeys });
  };
  //重置
  this.handleReset = clearFilters => {
   clearFilters();
   this.setState({ searchText: '' });
  };
  const columns = [
   { title: '报警时间', dataIndex: 'time', key: 'time',
   filterDropdown: ({ setSelectedKeys, selectedKeys, confirm, clearFilters }) => (
    <div style={{ padding: 8 }}>
      <DatePicker placeholder={`搜索时间`}
      value={selectedKeys[0]}
      onChange={dateString => setSelectedKeys(dateString ? [dateString] : [])}
      onPressEnter={() => this.handleSearch(selectedKeys, confirm)}
      style={{ width: 188, marginBottom: 8, display: 'block' }}/>
     <Button
      type="primary"
      onClick={() => this.handleSearchtime(moment(selectedKeys[0]._d).format('YYYY-MM-DD'), confirm)}
      icon="search"
      size="small"
      style={{ width: 90, marginRight: 8 }}>
      搜索
     </Button>
     <Button onClick={() => this.handleReset(clearFilters)} size="small" style={{ width: 90 }}>重置</Button>
    </div>
   ),
   filterIcon: filtered => (
    <Icon type="search" style={{ color: filtered ? '#1890ff' : undefined }} />
   ),
   onFilter: (value, record) => {
    return record.time.indexOf(moment(value).format('YYYY-MM-DD')) != -1},
   render: text => (
    <Highlighter
     highlightStyle={{ backgroundColor: '#ffc069', padding: 0 }}
     searchWords={[this.state.searchText]}
     autoEscape
     textToHighlight={text.toString()}
    />
   ),
   },
   { title: '来电', key: 'callnum', dataIndex: 'callnum', ...this.getColumnSearchProps('callnum', '来电'), },
   { title: '时长', key: 'longtime', dataIndex: 'longtime', }
  ];
  const data = [
   { key: '1', time: '2019-07-30 16:31:05', callnum: '13546540218', longtime: '37秒' },
   { key: '2', time: '2019-06-24 22:08:05', callnum: '13546540218', longtime: '1分12秒' },
   { key: '3', time: '2017-08-15 12:31:05', callnum: '13546540218', longtime: '1分10秒' },
   { key: '4', time: '2016-12-30 06:15:00', callnum: '13546540218', longtime: '20秒' }
  ];
  return (
    <Table className="accidentTable" columns={columns} dataSource={data} bordered size="small" />
  )
 }
}

以上所述是小编给大家介绍的antd通过 filterDropdown 自定义按某天时间搜索功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

更多精彩内容其他人还在看

软件测试面试如何测试一个杯子

本文主要介绍软件测试面试如何测试一个杯子,这里帮大家整理了详细的面试资料,和面试需要准备的知识点,有兴趣的小伙伴可以参考下
收藏 0 赞 0 分享

软件测试面试如何测试网页的登录页面

本文主要介绍软件测试面试如何测试网页的登录页面,这里整理了相关软件测试的一些基本知识,希望能帮助软件测试的同学
收藏 0 赞 0 分享

常见前端面试题及答案

本文是在GitHub上看到一个大牛总结的前端常见面试题,很多问题问的都很好,很经典、很有代表性。上面没有答案,我就整理了一下,从网上找了一些相关问题的答案
收藏 0 赞 0 分享

PHP和Java的主要区别有哪些?哪个最适合Web开发语言?

Java和PHP都是编程语言,大家知道它们最大的区别就是一个是静态语言一个是动态语言吧。没错,Java是一种静态语言,PHP是一种动态语言。那它们还有哪些区别? 哪个最适合Web开发语言?下面,小编再给大家详细介绍下。
收藏 0 赞 0 分享

玩转markdown 分享几个需要用到的工具

markdown是一个面向写作的语法引擎,markdown的最终目的都是解析成html用于网页浏览,所以它兼容html语法,即你可以在 markdown文档中使用原生的html标签
收藏 0 赞 0 分享

可能是最通俗的一篇介绍markdown的文章

这些日子一直在简书上使用markdown写作,已经渐渐的痴迷于这种简洁纯粹的写作方式了。不过就我逐渐入门markdown的写作过程来看,目前我看到的各种介绍markdown写作方式的文章都还略显极客,对于大多数像我一样没有基础的普通人来说,可能内容上的可接受性没有那么强
收藏 0 赞 0 分享

献给写作者的 Markdown 新手指南

Markdown 是一种「电子邮件」风格的「标记语言」,我们强烈推荐所有写作者学习和掌握该语言。为什么
收藏 0 赞 0 分享

github pull最新代码实现方法

本文主要介绍 github pull最新代码的资料,这里对 github pull最新代码做了详细流程介绍,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

GitHub Eclipse配置使用教程详解

本文主要介绍GitHub Eclipse,这里对Eclipse 使用GitHub的教程,图文并茂详细说明如何操作,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

Git 教程简单入门介绍

本文主要介绍Git 教程简单入门的东西,这里整理了Git 的基础资料和简单命令,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多