Iterate over array in React - javascript

I want to iterate over an array that contains the end of a url so I can then 'concatenate' to the main site domain with the aim to gain a fully functional url (ie. www.mainUrlDomain.com/some-url)
This is my code:
<div>
<table>
<tbody>
<tr>
<th>Name</th>
<th>City</th>
<th>Code</th>
<th>Symbol</th>
</tr>
{data.map(data => (
<tr key={data.code}>
<td>
<Image
src={`${mainUrlDomain}/${data.code.toLowerCase()}.png`}
width={30}
height={20}
/>
</td>
{landingPagesKeys.includes(`${data.code}`)
?
<Link
href={`${mainUrlDomain}/${landingPages}`}
>
<td>
<a>{data.name}</a>
</td>
</Link> : <td>{data.name}</td>}
<td>{data.code}</td>
<td>{data.symbol}</td>
</tr>
))})
</tbody>
</table>
</div>
I have tried to add a way to iterate over the landingPages array like this:
{landingPagesKeys.includes(`${data.code}`)
?
{landingPages.map(data => (
<Link
href={`${mainUrlDomain}/${data`}
>
<td>
<a>{data.name}</a>
</td>
</Link> )} : <td>{data.name}</td>}
<td>{data.code}</td>
<td>{data.symbol}</td>
</tr>
))})
Unfortunately it didn't show me the array data as expected which contains the end of the desired url (ie. some-url) and landingPages after the url domain as the first example of code shows the entire array.
How to map through the array and obtain each individual url that landingPages contains?

You can use filter function to filter the data in an array.
landingPages.filter(data => landingPagesKeys.includes(`${data.code}`)).map(data => <div>...</div>)

Related

React: Warning: validateDOMNesting(...): <table> cannot appear as a child of <tr>

I have some nested data, and i populated it in a table. but i got this error:
" cannot appear as a child of "
How can it be solved
{projects.map((project, key) => (
<tbody key={key}>
<tr><td rowSpan={project.receivers.length + 1}>{key}</td></tr>
{project.receivers.map((p, key) => (
<tr key={key}><td>{p.receiverName}</td>
{p.pages.map((n, k) => (
<table key={k}>
<tbody>
<tr><td>{n}</td></tr>
</tbody>
</table>
))}
<td>{project.title}</td>
<td>{project.message}</td>
</tr>
))}
</tbody>
))}
As the error message says <tr><table>...</table></tr> isn't allowed.
If a table needs to go inside another (which is a sensible data structure so infrequently I've only seen once (dubious) case of it in 25 years) then the <table> needs to go inside a <td> or <th>.
In this case your nested table only has one row and one column, so it makes no sense for it to be a table at all.
Replace:
{p.pages.map((n, k) => (
<table key={k}>
<tbody>
<tr><td>{n}</td></tr>
</tbody>
</table>
))}
With
{p.pages.map((n, k) => <td key={k}>{n}</td>)}

How do I create a search filter for a JSON datatable in React?

I've been trying to implement a search filter for a JSON datatable by filtering the data by values of the data header...
Here's the input field
<input type= "text" placeholder="Search..." value={search} onChange={(e) =>
{setSearch(e.target.value)}} />
below is the datatable..
<table cellSpacing="40" cellPadding="20" >
<thead>
<tr>
<th>#ID</th>
<th>Name</th>
<th>Gender</th>
<th>LGA</th>
<th>WARD</th>
</tr>
</thead>
<tbody>
{person.map(record =>(
<tr key = {record.id}>
<td>{record.id}</td>
<td> <Link to={`/beneficiary/${record.full_name}`} >
{record.full_name}</Link></td>
<td>{record.gender}</td>
<td>{record.lga}</td>
<td> {record.ward} </td>
</tr>
))}
</tbody>
</table>
}
How do i wrap the input field with the datable to be able to get data by values of their data header. Say, a header for gender..i want to get results for male only.etc
By your description you need to add a filter rule to person.map(...), i.e. you need to use person.filter(somefilterfunction).map(record => (. So the question now becomes how to program the somefilterfunction to cater to your needs, for example you want to filter for male, then use
(person) => {
if (person.gender === search) {
return person;
}
}
the search here should be your state (I guess you are using React hooks), and other filter rules can be implemented similarly.
Modern React also supports useMemo and it can serve as a cache for the filtered data, this may have better performances. You may look deeper into it by look at the official document or Google a bit.
Just filter the Data first
{person.filter((e) => ['name', 'descr']
.some((property)=> e[property].toLowerCase()
.includes(search.toLowerCase())))
.map(record =>(
<tr key = {record.id}>
<td>{record.id}</td>
<td> <Link to={`/beneficiary/${record.full_name}`} >
{record.full_name}</Link></td>
<td>{record.gender}</td>
<td>{record.lga}</td>
<td> {record.ward} </td>
</tr>
))}

REACTJS: how to make <th> print only once and align properly with <td>

I have 2 loops in order to access the data. The problem is when I place my <th> outside Inner loop, <th> does not align with <td> and if I place my <th> inside inner for loop, <th> will repeat many times.
how can i make my table header <th> appear only once and align properly with the table data <td>?
here is the code: (I am using ReactJS)
<body>{item.table_text_data.map((c,j)=> (
<div>
<table>
<tr><th>bottom</th><th>Height</th><th>Left</th><th>Right</th><th>Text</th></tr>
{c.map((i,k) => { return (
<p key={'child' + k}>
<tr><td>{i.bottom}</td>
<td>{i.height}</td>
<td>{i.right}</td>
<td>{i.left}</td>
<td>{i.text}</td></tr>
</p>
)})}
</table>
</div>))}
</body>
And the output I'm getting for this is:
https://i.stack.imgur.com/gjNAp.png
So as you can see in the above screenshot, <th> is not aligned with <td>.
how can i overcome this? any help is much appreciated. Many thanks.
Try to follow the documents about correct table formatting (for example p is not a valid child of tr etc).
Try this:
<div>
<table>
<thead>
<tr>
<th>bottom</th>
<th>Height</th>
<th>Left</th>
<th>Right</th>
<th>Text</th>
</tr>
</thead>
<tbody>
{item.table_text_data.map((c,j)=> ( {c.map((i,k) => { return (
<tr key={'child' + k}>
<td>{i.bottom}</td>
<td>{i.height}</td>
<td>{i.right}</td>
<td>{i.left}</td>
<td>{i.text}</td>
</tr>
)})}))}
</tbody>
</table>
</div>

Can not .map 2 different arrays in same table row, <tr>

I am trying to map an entry to the table. In that entry, there is a column which can have more than one value.In that case, a sub-row will be created in the same row. I have attached an example below image. Here is the code I have tried which messes the table up completely.
{intake.map((value) => {
return (
<tr>
<th className="text-center" scope="row">{value}</th>
</tr>
)
})}
{attendanceIds.map((val, i) => {
return (
<tr>
<td className="text-center">{date[i]}</td>
<td className="text-center">{duration[i]}</td>
<td className="text-center">{module[i]}</td>
<td className="text-center">{start[i]}</td>
<td className="text-center">{topic[i]}</td>
<td className="text-center">{studentsPresent[i]}</td>
<td className="text-center">{totalStudents[i]}</td>
<td className="text-center"><button className="button" id={val}>Details</button></td>
</tr>
)
})}
This is what I desire to get
This is what I get from the code above
This is the data I have. (One attendance ID has multiple intakes)
The data looks like it belongs on the same row semantically, so you shouldn't use a new row, you should add your multiple entries as e.g. div (or whatever suits) in your <td>. Then use CSS to style as required.
From your question, it isn't entirely clear what your data structure is in your component, but assuming your attendanceIds map in the way that your image shows, you can do something like this:
{attendanceIds.map((val, i) => {
return (
<tr>
<td className="text-center">{
val.intake.length === 1
? {val.intake[0]}
: val.intake.map(item=>
<div>{item}</div>)
}
}</td>
// add the rest of the <td>s here
</tr>
)
})}
(Note that I've left the rest of the mapping up to you as the way you've done it isn't clear to me.)

React-router: Using <Link> as clickable data table row

I'm new to using ReactJS and react-router. I want a clickable table row and something like the following setup:
<Link to=“#”>
<tr>
<td>{this.props.whatever1}</td>
<td>{this.props.whatever2}</td>
<td>{this.props.whatever3}</td>
</tr>
</Link>
but I know you can't put <a> tags between the <tbody> and <tr> tags. How else can I accomplish this?
PS: I prefer not to use jQuery if possible.
onClick works, but sometimes you need an actual <a> tag for various reasons:
Accessibility
Progressive enhancement (if script is throwing an error, links still work)
Ability to open a link in new tab
Ability to copy the link
Here's an example of a Td component that accepts to prop:
import React from 'react';
import { Link } from 'react-router-dom';
export default function Td({ children, to }) {
// Conditionally wrapping content into a link
const ContentTag = to ? Link : 'div';
return (
<td>
<ContentTag to={to}>{children}</ContentTag>
</td>
);
}
Then use the component like this:
const users = this.props.users.map((user) =>
<tr key={user.id}>
<Td to={`/users/${user.id}/edit`}>{user.name}</Td>
<Td to={`/users/${user.id}/edit`}>{user.email}</Td>
<Td to={`/users/${user.id}/edit`}>{user.username}</Td>
</tr>
);
Yes, you'll have to pass to prop multiple times, but at the same you have more control over the clickable areas and you may have other interactive elements in the table, like checkboxes.
Why don't you just use onClick?
var ReactTable = React.createClass({
handleClick: function(e) {
this.router.transitionTo('index');
},
render: function() {
return(
<div>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Full Detail</th>
</tr>
</thead>
<tbody>
<tr onClick={this.handleClick.bind(this)}>
<td>{user.name}</td>
<td>{user.age}</td>
<td>{details}</td>
</tr>
</tbody>
</table>
</div>
);
}
});
This answers is based on #Igor Barbasin suggestion. This will add the link to the whole row instead of just the content and we also don't need to wrap all the individual 'td' element with 'Link'.
.table-row {
display: table-row
}
export default function Table() {
return (
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
{/* treats the link element as a table row element */}
<Link className="table-row">
<td>Noname</td>
</Link>
</tbody>
</table>
)
}
You can use useHistory() hook:
Declare:
const history = useHistory();
and use it with <tr> or <td> tag:
<tr onClick={() => history.push("/yoururl")}>
<td>{this.props.whatever1}</td>
<td>{this.props.whatever2}</td>
<td>{this.props.whatever3}</td>
</tr>
{shipment.assets.map((i, index) => (
<tr
style={{ cursor: "pointer" }}
onClick={(e) => e.preventDefault()}
>
<td>{index + 1}</td>
<td>{i._id}</td>
<td>{i.status}</td>
<td></td>
</tr>
))}

Categories

Resources