我有一个 Material-UI 的 <Table>
,并且在每个 <TableRow>
(动态呈现)为 <TableBody>
,我想为其中一列设置一个按钮 ( <FlatButton>
)。一旦点击按钮,它就会打开一个<Dialog>
。在它里面想要一个工作<Tabs>
.
那么我怎样才能显示 <FlatButton>
对于特定列的每一行,当单击按钮时,显示 <Dialog>
连同工作 <Tabs>
在里面作为内容?并且有 <Dialog>
在外面点击时关闭?
到目前为止,我有以下内容,但遇到了以下问题:打开但速度很慢并且在 <Dialog>
之外点击没有关闭它,<Tabs>
可见但不工作:
主表:
import React, { Component } from 'react'
import {
Subheader,
Table,
TableBody,
TableHeader,
TableHeaderColumn,
TableRow,
} from 'material-ui'
import RenderedTableRow from ‘./RenderedTableRow'
export default class MainTable extends Component {
constructor() {
super()
}
render() {
return (
<div>
<div>
<Subheader>Table</Subheader>
<Table
multiSelectable={true}
>
<TableHeader
displaySelectAll={true}
enableSelectAll={true}
>
<TableRow>
<TableHeaderColumn>
Col 1
</TableHeaderColumn>
<TableHeaderColumn>
Col 2
</TableHeaderColumn>
<TableHeaderColumn>
Col 3
</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody
deselectOnClickaway={false}
stripedRows
>
<RenderedTableRow {...this.props}/>
</TableBody>
</Table>
</div>
</div>
)
}
}
渲染表行:
import React, { Component } from 'react'
import { Dialog, FlatButton, Tabs, Tab, TableRow, TableRowColumn } from 'material-ui'
import ContentAdd from 'material-ui/svg-icons/content/add';
export default class RenderedTableRow extends Component {
constructor(props) {
super(props)
this.state = {
open: false,
}
this._handleOpen = this._handleOpen.bind(this)
this._handleClose = this._handleClose.bind(this)
}
_handleOpen() {
this.setState({
open: true
})
}
_handleClose() {
this.setState({
open: false
})
}
render() {
const {
children,
...rest
} = this.props
const actions = [
<FlatButton
label="Cancel"
primary={true}
onClick={this._handleClose}
/>,
]
return (
<TableRow {...rest}>
{children[0]}
<TableRowColumn>Red</TableRowColumn>
<TableRowColumn>John, Joshua</TableRowColumn>
<TableRowColumn>
<FlatButton
icon={<ContentAdd/>}
onClick={this._handleOpen}
/>
</TableRowColumn>
<Dialog
actions={actions}
autoScrollBodyContent={true}
open={this.state.open}
onRequestClose={this._handleClose}
modal={false}
title='Test'
>
<Tabs>
<Tab label="Item One" >
<div>
<h2 >Tab One</h2>
<p>
This is an example tab.
</p>
</div>
</Tab>
<Tab label="Item Two" >
<div>
<h2>Tab Two</h2>
<p>
This is another example tab.
</p>
</div>
</Tab>
</Tabs>
</Dialog>
</TableRow>
)
}
}
预先感谢您并将接受/赞成答案。
最佳答案
MainTable
组件中的整个表格可能只有一个对话框。这样效率更高,因为您不需要每行一个对话框,而只需要一个对话框。
为了让 RenderedTableRow
中的按钮打开模式并告诉它选择了哪一行,您需要将回调函数从 MainTable
传递到 RenderedTableRow
调用时设置要打开的对话框并存储选择的行:
export default class MainTable extends Component {
state = {
selectedRow: null,
}
handleSelectRow(rowIndex) {
this.setState({
selectedRow: rowIndex,
})
}
render() {
return (
<div>
<div>
<Subheader>Table</Subheader>
<Table
multiSelectable={true}
>
// ...
<TableBody
deselectOnClickaway={false}
stripedRows
>
{rows.map((row, index) => (
<RenderedTableRow
row={row}
{...this.props}
onSelectRow={() => this.handleSelectRow(index)}
/>
))}
</TableBody>
</Table>
</div>
// Dialog goes here and is only rendered once per table
// it is only open when there is a row selected
<Dialog
open={Boolean(this.state.selectedRow)}
>
// you can get the selected row with rows[this.state.selectedRow]
</Dialog>
</div>
)
}
}
关于javascript - ReactJS + Material-UI : How to use Material-UI’s FlatButton and Dialog in each TableRow?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41711621/