【问题标题】:Adding Custom columns in React Material-table在 React Material-table 中添加自定义列
【发布时间】:2021-12-14 08:08:35
【问题描述】:

我是 MERN 的新手,因此我们将不胜感激。我正在尝试创建一个股票应用程序。 我已经使用 axios 检索数据并使用 Material-Table 将其显示在表格中。


    import React, {useState, useEffect} from 'react'
    import MaterialTable from 'material-table'
    import axios from "axios";
    
    const DataTable = () => {
      const [tableData, setTableData] = useState([]);
      const columns = [
        {title: 'NAME', field: 'name', width: 200},
        {title: 'SYMBOL', field: 'symbol', width: 200},
        {title: 'MARKET CAP', field: 'market_cap', width: 200},
        {title: 'CURRENT PRICE', field: 'price', width: 200}
      
      ]
      useEffect(() => {
            axios
              .get(
                "https://api.nomics.com/v1/currencies/ticker?key=xyz&interval=1d,30d&convert=USD&per-page=100"`
              )
              .then((res) => {
                console.log(res);
                setTableData(res.data);
              })
          }, []);
    
      return (
          <div>
              <div >
          <MaterialTable style={{height: 400, width: '70%', left: '14%', top: 100}}
            data={tableData}
            columns={columns}
            title={"Stock Details Table"}
            options={{
          search: true
          
        }}
            
          />
        </div>
          </div>
        
      )
    }
    
    export default DataTable

但是现在,我希望能够将我选择的任何公司的行数据保存到 mongoDb 数据库中,并且应该能够查看我选择的所有公司。

我的问题是,我如何添加一个单独的列,其中包含每个公司的按钮,以使我能够将行数据添加到我的 mongodb 数据库中。我不知道如何将自定义数据与来自 api 的数据一起添加。

【问题讨论】:

    标签: reactjs mongodb react-hooks material-ui material-table


    【解决方案1】:

    根据材料表documentation&lt;MaterialTable&gt; 组件存在一个onClick 操作属性:

    onClick:单击按钮时将触发此事件。参数为eventrowrows

    因此,您需要将一个函数传递给onClick 属性。 此函数将获取行详细信息,以便您可以通过另一个 API 调用将其发布到服务器。

    const handleSelectedRow = (event, row) => {
      // console.log(row) to see the data and choose your require to send with API calling
      axios.post("/saveMySelection", row).then(() => {}).catch(() => {}) // don't forget to use then and catch method with proper actions
    }
    
    
    <MaterialTable style={{height: 400, width: '70%', left: '14%', top: 100}}
      data={tableData}
      columns={columns}
      title={"Stock Details Table"}
      options={{
        search: true      
      }}
      actions={[
        {
          icon: 'save',
          tooltip: 'Save User',
          onClick: (event, rowData) => hadnleSelectedRow
        }
      ]}
    />
    

    您还可以在我之前提到的文档链接上查看工作版本的第一个示例。

    【讨论】:

    • 非常感谢!如果我要从 mongodb app.get('/saveMySelection', (req, res) => { for (let item of req.query){ console.log(item.name) console.log(item.价格)} ......我会这样做吗?
    • 这取决于您对项目要求的实施,我建议您再问一个问题(在新帖子中),更加关注该问题,但该问题可能会导致您不努力就成为基于意见的问题澄清@Leo
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2018-10-26
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    相关资源
    最近更新 更多