Reputation: 313
I am working on sorting . Actually it working fine I can sort data in asc
and desc
. I am using sorting for different fields. For example I have 5 fields in table if user want to sort data for field 1 and user click on next ( I am also using pagination ) , now let suppose user is on page 5 and user want to sort data for fields 2 here I want to reset the value of state to initial value ( Initial Value of state val1=5
and val2=0
). How I will do this , I am new to ReactJS , Could someone please help me how I can fix this problem .
Sorry , If I made mistake in English Grammar because I am not native speaker .
Code
sortingData= (ascValue,desValue) => {
this.setState(
{
dataSort:
this.state.sortedData === ascValue? desValue: ascValue
},
() => {
this.loadData();
}
);
}
**Full Component Code**
class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
Item: 5,
skip: 0
}
this.handleClick = this.handleClick.bind(this);
}
urlParams() {
return `http://localhost:8001/meetups?filter[limit]=${(this.state.Item)}&&filter[skip]=${this.state.skip}`
}
handleClick() {
this.setState({skip: this.state.skip + 1})
}
render() {
return (
<div>
<a href={this.urlParams()}>Example link</a>
<pre>{this.urlParams()}</pre>
<button onClick={this.handleClick}>Change link</button>
</div>
)
}
}
ReactDOM.render(<Example/>, document.querySelector('div#my-example' ))
Upvotes: 1
Views: 798
Reputation: 1726
Better thing to do it to have state for all five data.
// In constructor
state = {
dataSort: {
data1: {sortedBy: asc, pagination: 1},
data2: {sortedBy: asc, pagination: 1},
data3: {sortedBy: asc, pagination: 1},
data4: {sortedBy: asc, pagination: 1},
data5: {sortedBy: asc, pagination: 1}
}
}
// When updating sorting logic for particular data
this.setState({
dataSort: {
...dataSort,
data1: {
sortBy: this.state.dataSort.data1 === val1? val2 : val1;
pagination: 5
}
}
})
Now, if you want to reset then you can just change the pagination value or sortby value for particular data.
Upvotes: 1