In my React App I request data from an API and store it in state:
const [entities, setEntities] = useState([]);
in my useEffect
hook I get the Data from the API - basicly I have the following example array as my entities:
["@Organization", "@City", "@Immobilie", "@Currency", "@Test"]
Now I want to display all entities in a table and have a radio button in each row to select ONE entity.
I'm using React-Bootstrap to build the Table
<Table striped bordered style={{ color: "#4d4d4d" }}>
<thead>
<tr>
<th>Entities</th>
</tr>
</thead>
<tbody>
{entities.map(entity => <tr key={entity} style={{ color: "#4d4d4d" }}>
<td>
<Row>
<Col>
<Form.Check
type="radio"
label={entity}/>
</Col>
</Row>
</td>
</tr>)}
</tbody>
</Table>
In the <tbody>
I map every entry of the entities
array to a Table Element.
But Now I can click the Radio Button in each tablerow, without deselecting the others.
I tried to wrap a <Form>
element around the Table, but nothing changes.
If I wrap the <Form>
around the <tbody>
the <tbody>
doesnt get populated with tablerows.
When I put the <Form>
inside the <Table>
element the whole table doesn't render anymore.
How can I group the radio Buttons so only one is selectable in this dynamicly build table?
Thanks to The KNVBs comment I added a name attribute to my Form.Check
element
<Form.Check
type="radio"
name="entitySelection"
label={entity} />
Now only one tablerow-radio Button is selectable