简体   繁体   English

如何从Material Ui获取Date Picker的值

[英]How to get value of Date Picker from Material Ui

If I created a datepicker with Material Ui, how do I get the user's input value? 如果我使用Material Ui创建了一个datepicker,我如何获得用户的输入值?

Say the user selects 01.01.2016-> how do i grab that value and store it as a variable with data type date? 假设用户选择01.01.2016->我如何获取该值并将其存储为具有数据类型日期的变量?

So far what I've tried to do is use states to pass back a JSON object but that doesnt seem to be working. 到目前为止,我尝试做的是使用状态来传回一个JSON对象,但这似乎并没有起作用。 For example: 例如:

getInitialState: function() {
    return {
      startDate: null,
      endDate:null    
    };
  },

render: function() {
return (
      <div>
          <DatePicker hintText="Start Date" value={this.state.startDate}  onChange={this._handleStartInput} />
          <DatePicker hintText="End Date" value={this.state.endDate} onChange={this._handleEndInput} />
      </div>
);},

_handleStartInput: function(value) {
    this.setState({
      startDate: value
    });
  },

  _handleEndInput: function(value) {
    this.setState({
      endDate: value
    });
  },

And then from there I can create another function that pulls the value, but right now after I select a date, the state never changes and is displayed in the UI. 然后从那里我可以创建另一个拉取值的函数,但是在我选择日期之后,状态永远不会改变并显示在UI中。

I tried this and I paint the console value 我试过这个,然后绘制控制台值

<DatePicker  container="inline" floatingLabelText="Fecha desde" onChange={(x, event) => this.setFechaDesde(x,event)}    defaultDate={new Date()} />

and

setFechaDesde(x,event){
        console.log(JSON.stringify(event));
        console.log(JSON.stringify(x));
    }

Which version of Material-UI are you using ? 您使用的是哪个版本的Material-UI?

By reading the doc you see that 通过阅读文档,你会看到

onChange Callback function that is fired when the date value changes. 日期值更改时触发的onChange回调函数。 Since there is no particular event associated with the change the first argument will always be null and the second argument will be the new Date instance. 由于没有与更改关联的特定事件,因此第一个参数将始终为null,第二个参数将是新的Date实例。

You should try adding a second param to your callbacks. 您应该尝试在回调中添加第二个参数。

Your onChange handler functions should take two parameters: event and date, where event is always null and date is the new date object. 你的onChange处理函数应该有两个参数:event和date,其中event始终为null,date是新的date对象。

_handleStartInput: function(event, date) {
    var currentState = this.state;
    currentState.startDate = date;
    this.setState(currentState);
},

_handleEndInput: function(event, date) {
    var currentState = this.state;
    currentState.endDate = date;
    this.setState(currentState);
}

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

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