首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Material-table -如果没有数据显示消息

Material-table -如果没有数据显示消息
EN

Stack Overflow用户
提问于 2020-08-08 10:20:23
回答 2查看 6.9K关注 0票数 2

我使用的是材料表https://material-table.com/#/docs/get-started。但是,如果没有数据返回,我似乎找不到任何有关显示默认消息的信息?

我想知道有没有人知道我会怎么做。下面是我创建的测试表(使用假数据)。现在,如果数据是空的,我想要一条消息,用一个按钮来显示表数据将显示"create your ad now“的位置。

代码语言:javascript
复制
import React from 'react';
import { forwardRef } from 'react';
import MaterialTable from 'material-table';
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';

export default function MaterialTableDemo() {

    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} />)
      };

    const columns = {columns: [
        { title: 'Ad Name', field: 'name' },
        { title: 'Status', field: 'status' },
        { title: 'Formate', field: 'formate'},
        { title: 'Cost Type', field: 'costtype'},
        { title: 'Ad Listens', field: 'adlistens',  type: "numeric" },
        { title: 'Ads Served', field: 'adserved',  type: "numeric" },
        { title: 'Budget', field: 'budget',  type: "numeric" },
        { title: 'End Date', field: 'enddate',  type: "date" },
       
      ]};
   
  const data = { data: [
    { name: 'Test Ad', status: 'Pending', formate: 'Radio/Podcast', costtype: 'PPL', adlistens:0, adserved:0, budget:'$100', enddate:'02-02-2025' },
    { name: 'ZTest Ad', status: 'Pending', formate: 'Radio/Podcast', costtype: 'PPL', adlistens:0, adserved:0, budget:'$100', enddate:'02-05-2025' },
    { name: 'DTest Ad', status: 'Pending', formate: 'Radio/Podcast', costtype: 'PPL', adlistens:0, adserved:0, budget:'$100', enddate:'02-01-2025' },
  
]};

  
  return (
    <div style={{ maxWidth: "90%", margin:'5vh auto' }}>
    <MaterialTable
      title="Your Ads"
      icons={tableIcons}
      columns={columns.columns}
      data={data.data}
      /*editable={{
        onRowAdd: (newData) =>
          new Promise((resolve) => {
            setTimeout(() => {
              resolve();
              setState((prevState) => {
                const data = [...prevState.data];
                data.push(newData);
                return { ...prevState, data };
              });
            }, 600);
          }),
        onRowUpdate: (newData, oldData) =>
          new Promise((resolve) => {
            setTimeout(() => {
              resolve();
              if (oldData) {
                setState((prevState) => {
                  const data = [...prevState.data];
                  data[data.indexOf(oldData)] = newData;
                  return { ...prevState, data };
                });
              }
            }, 600);
          }),
        onRowDelete: (oldData) =>
          new Promise((resolve) => {
            setTimeout(() => {
              resolve();
              setState((prevState) => {
                const data = [...prevState.data];
                data.splice(data.indexOf(oldData), 1);
                return { ...prevState, data };
              });
            }, 600);
          }),
      }}*/
    />
    </div>
  );
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-08 11:26:54

您需要使用material表的component overriding来实现这一点。下面是一个重写body以在表格中放置一个按钮的示例(我没有添加任何CSS,所以它很难看)。

https://codesandbox.io/s/goofy-moore-h2fkg?file=/src/Table.jsx

票数 1
EN

Stack Overflow用户

发布于 2020-11-17 22:57:26

您可以通过一种更优雅的方式来实现这一点,通过使用localization属性,如下所示:

代码语言:javascript
复制
<MaterialTable
    localization={{
        body: {
            emptyDataSourceMessage: (
                <Button color="primary" className={classes.button}>
                    Create your ad now
                </Button>
            ),
        },
    }}
    {...otherProps}
/>;
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63311224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档