I am having trouble in fragmenting table rows from the JSON fetched data.
Code looks like below:
<table>
<thead>
<tr>
<th scope="col">Location ID #</th>
<th scope="col">Device Name</th>
</tr>
</thead>
<tbody>
{Object.keys(deviceList).map((key, index) => {
return (
<tr key={index}>
{deviceList[key].ports.map(port => port.value.id)
.map(ids => {
console.log("type", typeof(ids)) // it says number
return <td>{ids}</td>
})
}
</tr>
)
})}
</tbody>
</table>
actual result:
ID. Name.
23301 24401
99501 87701
22201
The above result is because one port can have multiple ID's but it shows that in parallel instead in the next row. So there are three ports and portA has two devices, portB has two devices and portC has just one.
expected result:
ID. Name.
23301
24401
99501
87701
22201
I could fix it by changing the way I was implementing it.
{Object.keys(deviceList).map(key => {
return deviceList[key].ports.map(port => {
return (
<tr>
<td> {port.value.siteId}</td>
<td>{port.value.name}</td>
<td>{deviceList[key].value.targetId === undefined ? deviceList[key].value.name : deviceList[key].value.targetId}</td>
<td>{deviceList[key].value._type}</td>
<td>{port.description === undefined ? deviceLookup[key].value.name : deviceList[key].value.name + ' ' + "[" + port.description + "]"}</td>
</tr>
)
})
})
}
Related
I am using json server to performing GET,POST,DELETE operation however DELETE method doesn't work, getting an error "XHR DELETE http://localhost:3333/students1" in console and then I get an empty object.
In network section it returns the code with 404. In request i get -
const deleteHandle = (id) => {
fetch('http://localhost:3333/students' + id, {
method:"DELETE",
}).then((rawData) => {
rawData.json().then((resp) => {
console.log(resp);
})
})
}
return (
<div className='tableContainer'>
<table border={1}>
<tbody>
<tr>
<th>Id</th>
<th>USER NAME</th>
<th>Email</th>
<th>Mobile No</th>
<th>Delete Users</th>
</tr>
{
data.map((val, index) => {
return (
<tr key={index}>
<td>{val.id}</td>
<td>{val.userName}</td>
<td>{val.email}</td>
<td>{val.mobileNo}</td>
<td><button onClick={() => { deleteHandle(val.id) }}>Delete User</button></td>
</tr>
)
})
}
</tbody>
</table>
</div>
Error I am getting :
i would like to output 10 times before changing column, I saw some other examples but I didn't understand, im quite new to react. Furthermore i would like to show different timeslots in the first column 07:00-08:30, 08:30-10 and so on, with the setup i have at the moment how would i go about doing that?
function BookingTable() {
useEffect(() => {
Axios.post('http://localhost:3001/api/get/week1').then((response) => {
setIsBooked(response.data)
console.log(response.data);
})
}, []);
const [isBooked, setIsBooked] = useState([])
const renderTableData = () => {
return isBooked?.map((val) => (
<tr class>
<td></td>
<td>{val.booked}</td>
</tr>))
}
return (
<table id="table">
<thead>
<tr>
<th> </th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Satuday</th>
<th>Sunday</th>
</tr>
</thead>
<tbody>
{renderTableData()}
</tbody>
</table>
)
}
export default BookingTable
let favorites=[];
var myObj = {
"username" : username,
"problems" : solved
};
//push the object to your array
favorites.push( myObj );
I am pushing the response from an Axios API into an array. How to display the information in ReactJS in the form of table.
Simple map function returning each row of the body:
<table>
<thead>
<tr>
<th>Username</th>
<th> Problems</th>
</tr>
</thead>
<tbody>
{favourites.map((fav) => {
return (
<tr>
<td>{fav.username}</td>
<td>{fav.problems}</td>
</tr>
);
})}
</tbody>
</table>;
I have an array which contains multiple objects like this:
const locations = [
{
information: ["Data Analyst", "Uruguay", "Montevideo", "$30,000"],
},
{
information: ["CTO", "UK", "Manchester", "$75,000"],
}
];
And I want to display each array in a new table row containing a <td> with each string.
Currently I have done this:
<tr>
{locations.map((location) => <td>{location.information}</td>)};
</tr>
Which returns each piece of information on a <td>
This is how the data looks at the minute:
And how it's meant to look
For this you have to use Array.map() like:
// To create tha table body by iterating data array
{
locations.map((location, locationIndex) =>
<tr key={locationIndex}>
<td>{location.information[0]}</td>
<td>{location.information[1]}</td>
<td>{location.information[2]}</td>
<td>{location.information[3]}</td>
</tr>
)
}
// In case you don't want to harcode the array index
{
locations.map((location, locationIndex) =>
<tr key={locationIndex}>
location.information.map((loc, locIndex) =>
<td key={locIndex}>{loc}</td>
)
</tr>
)
}
Change the code from,
<tr>
{locations.map((location) => <td>{location.information}</td>)};
</tr>
To:
<table border="1" width="100%" className="component_useRefBox">
<tbody>
{locations.map((location, i) => {
return (
<tr key={i}>
{location.information.map((data, j) => {
return <td key={j}> {data} </td>;
})}
</tr>
);
})}
</tbody>
</table>
To display two records row by row, you need to move {locations.map((location) .... )} above <tr> ... </tr> tag, because while using map method only each row will get iterated and you will get two separate rows..
{locations.map((location, i) => {
return (
<tr>
....
</tr>
)})}
As location.information is of array data, you cannot assign it directly.
You need to iterate using map and display each data like,
{location.information.map((data) => {
return <td> {data} </td>;
})}
This is the table below that I need to iterate.
<tbody>
<tr>
<td>1</td>
<td>Thomas Hill</td>
<td>4</td
</tr>
<tr>
<td>2</td>
<td>Greg Hill</td>
<td>39</td
</tr>
<tr>
<td>3</td>
<td>Jane Hill</td>
<td>31</td
</tr>
</tbody>
I am trying get the first td each and code it like this.
let values = []
cy.get('tbody > tr')
.find('td')
.each(($el, $index) => {
cy.wrap($el)
.invoke('text')
.then(num => {
if($index!==0)
values.push(num)
})
})
Upon searching it, the arrays has no value. I want to get value of the first TD only in each row.
You say that you try to invoke the first Td value, of each row, but you are invoking a command that gets each Td in the code. Here is my suggestion
let values = []
cy.get('tbody > tr')
.each(($el) => {
cy.wrap($el)
.find('td')
.first()
.invoke('text')
.then(num => {
values.push(num)
})
})