标题重写为:"Mui数据表格返回的行号为NaN"
P粉245276769
P粉245276769 2023-09-02 22:10:25
[JavaScript讨论组]
<p><pre class="brush:php;toolbar:false;">import React, { useState, useEffect } from &quot;react&quot;; import axios from &quot;axios&quot;; import io from &quot;socket.io-client&quot;; import { DataGrid } from &quot;@mui/x-data-grid&quot;; import NaveBar from &quot;../NaveBar/NaveBar&quot;; import SideBar from &quot;../SideBar/SideBar&quot;; import &quot;./NewsTable.scss&quot;; const socket = io(&quot;http://localhost:5000/&quot;); const NewsTablee = () =&gt; { const [news, setNews] = useState([]); //update news deeebd useEffect(() =&gt; { const fetchData = async () =&gt; { try { const allnews = await axios.get(&quot;http://localhost:5000/api/news&quot;); setNews(allnews.data.Date); } catch (error) { console.log(error); } }; socket.on(&quot;updateNewsTable&quot;, () =&gt; { fetchData(); }); }, [news]); useEffect(() =&gt; { axios .get(&quot;http://localhost:5000/api/news&quot;) .then((s) =&gt; { setNews(s.data.Date); }) .catch((er) =&gt; { console.log(er); }); }, []); const message = () =&gt; { socket.emit(&quot;a news updated&quot;, &quot;a news updated&quot;); }; const columns = [ { field: &quot;_id&quot;, headerName: &quot;no&quot;, flex: 0.5, renderCell: (params) =&gt; { console.log(params.row.Index + 1); // add this line return &lt;div&gt;{(params.row.index + 1).toString()}&lt;/div&gt; ; }, }, { field: &quot;Title&quot;, headerName: &quot;Title&quot;, flex: 3, sortable: true }, { field: &quot;createdAt&quot;, headerName: &quot;createdAt&quot;, flex: 2 }, { field: &quot;UserName&quot;, headerName: &quot;UserName&quot;, flex: 1 }, { field: &quot;Resurce&quot;, headerName: &quot;Resurce&quot;, flex: 1 }, { field: &quot;Language&quot;, headerName: &quot;Language&quot;, flex: 1 }, { field: &quot;ProtectionLevel&quot;, headerName: &quot;Protection Level&quot;, flex: 1 }, { field: &quot;priority&quot;, headerName: &quot;Priority&quot;, flex: 1 }, { field: &quot;Media&quot;, headerName: &quot;Media&quot;, flex: 1 }, ]; return ( &lt;&gt; &lt;NaveBar /&gt; &lt;SideBar /&gt; &lt;div className='content-wrapper' style={{ minHeight: &quot;1172.56px&quot; }}&gt; &lt;div className='NewsTableContainer'&gt; &lt;div className='headrt'&gt; &lt;button onClick={message} style={{ height: 30, width: 30 }}&gt;&lt;/button&gt; &lt;/div&gt; &lt;div style={{ height: &quot;570px&quot;, width: &quot;100%&quot; }} className='newsTable'&gt; &lt;DataGrid rows={news} columns={columns} getRowId={(row) =&gt; row._id} sortModel={[{ field: &quot;createdAt&quot;, sort: &quot;desc&quot; }]} /&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/&gt; ); };</pre> <p>导出默认NewsTablee;}</p> <p>返回行号为nan</p>
P粉245276769
P粉245276769

全部回复(1)
P粉593649715

作为良好的实践,我建议像这样重新定义列:

{
      field: "_id",
      headerName: "no",
      flex: 0.5,
      type: 'number', valueGetter: (params) =>  `${params.row.index + 1}`
    },

另外,我假设{news}对象确实有一个index属性,它是一个数字。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号