Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  173] [ 5]  / answers: 1 / hits: 8457  / 3 Years ago, fri, april 30, 2021, 12:00:00

At what i m trying to do is i want to make checkbox in table using react js .but in my below in first not able to check box shown. i m trying to do is n my below code but i am unable to do is in my below code,and i also want to make one submit button inside table using reactjs .


anyone help me out this. I m stuck on that.its very thankful.


check her in my code https://codepen.io/svpan/pen/NWdJvmX


class TableComponent extends React.Component {
state = {};

handleRowClick = async (rowID) => {
// make an API call here, sth like
console.log(rowID)
const url1 = "https://grandiose-mulberry-garnet.glitch.me/query?id="+rowID;
const url2 = "https://grandiose-mulberry-garnet.glitch.me/params/"+rowID;
// const url = "https://mocki.io/v1/4d51be0b-4add-4108-8c30-df4d60e8df54";
// you can use any of the above API to test.
const response = await fetch(url1);
const res = await response.json();
// console.log(res)
this.setState({
...res,
});
window.open(res.url, '_blank');
};

render() {
var dataColumns = this.props.data.columns;
var dataRows = this.props.data.rows;

var tableHeaders = (
<thead>
<tr>
{" "}
{dataColumns.map(function (column) {
return <th> {column} </th>;
})}{" "}
</tr>{" "}
</thead>
);

var tableBody = dataRows.map((row) => {
return (
<tr onClick={() => this.handleRowClick(row.id)} key={row.id}>
{" "}
{dataColumns.map(function (column) {
return (
<td>
{" "}
<a target="_blank" rel="noopener noreferrer" href={row.url}>
{" "}
{row[column]}{" "}
</a>
</td>
);
})}{" "}
</tr>
);
});

// Decorate with Bootstrap CSS
return (

<table className="table table-bordered table-hover" width="100%">
{" "}
{tableHeaders} {tableBody}{" "}
</table>

);
}
}

// Example Data
var tableData = {

columns: ['Select','Service_Name', 'Cost/Unit'],
rows: [{
'Service_Name': 'airplane',
'Cost/Unit': 50,
'id': 1

}, {
'Service_Name': 'cat',
'Cost/Unit': 50,
'id': 2
},{
'Service_Name': 'fruits',
'Cost/Unit': 50,
'id': 5
}, {
'Service_Name': 'pool',
'Cost/Unit': 50,
'id': 4
}]
};


ReactDOM.render(
<TableComponent data = {tableData} />,
document.getElementById('table-component')

);

More From » html

 Answers
1

I've tweaked your code a little bit to add checkbox in select column.


  class TableComponent extends React.Component {
state = {};

handleRowClick = async (rowID) => {
// make an API call here, sth like
console.log(rowID)
const url1 = "https://grandiose-mulberry-garnet.glitch.me/query?id="+rowID;
const url2 = "https://grandiose-mulberry-garnet.glitch.me/params/"+rowID;
// const url = "https://mocki.io/v1/4d51be0b-4add-4108-8c30-df4d60e8df54";
// you can use any of the above API to test.
const response = await fetch(url1);
const res = await response.json();
// console.log(res)
this.setState({
...res,
});
window.open(res.url, '_blank');
};

render() {
var dataColumns = this.props.data.columns;
var dataRows = this.props.data.rows;

var tableHeaders = (
<thead>
<tr>
{" "}
{dataColumns.map(function (column) {
return <th> {column} </th>;
})}{" "}
</tr>{" "}
</thead>
);

var tableBody = dataRows.map((row) => {
return (
<tr onClick={() => this.handleRowClick(row.id)} key={row.id}>
{dataColumns.map(function (column) {
if(column == 'Select')
return (
<td>
<input type="checkbox" />
</td>
);
else
return (
<td>
<a target="_blank" rel="noopener noreferrer" href={row.url}>
{
row[column]
}
</a>
</td>
);
})}
</tr>
);
});

// Decorate with Bootstrap CSS
return (

<div>
<table className="table table-bordered table-hover" width="100%">
{tableHeaders} {tableBody}
</table>
<input type="submit" value="submit" />
</div>

);
}
}

// Example Data
var tableData = {

columns: ['Select','Service_Name', 'Cost/Unit'],
rows: [{
'Service_Name': 'airplan',
'Cost/Unit': 50,
'id': 1

}, {
'Service_Name': 'cat',
'Cost/Unit': 50,
'id': 2
},{
'Service_Name': 'fruits',
'Cost/Unit': 50,
'id': 5
}, {
'Service_Name': 'pool',
'Cost/Unit': 50,
'id': 4
}]
};


ReactDOM.render(
<TableComponent data = {tableData} />,
document.getElementById('table-component')

);

Like a checkbox submit button can be added.


[#1409] Friday, April 23, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tina

Total Points: 91
Total Questions: 106
Total Answers: 104

Location: Vanuatu
Member since Fri, May 13, 2022
2 Years ago
tina questions
Tue, Jul 7, 20, 00:00, 4 Years ago
Mon, Feb 3, 20, 00:00, 4 Years ago
;