I can't call function inside static function. Inside static function containing looping map to create a list, inside lopping I call function using a onClick but not working. I don't know to parse function correctly
ListData.js
constructor(props) {
super(props);
this.state = {
forecasts: [],
loading: true
};
// This binding is necessary to make "this" work in the callback
this.getClientReport = this.getClientReport.bind(this);
this.handleDelete = this.handleDelete.bind(this);
fetch('api/SampleData/Employees')
.then(response => response.json())
.then(data => {
this.setState({ forecasts: data, loading: false });
});
}
//handle download file
getClientReport(id) {
alert('test')
}
//handle delete
handleDelete(id) {
alert('test')
}
//handle table
static renderForecastsTable(forecasts) {
return (
<table className='table table-striped'>
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Photo</th>
<th>Height</th>
<th>Weight</th>
<th></th>
</tr>
</thead>
<tbody>
{forecasts.map((forecast, index) =>
<tr key={forecast.employeeId}>
<td>{index + 1}</td>
<td>{forecast.employeeName}</td>
<td><a onClick={() => this.getClientReport(forecast.employeeId)}>Download File</a></td>
<td>{forecast.height}</td>
<td>{forecast.weight}</td>
<td>
<Link to={"/add-list/" + forecast.employeeName}>Edit</Link> |
<a onClick={() => this.handleDelete(forecast.employeeId)}>Delete</a>
</td>
</tr>
)}
</tbody>
</table>
);
}
render() {
let contents = this.state.loading
? <p><em>Loading...</em></p>
: ListData.renderForecastsTable(this.state.forecasts);
return (
<div>
<h2>List Employee</h2>
{contents}
</div>
);
}
handleDelete and getClientReport not create onclick in HTML