javascript - 如何在 MTableToolbar 上应用 disableGutters 道具?
问题描述
我正在使用 react material-table并希望覆盖工具栏的默认样式以传递道具 disableGutters={true} ,就像我们在material-ui toolbar中所做的那样。这是我的代码
<MaterialTable
// other props
components={{
Toolbar: (props) => {
return (
<div>
<MTableToolbar {...props} style={{paddingLeft:"0px"}}/>
</div>
);
},
}}
/>
但它不能删除排水沟填充。我也尝试过<MTableToolbar {...props} disableGutters={true}/>
,但没有任何效果。
解决方案
MTableToolbar
有这些课程MuiToolbar-root MuiToolbar-regular MuiToolbar-gutters
。您可以使用@material-ui/styles
. 先安装,npm install @material-ui/style
. 然后按照下面的代码:
import React from 'react';
import MaterialTable, { MTableToolbar } from 'material-table';
import { makeStyles } from '@material-ui/core/styles';
const useStyles = makeStyles({
toolbarWrapper: {
'& .MuiToolbar-gutters': {
paddingLeft: 0,
paddingRight: 0,
}
},
});
export default function App() {
const classes = useStyles();
return (
<MaterialTable
title="Toolbar Overriding Preview"
columns={[
{ title: 'Name', field: 'name' },
{ title: 'Surname', field: 'surname' },
{ title: 'Birth Year', field: 'birthYear', type: 'numeric' },
{
title: 'Birth Place',
field: 'birthCity',
lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
},
]}
data={[
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
]}
components={{
Toolbar: props => (
<div className={classes.toolbarWrapper}><MTableToolbar {...props} /></div>
),
}}
/>
)
}
替代解决方案:
还有另一种方法可以使用自己的标题,而不是覆盖原来的标题。
您必须复制道具以隐藏默认标题并显示您自己的。
Grid
与 the一起使用,MTableToolbar
以便它们仍然彼此相邻出现。
这是代码:
import React from 'react';
import MaterialTable, { MTableToolbar } from 'material-table';
import { Grid } from '@material-ui/core';
export default function App() {
return (
<MaterialTable
title="Toolbar Overriding Preview"
columns={[
{ title: 'Name', field: 'name' },
{ title: 'Surname', field: 'surname' },
{ title: 'Birth Year', field: 'birthYear', type: 'numeric' },
{
title: 'Birth Place',
field: 'birthCity',
lookup: { 34: 'İstanbul', 63: 'Şanlıurfa' },
},
]}
data={[
{ name: 'Mehmet', surname: 'Baran', birthYear: 1987, birthCity: 63 },
{ name: 'Zerya Betül', surname: 'Baran', birthYear: 2017, birthCity: 34 },
]}
components={{
Toolbar: props => {
// This will let you use your own Title while keeping the search
const propsCopy = { ...props };
// Hide default title
propsCopy.showTitle = false;
return (
<Grid container direction="row">
<Grid item xs={6}>
<h2>Your Own Title</h2>
</Grid>
<Grid item xs={6}>
<MTableToolbar {...propsCopy} />
</Grid>
</Grid>
);
}
}}
/>
)
}
推荐阅读
- typescript - 如何在 VSCode 扩展开发中向 QuickPick 添加新元素?
- python - 从 github 代码组织中克隆所有代码仓库的 python 脚本
- javascript - 每次使用同一个 JavaScript 函数时,如何获得一个新值?
- python - 如何使用 lambda 和 python 在 s3 上压缩文件
- python - 检查 get_source() 在作为参数传递给函数并且换行时不返回完整的 lambda 代码?
- amazon-web-services - 我正在使用带有颤振框架的aws。我试图删除一个auth cognito pool并尝试放大推送然后得到这个错误请帮我解决
- javascript - 页面加载后的 Cheerio 选择器
- angular - 步进器材料:在打字稿中使用 stepper.reset()
- android - Paypal 集成 Django Rest Api/ Android 客户端
- c# - 在罚款前添加 $ 符号