gpt4 book ai didi

reactjs - React material-table 不显示表格图标

转载 作者:行者123 更新时间:2023-12-04 11:18:14 27 4
gpt4 key购买 nike

我有一个项目,我必须使用一张 table 。我正在使用 Material 表。但我似乎无法让它看起来正确。表格所需的图标没有显示,而是我得到了一些占位符文本。
这是我用来显示表格的代码。我在项目中安装了 Material 表和 Material 图标。

class RegistrationList extends Component {
state = {
registrations: [],
};

infoButtonHandler(id) {}

componentDidMount() {
axios.get("http://localhost:3030/api/items").then((res) => {
let registrations = [];
res.data.forEach((registration) => {
let childeren = "";
for (let i = 0; i < registration.childeren.length; i++) {
childeren += registration.childeren[i].name;
if (i + 1 !== registration.childeren.length) {
childeren += ", ";
}
}
registrations.push({
_id: registration._id,
name: registration.name,
childeren: childeren,
street: registration.street,
houseNumber: registration.houseNumber,
city: registration.city,
postalCode: registration.postalCode,
});
});
this.setState({ registrations: registrations });
});
}

rowClickedHandler(rowData) {
this.props.history.push("/RegistrationDetail/" + rowData._id);
}

render() {
let table = (
<MaterialTable
title="Overzicht"
columns={[
{ title: "familienaam", field: "name" },
{ title: "kinderen", field: "childeren" },
{ title: "dorp", field: "city" },
{ title: "postcode", field: "postalCode" },
{ title: "straat", field: "street" },
{ title: "nr.", field: "houseNumber" },
]}
data={this.state.registrations}
options={{
search: true,
}}
onRowClick={(event, rowData) => this.rowClickedHandler(rowData)}
/>
);
return <div>{table}</div>;
}
}
enter image description here

最佳答案

它在网站上说要做什么material-table
添加 Material 图标
在 material-table 中使用图标有两种方法,一种是通过 html 导入 Material 图标字体,另一种是导入 Material 图标并使用 material-table icons 属性。
HTML

<link
rel="stylesheet"
href="https://fonts.googleapis.com/icon?family=Material+Icons"
/>
或者
导入 Material 图标
可以导入图标以在 Material 表中使用,从而比使用字体库更灵活地自定义 Material 表的外观和感觉。
使用 npm 安装 @material-ui/icons:
npm install @material-ui/icons --save
使用 yarn 安装@material-ui/icons:
yarn add @material-ui/icons
如果您的环境不支持 tree-shaking,则推荐的导入图标的方法如下:
import AddBox from "@material-ui/icons/AddBox";
import ArrowDownward from "@material-ui/icons/ArrowDownward";
如果您的环境支持 tree-shaking,您还可以通过以下方式导入图标:
import { AddBox, ArrowDownward } from "@material-ui/icons";
注意:以这种方式导入命名导出将导致项目中包含每个图标的代码,因此除非您配置 tree-shaking,否则不建议这样做。它也可能影响热模块重载性能。来源:@material-ui/icons
例子
import { forwardRef } from 'react';

import AddBox from '@material-ui/icons/AddBox';
import ArrowDownward from '@material-ui/icons/ArrowDownward';
import Check from '@material-ui/icons/Check';
import ChevronLeft from '@material-ui/icons/ChevronLeft';
import ChevronRight from '@material-ui/icons/ChevronRight';
import Clear from '@material-ui/icons/Clear';
import DeleteOutline from '@material-ui/icons/DeleteOutline';
import Edit from '@material-ui/icons/Edit';
import FilterList from '@material-ui/icons/FilterList';
import FirstPage from '@material-ui/icons/FirstPage';
import LastPage from '@material-ui/icons/LastPage';
import Remove from '@material-ui/icons/Remove';
import SaveAlt from '@material-ui/icons/SaveAlt';
import Search from '@material-ui/icons/Search';
import ViewColumn from '@material-ui/icons/ViewColumn';

const tableIcons = {
Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
DetailPanel: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
Export: forwardRef((props, ref) => <SaveAlt {...props} ref={ref} />),
Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />),
FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
SortArrow: forwardRef((props, ref) => <ArrowDownward {...props} ref={ref} />),
ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />),
ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />)
};

<MaterialTable
icons={tableIcons}
...
/>

关于reactjs - React material-table 不显示表格图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63647493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com