Dan*_* G. 3 reactjs react-admin
在列数很少的列表视图中,列非常宽。我想限制至少其中一些列的宽度(最后一列除外):
仍在努力加快 react-admin、react 和 material-ui 的速度。我确定其中涉及一些样式。有人可以指出我正确的方向吗?谢谢。
更新:我按照 Jozef 的建议添加了样式,但没有更改。下面是它在 Inspect 中的样子:
有两个选项可以自定义单元格的宽度。
如果要均匀设置宽度,可以更改table-layoutDatagrid
<Datagrid style={{tableLayout: 'fixed'}} rowClick="edit">
<TextField source="sourceName" />
<BooleanField source="active" />
<TextField source="organizationName" />
</Datagrid>
Run Code Online (Sandbox Code Playgroud)
如果这还不够,那么我们必须Datagrid使用其所有依赖项创建自定义,以便我们可以传递给TableCell组件特定的宽度。无论是百分比还是像素值。这不是很好的文档,所以你必须依赖源代码ra-ui-materialui
这是例子
import {
Datagrid,
DatagridBody,
DatagridCell,
TextField,
BooleanField
} from 'react-admin';
import Checkbox from '@material-ui/core/Checkbox';
import TableCell from '@material-ui/core/TableCell';
import TableRow from '@material-ui/core/TableRow';
const CustomDatagridRow = ({ record, resource, id, onToggleItem, children, selected, basePath }) => (
<TableRow key={id}>
<TableCell style={{width="10%"}} padding="none">
{record.selectable && <Checkbox
checked={selected}
onClick={() => onToggleItem(id)}
/>}
</TableCell>
{React.Children.map(children, field => (
<TableCell style={{width: field.props.width}} key={`${id}-${field.props.source}`}>
{React.cloneElement(field, {
record,
basePath,
resource,
})}
</TableCell>
))}
</TableRow>
);
const CustomDatagridBody = props => <DatagridBody {...props} row={<CustomDatagridRow />} />;
const CustomDatagrid = props => <Datagrid {...props} body={<CustomDatagridBody />} />;
<CustomDatagrid style={{tableLayout: 'fixed'}} rowClick="edit">
<TextField width="20%" source="sourceName" />
<BooleanField width="20%" source="active" />
<TextField width="50%" source="organizationName" />
</CustomDatagrid>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1841 次 |
| 最近记录: |