简体   繁体   English

如何在单击时清除按钮上的日期范围选择器值?

[英]how to clear date range picker values on Button on click?

This is my code and I have date range picker and a clear button.这是我的代码,我有日期范围选择器和一个清除按钮。 when I click on the clear button the state get changed(changed to null values) and I want to show it in the UI.当我单击清除按钮时,状态会发生变化(更改为空值)并且我想在 UI 中显示它。 how can I set the value of date range picker to show it ?如何设置日期范围选择器的值来显示它?

const [para, _setPara] = useState({
  startDate: null,
  endDate:null
} as any);

const onChange =(date:any, dateString:any) =>{
    setPara({
      startDate:date[0].format("YYYY-MM-DD HH:mm:ss"),
      endDate: date[1].format("YYYY-MM-DD HH:mm:ss")
    })
}

const clearSearch =()=>{
  setPara({
    startDate: null,
    endDate:null
  })
}

return(
<RangePicker onChange={onChange} allowClear={false} value={?} />
<Button
   type="primary"
   onClick={() => clearSearch()}
   danger
>
)

The value you get in the onChange is in the same format as the component expects it, so you either need to keep it in the state as it is and convert to string when you need it, or you keep the string in the state and convert to the correct format on each render.您在onChange获得的值与组件期望的格式相同,因此您需要将其保持原样并在需要时转换为字符串,或者将字符串保持在状态中并进行转换到每个渲染的正确格式。 You can pass the values in an array您可以传递数组中的值

<RangePicker onChange={onChange} allowClear={false} value={[moment1,moment2]} />

To create a moment from string you need to import moment and call it with the date format you're using:要从字符串创建时刻,您需要导入moment并使用您正在使用的日期格式调用它:

import moment from 'moment';
...
const moment1=moment(startDate,"YYYY-MM-DD HH:mm:ss")

to clear just set the both moments to null清除只是将两个时刻设置为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM