admin管理员组文章数量:1323715
I use Date Picker ponent from Material-UI for React JS. I want to show the selected date on the table. A date is an object and I have an error when trying to show in a table row. How to do this?
import React, { Component } from 'react';
import DatePicker from 'material-ui/DatePicker';
import { Table, TableBody, TableHeader, TableHeaderColumn, TableRow, TableRowColumn } from 'material-ui/Table';
export default class AddTaskDialog extends Component {
constructor(props) {
super(props);
this.state = { controlledDate: {} };
this.handleChangeDate = this.handleChangeDate.bind(this);
}
handleChangeDate = (event, date) => {
this.setState({
controlledDate: date,
});
};
render() {
return (
<div>
<DatePicker hintText="Date" value={this.state.controlledDate} onChange={this.handleChangeDate}/>
<Table>
<TableHeader>
<TableRow>
<TableHeaderColumn>Date</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableRowColumn>{this.state.controlledDate}</TableRowColumn>
</TableRow>
</TableBody>
</Table>
</div>
);
}
}
I use Date Picker ponent from Material-UI for React JS. I want to show the selected date on the table. A date is an object and I have an error when trying to show in a table row. How to do this?
import React, { Component } from 'react';
import DatePicker from 'material-ui/DatePicker';
import { Table, TableBody, TableHeader, TableHeaderColumn, TableRow, TableRowColumn } from 'material-ui/Table';
export default class AddTaskDialog extends Component {
constructor(props) {
super(props);
this.state = { controlledDate: {} };
this.handleChangeDate = this.handleChangeDate.bind(this);
}
handleChangeDate = (event, date) => {
this.setState({
controlledDate: date,
});
};
render() {
return (
<div>
<DatePicker hintText="Date" value={this.state.controlledDate} onChange={this.handleChangeDate}/>
<Table>
<TableHeader>
<TableRow>
<TableHeaderColumn>Date</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableRowColumn>{this.state.controlledDate}</TableRowColumn>
</TableRow>
</TableBody>
</Table>
</div>
);
}
}
Share
Improve this question
edited Dec 18, 2017 at 9:14
meditat
1,16516 silver badges35 bronze badges
asked Dec 18, 2017 at 8:18
ItalikItalik
6963 gold badges20 silver badges37 bronze badges
3 Answers
Reset to default 3The date that is sent to the handleChangeDate
handler is of type object
. You need to convert it to a date string in order to render inside the TableRowColumn
.
export default class AddTaskDialog extends Component {
constructor(props) {
super(props);
this.state = { controlledDate: new Date() };
this.handleChangeDate = this.handleChangeDate.bind(this);
}
handleChangeDate = (event, date) => {
this.setState({
controlledDate: date
});
};
// INSIDE RENDER
<TableRowColumn>{this.state.controlledDate.toDateString()}</TableRowColumn>
const date = new Date();
console.log(typeof date);
console.log(date.toDateString());
{this.state.controlledDate}
not {this.controlledDate}
You are missing the state in the TableRow.
constructor(props) {
super(props);
this.state = { controlledDate: '' };
this.handleChangeDate = this.handleChangeDate.bind(this);
}
...
render() {
...
<TableRow>
TableRowColumn>{this.state.controlledDate || New Date(Date.now())}</TableRowColumn>
</TableRow>
}
本文标签: javascriptHow to get date from Date Picker and show in the table MaterialUI ReactJSStack Overflow
版权声明:本文标题:javascript - How to get date from Date Picker and show in the table? Material-UI. ReactJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742126024a2421944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论