I have two arrays, 1 column and 2 rows. I want to add rows array data to columns array dynamically as per shown in code. For time being i have taken hard coded values in these arrays i have one add button. Actually i want to render n*n matrix which has drop down buttons.
I have defined one method on add button and using for loop i pushed columns array to rows array.
import React, { Component } from "react";
import './Table.css';
export class Table extends Component {
constructor(props) {
super(props)
this.state = {
columns: ['state 1', 'state 2', 'state 3', 'state 4', 'state 5', ''],
emptyheader: [''],
rows: [
['state 1', 'state 2', 'state 3', 'state 4', '', ''],
['state 2', 'state 2', 'state 3', 'state 4', ' ', ''],
['state 3', 'state 2', 'state 3', 'state 4', ' ', ''],
['state 4', 'state 2', 'state 3', 'state 4', ' ', ''],
['state 5', 'state 2', 'state 3', 'state 4', ' ', '']
],
selectedTeam: ''
}
this.handleChange = this.handleChange.bind(this)
}
render() {
return (
<div>
<table align="center">
<tbody>
<tr>
{this.state.emptyheader.map((emptyheader, i) =>
<td >{emptyheader}</td>
)}
{this.state.columns.map((column, i) =>
<td >{column}</td>
)}
</tr>
{this.state.rows.map((row, i) =>
<tr key={i}>
{row.map((cell, i) =>
(i < 1) ? (
<td scope="row" key={i}>{cell}</td>
) : (
<td>
<select>
<option value="forward">Forward</option>
<option value="reverse">Reverse</option>
<option value="NA">NA</option>
</select>
</td>
)
)}
</tr>
)}
</tbody>
</table>
</div>
);
}
}
export default Table;
I want to render n*n matrix
handleChange.