我目前正在使用ReactJS + Material-UI,使用Material-UI的
那么有什么方法可以任意指定
<Table>
时,列宽度会根据内容自动设置。目前看起来所有列都是等宽的,但我想让某些列比其他列宽一些。那么有什么方法可以任意指定
<TableRow>
的列宽,并仍然基于内容进行动态调整吗?<Table>
时,列宽度会根据内容自动设置。目前看起来所有列都是等宽的,但我想让某些列比其他列宽一些。<TableRow>
的列宽,并仍然基于内容进行动态调整吗?const {
Table,
TableHeader,
TableHeaderColumn,
TableBody,
TableRow,
TableRowColumn,
MuiThemeProvider,
getMuiTheme,
} = MaterialUI;
class Example extends React.Component {
render() {
const customColumnStyle = { width: 12, backgroundColor: 'yellow' };
return (
<div>
<Table>
<TableHeader>
<TableRow>
<TableHeaderColumn>A</TableHeaderColumn>
<TableHeaderColumn style={customColumnStyle}>B</TableHeaderColumn>
<TableHeaderColumn>C</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableRowColumn>1</TableRowColumn>
<TableRowColumn style={customColumnStyle}>2</TableRowColumn>
<TableRowColumn>3</TableRowColumn>
</TableRow>
<TableRow>
<TableRowColumn>4</TableRowColumn>
<TableRowColumn style={customColumnStyle}>5</TableRowColumn>
<TableRowColumn>6</TableRowColumn>
</TableRow>
<TableRow>
<TableRowColumn>7</TableRowColumn>
<TableRowColumn style={customColumnStyle}>8</TableRowColumn>
<TableRowColumn>9</TableRowColumn>
</TableRow>
</TableBody>
</Table>
</div>
);
}
}
const App = () => (
<MuiThemeProvider muiTheme={getMuiTheme()}>
<Example />
</MuiThemeProvider>
);
ReactDOM.render(
<App />,
document.getElementById('container')
);
<Table>
组件中有一个隐藏的属性,使其表现类似于HTML的<table>
元素,即根据内容自适应列宽:
<Table style={{ tableLayout: 'auto' }} fixedHeader={false} ...>
...
</Table>
虽然不能分别样式每一列,但至少默认情况下对于少量内容的大型列来说不太丑陋。
...
而且,通过这样做,你可以得到一个具有无限列的滚动表格...componentDidMount() {
let tbody = $(this.refs.table)
if (tbody && tbody.length == 1) {
let div = tbody[0].refs.tableDiv
div.style.overflowX = 'auto'
div.parentElement.style.overflowX = 'hidden'
} else {
// error handling
}
}