[英]How to remove React-bootstrap popover onClick?
I am rendering a table inside popover, which is triggered by an event in another React class. 我在popover中渲染一个表,该表由另一个React类中的事件触发。
The table is correctly rendered and shown. 该表已正确呈现并显示。 I do however want for the user to be able to remove it again, with a button in the popover.
但是,我确实希望用户能够再次使用弹出窗口中的按钮将其删除。
I think I'm on the right path, but right now, nothing happens when the user clicks the Button. 我认为我走在正确的道路上,但是现在,当用户单击按钮时,什么也没有发生。 It will start out as false and when it's rendered, return true;
它将以false开始,并在渲染时返回true;否则,它将返回true。 but how do I actually hide the
popover
? 但是我实际上如何隐藏
popover
?
Sample code: 样例代码:
let Sample = React.createClass({
getInitialState : function () {
return{
showTable: false,
data: [],
selectedOption: this.selectedOption,
};
},
onClick: function() {
this.setState({ showTable: false });
},
loadAjax : function // An ajax call
// In here we will do --> this.setState({ showTable: true });
renderTable // Table content rendered here
render : function () {
let tableData = this.state.data;
if (tableData && this.state.selectedOption) {
return (
<Popover className="styling-table"
id="popover-trigger-focus"
title={this.state.selectedOption}
ref="popover">
<Button onClick={this.onClick} />
<Table striped bordered condensed hover>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
</thead>
<tbody>
{tableData.map(this.renderTable)}
</tbody>
</Table>
</Popover>
)
}
else {
return <div></div>
}
}
});
Add showTable
to your if
condition in your render
function: 将
showTable
添加到render
函数的if
条件中:
render : function () {
let tableData = this.state.data,
showTable = this.state.showTable;
if (showTable && (tableData && this.state.selectedOption)) {
// show Popup
}
else {
// show empty div
}
}
This way, when you click the <Button>
, this.state.showTable
, your component will re-render and then show the correct output. 这样,当您单击
<Button>
, this.state.showTable
,您的组件将重新呈现,然后显示正确的输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.