antd通过 filterDropdown 自定义--按某天时间搜索
程序员文章站
2023-01-22 08:11:32
import React, { Component } from 'react'; import { Table, Input, Button, Icon, DatePicker } from 'antd'; import moment from 'moment'; import Highlight ......
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" /> ) } }
下一篇: 安卓开发之事件处理机制