I added a custom column with add and minus buttons to every row and I want onClick each + buttons, counter will be updated for that particular row only not every row?
code: const dataFormatter = (cell, row, rowIndex, formatExtraData) => {
return (
<div>
<div onClick={() => addToCart(rowIndex)}>
<img src={plus} style={{backgroundColor: '#2a4252'}}/>
</div>
<div id='text-val'> {formatExtraData} </div>
<div onClick={() => removeFromCart(rowIndex)}>
<img src={minus} style={{backgroundColor: '#2a4252'}}/>
</div>
</div>
);
};
and ... formatExtraData: this.state.counter, ...
const addToCart = (index) => { this.setState({counter: this.state.counter + 1}); }
...
Hi, you want that when a + button is clicked the counter increase just for that specific row of the button clicked? Is that correct?
Hi, you want that when a
+button is clicked the counter increase just for that specific row of the button clicked? Is that correct?
Yes
Could you provide the code for that component so I can help you more efficiently?
Could you provide the code for that component so I can help you more efficiently? ........ constructor(props) { super(props); this.state = { counter: 0 }; } ...... const columns = [{ { dataField: '', text: 'Qty', formatter: dataFormatter, formatExtraData: this.state.counter, headerStyle: () => { return {width: '15%', textAlign: 'center', fontSize: '12px', color: '#7F7F7F'}; }, }]; .......
const dataFormatter = (cell, row, rowIndex, formatExtraData) => {
return (
<div>
<div onClick={() => addToCart(rowIndex)}>
<img src={plus} style={{backgroundColor: '#2a4252'}}/>
</div>
<div id='text-val'> {formatExtraData} </div>
<div onClick={() => removeFromCart(rowIndex)}>
<img src={minus} style={{backgroundColor: '#2a4252'}}/>
</div>
</div>
);
};
..........
const addToCart = (index) => { console.log(index) this.setState({counter: this.state.counter + 1}); }
.....
The problem is in the state, all the rows of the table are receiving the same state variable, so when you update one row the state is the same for all the others. In order to make each row increase the counter individually, you need to have one counter (state) for each row.
The problem is in the state, all the rows of the table are receiving the same state variable, so when you update one row the state is the same for all the others. In order to make each row increase the counter individually, you need to have one counter (state) for each row.
can we do something with rowIndex?If not how we are gonna have counters for each row?can you please suggest the best way to achieve this?
The items to be displayed in the table are received as props in the component?
The items to be displayed in the table are received as props in the component?
yes
The items to be displayed in the table are received as props in the component?
yes
My approach is the following after you receive your table data as props you can iterate through then, this way you add a default counter for each row.
state = {
tableCounters: []
};
componentDidMount() {
const tableCounters = [];
const tableData = this.props.myTableData;
tableData.forEach(() => {
tableCounters.push({ counter: 0 });
});
this.setState({ tableCounters });
}
After that, you can implement the method getCounter and pass it to your formatExtraData
getCounter = rowIndex => {
if (this.state.tableCounters[rowIndex] !== undefined) {
return this.state.tableCounters[rowIndex].counter;
}
};
And your dataFormatter will look like this
const dataFormatter = (cell, row, rowIndex, formatExtraData) => {
return (
<div>
<div onClick={() => addToCart(rowIndex)}>
<img src={plus} style={{ backgroundColor: "#2a4252" }} />
</div>
<div id="text-val"> {formatExtraData(rowIndex)} </div>
<div onClick={() => removeFromCart(rowIndex)}>
<img src={minus} style={{ backgroundColor: "#2a4252" }} />
</div>
</div>
);
};
With that done you need to update your addToCart in order to update the counter for the specific row index
The items to be displayed in the table are received as props in the component?
yes
My approach is the following after you receive your table data as props you can iterate through then, this way you add a default counter for each row.
state = { tableCounters: [] }; componentDidMount() { const tableCounters = []; const tableData = this.props.myTableData; tableData.forEach(() => { tableCounters.push({ counter: 0 }); }); this.setState({ tableCounters }); }After that, you can implement the method getCounter and pass it to your
formatExtraDatagetCounter = rowIndex => { if (this.state.tableCounters[rowIndex] !== undefined) { return this.state.tableCounters[rowIndex].counter; } };And your
dataFormatterwill look like thisconst dataFormatter = (cell, row, rowIndex, formatExtraData) => { return ( <div> <div onClick={() => addToCart(rowIndex)}> <img src={plus} style={{ backgroundColor: "#2a4252" }} /> </div> <div id="text-val"> {formatExtraData(rowIndex)} </div> <div onClick={() => removeFromCart(rowIndex)}> <img src={minus} style={{ backgroundColor: "#2a4252" }} /> </div> </div> ); };With that done you need to update your
addToCartin order to update the counter for the specific row index
Hey I tried like below but does not seem to work. .... this.state = { counter: 0, tableCounters: [] }; ....
componentDidUpdate(prevProps, prevState) {
const tableCounters = [];
const tableData = this.props.data;
tableData.forEach(() => {
tableCounters.push({counter: 0});
});
if (this.props.data !== prevProps.data) {
this.setState({tableCounters: tableCounters});
}
}
getCounter = rowIndex => {
if (this.state.tableCounters[rowIndex] !== undefined) {
return this.state.tableCounters[rowIndex].counter;
}
};
....
const dataFormatter = (cell, row, rowIndex, formatExtraData) => { return (
Hi @dashName , your state should be like this
state = {
tableCounters: []
};
And your addToCart method should loop through the array of state counters and then update the state counter for the specific row, the way that you implemented it will do not work because it is setting the state on the "global" counter.
I think this will do the trick
addToCard = rowIndex => {
this.setState(prevState => ({
tableCounters: prevState.tableCounters.map((item, index) => {
if (index === rowIndex) {
return { counter: item.counter + 1 };
}
return item;
})
}));
}
And the componentDidUpdate should be componentDidMount
Hi @dashName , your state should be like this
state = { tableCounters: [] };And your
addToCartmethod should loop through the array of state counters and then update the state counter for the specific row, the way that you implemented it will do not work because it is setting the state on the "global" counter.I think this will do the trick
addToCard = rowIndex => { this.setState(prevState => ({ tableCounters: prevState.tableCounters.map((item, index) => { if (index === rowIndex) { return { counter: item.counter + 1 }; } return item; }) })); }And the
componentDidUpdateshould becomponentDidMount
ComponentDidMount was returning props value as undefined so i used componentDidUpdate..Thanks a lot for the solution..it works..really appreciate
Hi @dashName , your state should be like this
state = { tableCounters: [] };And your
addToCartmethod should loop through the array of state counters and then update the state counter for the specific row, the way that you implemented it will do not work because it is setting the state on the "global" counter. I think this will do the trickaddToCard = rowIndex => { this.setState(prevState => ({ tableCounters: prevState.tableCounters.map((item, index) => { if (index === rowIndex) { return { counter: item.counter + 1 }; } return item; }) })); }And the
componentDidUpdateshould becomponentDidMountComponentDidMount was returning props value as undefined so i used componentDidUpdate..Thanks a lot for the solution..it works..really appreciate
No problem
Hi @dashName , your state should be like this
state = { tableCounters: [] };And your
addToCartmethod should loop through the array of state counters and then update the state counter for the specific row, the way that you implemented it will do not work because it is setting the state on the "global" counter. I think this will do the trickaddToCard = rowIndex => { this.setState(prevState => ({ tableCounters: prevState.tableCounters.map((item, index) => { if (index === rowIndex) { return { counter: item.counter + 1 }; } return item; }) })); }And the
componentDidUpdateshould becomponentDidMountComponentDidMount was returning props value as undefined so i used componentDidUpdate..Thanks a lot for the solution..it works..really appreciate
No problem
On above context: ...
Currently it takes {formatExtraData(rowIndex)} data as the value but I want to support userInput as well , i.e make the div editable so that it can accepts the userInput numbers as well is it possible to support both features?
To be more specific override the formatExtraData(rowIndex) with user input, whenever there is a userInput
I tried with cell property but , celleditfactory is for whole cell right but here I dont want to do for whole cell but only the part of it
Hi, you can change the div that contains the number to a input so you can attach onChange method to it and support user input.
const dataFormatter = (cell, row, rowIndex, formatExtraData) => {
return (
<div>
<div onClick={() => addToCart(rowIndex)}>
<img src={plus} style={{ backgroundColor: "#2a4252" }} />
</div>
<input
id="input-quantity"
value={formatExtraData(rowIndex)}
onChange={// Custom on change goes here}
/>
<div onClick={() => removeFromCart(rowIndex)}>
<img src={minus} style={{ backgroundColor: "#2a4252" }} />
</div>
</div>
);
};
@dashName, you can send your custom onChange method, so I can see if is there any problem?
@dashName, you can send your custom
onChangemethod, so I can see if is there any problem?
const handleChange = (e) => {
console.log(e.currentTarget.value,row)
this.setState(prevState => ({
tableCounters: prevState.tableCounters.map((item, index) => {
return { counter: e.currentTarget.value };
})
}));
};
it is updating the value,but not sure how I am gonna pass the row value from dataFormatter to access addToCart
<input style={{backgroundColor: '#fff', padding: '2px 5px', outline: 'none'}} id='text-val' value={formatExtraData(rowIndex)} onChange={handleChange}/>
Your onChange method is not actually changing the counter for a specific row, you need to pass along with the event the row index that you want to change the counter.
Like this
onChange={e => onChange(rowIndex, e)}
Your onChange method will look like this
handleChange = (rowIndex, e) => {
this.setState(prevState => ({
tableCounters: prevState.tableCounters.map((item, index) => {
if (index === rowIndex) {
return { counter: e.currentTarget.value };
}
return item;
})
}));
};
Your
onChangemethod is not actually changing the counter for a specific row, you need to pass along with the event the row index that you want to change the counter.Like this
onChange={e => onChange(rowIndex, e)}Your
onChangemethod will look like thishandleChange = (rowIndex, e) => { this.setState(prevState => ({ tableCounters: prevState.tableCounters.map((item, index) => { if (index === rowIndex) { return { counter: e.currentTarget.value }; } return item; }) })); };
yes you are right.Thanks
Hi @dashName, it worked properly?