我有一個材料UI表分量,並且我添加結帳按鈕。 一切工作正常,除了兩件事情我不明白:陣營 - 的onClick丟失「這個」
當onClick
初始化handleCheckout()
功能,爲什麼handleRowSelection()
和被激發呢?
最重要的是,當onClick
來電handleCheckout()
,它損失的this
上下文,表明我this.state.selected is []
。
export default class PredictionsTable extends Component {
constructor(props) {
super(props);
this.state = {
selected: [],
tableData: [],
checkout: false
}
}
componentWillMount(){
axios.get('/dashboard')
.then((response) => {
this.setState({
tableData: response.data,
});
})
.catch((error)=> {
console.log(error);
});
}
isSelected = (index) => {
return this.state.selected.indexOf(index) !== -1;
};
handleCheckout = (event) => {
這裏的 「本」 丟失,並且它不顯示已選擇
console.log(this.state.selected);
};
handleRowSelection = (selectedRows) => {
this.setState({
selected: selectedRows,
},function(){
});
};
render() {
return (
<div className="table">
<RaisedButton label="Reveal my Forecasts" onClick={this.handleCheckout}
labelStyle={{color: greenA700}}
buttonStyle={{borderColor: greenA700}}/>
<Table
height={"300px"}
fixedHeader={true}
fixedFooter={false}
multiSelectable={true}
onRowSelection={this.handleRowSelection}
>
<TableHeader
displaySelectAll={true}
adjustForCheckbox={true}
enableSelectAll={false}
>
<TableRow style={{background: '#e9eaea'}}>
<TableHeaderColumn colSpan="6" style={{textAlign: 'center' , paddingRight: "90px"}}>
</TableHeaderColumn>
</TableRow>
<TableRow>
<TableHeaderColumn tooltip="The forecast coin name">Altcoin Name</TableHeaderColumn>
<TableHeaderColumn tooltip="The value of the coin according to forecast time">Current
Value</TableHeaderColumn>
<TableHeaderColumn tooltip="Upper limit of investment">Profit Sell Order</TableHeaderColumn>
<TableHeaderColumn tooltip="Lower limit of investment">Loss Sell Order</TableHeaderColumn>
<TableHeaderColumn tooltip="Date and time the forecast was generated">Date and
time</TableHeaderColumn>
<TableHeaderColumn
tooltip="Share information about the forecast">Comments</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody
showRowHover={true}
>
{this.state.tableData.map((row, index) => (
<TableRow key={index} selected={this.isSelected(index)}>
<TableRowColumn>{row.coin_name}</TableRowColumn>
<TableRowColumn>{row.coin_value}</TableRowColumn>
<TableRowColumn style={{paddingLeft: 50}}>{row.profitSellOrder}</TableRowColumn>
<TableRowColumn style={{paddingLeft: 50}}>{row.lossSellOrder}</TableRowColumn>
<TableRowColumn>{row.ptime}</TableRowColumn>
//comments
<TableRowColumn>{row.lossSellOrder}</TableRowColumn>
</TableRow>
))}
</TableBody>
</Table>
</div>
)
}
}
你能描述你如何做事情的情景? –
表格組件具有複選框和屬性「onRowSelection」,每次標記複選框時都會觸發handleRowSelection()。處理程序更新選定的狀態數組。我試着當用戶點擊一個按鈕,結賬沒有成功獲取所有選定的複選框。 – ori1212
這是功能究竟應該如何工作。 –