簡體   English   中英

添加帶有按鈕的列,該按鈕有助於在材料表反應中路由到新頁面(單擊時)

[英]Add column with a button which helps to route to a new page (when clicks) in material-table react

目前,我正在使用 covid 19 API 進行基於反應的個人項目。我正在使用材料表將國家/地區顯示為表記錄。 我需要添加一個帶有按鈕的列,該按鈕有助於在用戶單擊時路由到新頁面(具有相關國家/地區詳細信息的組件)。 任何幫助真的很感激。

componentDidMount() {
  axios
    .get('API URL')
    .then((res) => {
      this.setState({
        countries: res.data,
      });
    })
    .catch((err) => {
      console.log(err);
    });
}

render() {
  return (
    <div>
      <div className='c-overview-header' style={{ marginTop: 30 }}>

      <div className='country-tbl' style={{ marginTop: 20, height: 20 }}>
        <Container xs={12} md={8}>
          <Row>
            <Col>
              <MaterialTable
                columns={[
                  {
                    title: 'Flag',
                    field: 'flag',
                    filtering: false,
                    sorting: false,
                    render: (rowData) => (
                      <img
                        style={{ width: '50%', height: '50%' }}
                        src={rowData.countryInfo.flag}
                        alt={rowData.country}
                      />
                    ),
                  },

                  { title: 'Country Name', field: 'country' },
                  { title: 'Continent', field: 'continent' },
                  { title: 'Total Cases', field: 'cases', type: 'numeric' },
                  { title: 'Total Deaths', field: 'deaths', type: 'numeric' },
                ]}
                data={this.state.countries}
                options={{
                  filtering: true,
                  sorting: true,
                  actionsColumnIndex: -1,
                }}
                title=''
              />
            </Col>
          </Row>
        </Container>
      </div>
    </div>
  );
}

您可以按如下方式使用action道具

actions = {[
    {
      icon: 'info',
      tooltip : 'anything you want to display when hover over',
      onClick: (event) => window.location.href = '/path'
    }
 ]}

您可以使用actions道具,如下所示:

<MaterialTable
  title="Simple Action Preview"
  columns={[
    { title: "Name", field: "name" },
    { title: "Infected", field: "infected" }
  ]}
  data={[
    {
      name: "country1",
      infected: 300
    },
    {
      name: "country2",
      infected: 100
    }
  ]}
  actions={[
    {
      icon: "info",
      tooltip: "More info",
      onClick: (event, rowData) => {
        history.push(`/details/${rowData.name}`);
      }
    }
  ]}
/>

https://codesandbox.io/s/material-table-react-router-1hhre?file=/src/Table.jsx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM