如何在使用React时在HTML表格中显示特定列

huangapple go评论92阅读模式
英文:

How to display specific columns in html table using React

问题

以下是您的代码的中文翻译:

// 在 App.js 中有如下代码

// 从数据源获取了50列,但我只想在表格中显示4列。
// 当我使用下面的方法时,UI 显示的是空白行而不是数据。

// 你能帮助我吗?

import { useEffect, useState } from "react";

const App = () => {

    // 1. 创建 useState
    const [employees, setEmployees] = useState([]);

    // 2. 调用 API
    useEffect(() => {
        fetch("api/employee/GetEmployees")
            .then(response => { return response.json() })
            .then(responseJson => {
                //console.log(responseJson)
                setEmployees(responseJson)
            })
    }, [])

    console.log(employees)
    //console.log(setEmployees)
    //console.log(useState([]))

    // 3. 创建 div 和表格
    return (
        <div className="container">
            <h1>PPMDs</h1>
            <div className="row">
                <div className="col-sm-12">
                    <table className="table table-striped">
                        <thead>
                            <tr>
                                <th>PPMD ID</th>
                                <th>PPMD 姓名</th>
                                <th>PPMD</th>
                                <th>电子邮件地址</th>
                            </tr>
                        </thead>

                        <tbody>
                            {
                                employees.map((item) => (
                                    <tr key={item.PersonnelNumber}>
                                        <td>{item.PersonnelNumber}</td>
                                        <td>{item.FirstName}</td>
                                        <td>{item.Type}</td>
                                        <td>{item.EmailAddress}</td>
                                    </tr>
                                ))
                            }
                        </tbody>
                    </table>

                </div>

            </div>

        </div>
        
        )
}

export default App;

请注意,我只提供了代码的翻译部分,不包括问题或其他内容。如果您需要任何进一步的帮助,请随时提问。

英文:

i have below code in App.js

I am getting 50 columns from the source, where as i want to display only 4 columns in a table.
when i approach below method, the UI is displaying blank rows than data.

can you help.

import { useEffect, useState } from &quot;react&quot;;
const App = () =&gt; {
//1 create useState
const [employees, setEmployees] = useState([]);
//2 call api
useEffect(() =&gt; {
fetch(&quot;api/employee/GetEmployees&quot;)
.then(response =&gt; { return response.json() })
.then(responseJson =&gt; {
//console.log(responseJson)
setEmployees(responseJson)
})
}, [])
console.log(employees)
//console.log(setEmployees)
//console.log(useState([]))
//3 create div and table
return (
&lt;div className=&quot;container&quot;&gt;
&lt;h1&gt;PPMDs&lt;/h1&gt;
&lt;div className=&quot;row&quot;&gt;
&lt;div className=&quot;col-sm-12&quot;&gt;
&lt;table className=&quot;table table-striped&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;PPMD ID&lt;/th&gt;
&lt;th&gt;PPMD Name&lt;/th&gt;
&lt;th&gt;PPMD&lt;/th&gt;
&lt;th&gt;Email Address&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
{
employees.map((item) =&gt; (
&lt;tr key={item.PersonnelNumber}&gt;
&lt;td&gt;{item.PersonnelNumber}&lt;/td&gt;
&lt;td&gt;{item.FirstName}&lt;/td&gt;
&lt;td&gt;{item.Type}&lt;/td&gt;
&lt;td&gt;{item.EmailAddress}&lt;/td&gt;
&lt;/tr&gt;
))
}
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
)
}
export default App;

答案1

得分: 1

I found the solution for it.
it is the camel case of the column names is casing the issue.
also the way we represent the columns should be enclosed in double quotes.
here is the updated body code.

<tbody>
{
employees.map((item) => (
<tr key={item["personnelNumber"]}>
<td>{item["personnelNumber"]}</td>
<td>{item["firstName"] + ', ' + item["lastName"]}</td>
<td>{item["type"]}</td>
<td>{item["emailAddress"]}</td>
</tr>
))
}
</tbody>
英文:

i found the solution for it.
it is the camel case of the column names is casing the issue.
also the way we represent the columns should be enclosed in double quotes.
here is the updated body code.

                         &lt;tbody&gt;
{
employees.map((item) =&gt; (
&lt;tr key={item[&quot;personnelNumber&quot;]}&gt;
&lt;td&gt;{item[&quot;personnelNumber&quot;]}&lt;/td&gt;
&lt;td&gt;{item[&quot;firstName&quot;] + &#39;, &#39; + item[&quot;lasttName&quot;]}&lt;/td&gt;
&lt;td&gt;{item[&quot;type&quot;]}&lt;/td&gt;
&lt;td&gt;{item[&quot;emailAddress&quot;]}&lt;/td&gt;
&lt;/tr&gt;
))
}
&lt;/tbody&gt;

答案2

得分: 0

这段文本的翻译如下:

"Seems like nothing wrong with this code. I tried to reproduce it, passing a list of objects with the same values you try to retrieve and it returns me a table with a row per item.

Despite this, however, i see that you are fetching your api like this:

fetch(&quot;api/employee/GetEmployees&quot;)

Did you config a proxy server like explained here? https://create-react-app.dev/docs/proxying-api-requests-in-development/

Otherwise you are missing the first part of the url, in your GET request. I'm expecting something like this:

fetch(&quot;http://localhost:PORT/api/employee/GetEmployees&quot;)"
英文:

Seems like nothing wrong with this code. I tried to reproduce it, passing a list of objects with the same values you try to retrieve and it returns me a table with a row per item.

Despite this, however, i see that you are fetching your api like this:

fetch(&quot;api/employee/GetEmployees&quot;)

Did you config a proxy server like explained here? https://create-react-app.dev/docs/proxying-api-requests-in-development/

Otherwise you are missing the first part of the url, in your GET request. I'm expecting something like this:

fetch(&quot;http://localhost:PORT/api/employee/GetEmployees&quot;)

huangapple
  • 本文由 发表于 2023年6月15日 02:30:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定