I'm currently working on react table. So, I have a column of checkboxes, I have even maintained the count of my checkboxes. I just want to render the count of rows selected you can say or number of checkboxes checked in react table footer near pagination. Any sort of help will be appreciated. Thanks in advance
Asked
Active
Viewed 884 times
-2
-
3It's not quite clear what you are asking. Please create a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) so that it will be easier for someone to help you. – Tholle Apr 05 '19 at 10:46
-
Thanks @Tholle for replying but simply I just want to render a custom element in react table footer near pagination. Infact I have used props such as getTfootProps and other footer props. But thats not working out. – Deepankar Singh Apr 05 '19 at 10:48
-
Specify the library clearly in the question and include a link to the library. – Dehan Apr 05 '19 at 10:49
-
This is the link of docs: https://www.npmjs.com/package/react-table – Deepankar Singh Apr 05 '19 at 10:51
2 Answers
0
Have a state variable number_of_checked_checkboxes
. Increase/Decrease it's value based on the checkboxes checked.
constructor(props) {
super(props);
this.state = {number_of_checked_checkboxes: 0}
}
handleCheckBoxClick(e) {
if (e.target.checked)
this.setState({number_of_checked_checkboxes: this.state.number_of_checked_checkboxes + 1})
else
this.setState({number_of_checked_checkboxes: this.state.number_of_checked_checkboxes - 1})
}
render() {
const {number_of_checked_checkboxes} = this.state;
return (
<div>
{/* your table + pagination code goes here */}
<input type="checkbox" onClick={this.handleCheckBoxClick}/>
{/* your table + pagination code goes here */}
<div className={'checkbox-counter'}>{number_of_checked_checkboxes}</div>
</div>
);
}
Hope this helps.

anurag
- 2,176
- 2
- 22
- 41
-1
After doing lot of RND on react table I wasn't able to figure out how to achieve custom element rendering via React Table props. So, at the end I used traditional javascript or jquery DOM manipulation technique.
In componentDidMount I appended element where I wanted but text will not change dynamically, Therefore I did that in my method where I'm checking my checkboxes. See that method below:
componentDidMount() {
let parentDiv = document.getElementsByClassName('-pagination');
let pageClass = document.getElementsByClassName('select-wrap');
let elementToAppend = document.createElement('div')
elementToAppend.id = 'sel'
let textNode = document.createTextNode(`${this.state.checkItems.length} row selected`)
elementToAppend.appendChild(textNode);
pageClass[0].insertAdjacentElement("beforebegin", elementToAppend)
}
And where I checked my checkboxes I changed the text of that element:
singleChange = (employeeId, event) => {
let checkedItems = this.state.checkItems;
if (event.target.checked) {
checkedItems.push(employeeId);
}
else {
if (checkedItems.includes(employeeId)) {
let index = checkedItems.indexOf(employeeId);
checkedItems.splice(index, 1);
}
}
checkedItems.length == this.props.employees.length ? this.setState({ allChecked: true }) : this.setState({ allChecked: false })
this.setState({ checkItems: checkedItems })
//this.appendElements();
document.getElementById('sel').innerHTML = this.state.checkItems.length <= 1 ? `${this.state.checkItems.length} row selected` : `${this.state.checkItems.length} rows selected`;
}

Deepankar Singh
- 193
- 1
- 13
-
1I think you misunderstood. Here at SO both questions and answers are suppose to make sense for other users as well, and your question doesn't. Edit it how you've been asked, and the same goes for several of your other questions/answers. – Asons Apr 18 '19 at 14:40