-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Table] onRowSelection Bug #1897
Comments
The table 'resets' on the render method being (re)called. You can see this if you wrap the setState in your _onRowSelection method in a timeout. To get around it, you can do something like this:
|
Hi Andrew, Any ideas? |
@husek Does row selection work briefly when you change your _onRowSelection code to the following?
|
@andrewhn , Yes, it does work for a short time with this snippet. |
@husek So you see that when |
ahd strange thing, @andrewhn , it's that i set selected at TableRow, but still doesn't working after the render 'reset', here's an simplified version of my component:
|
This code snippet will work for a single checked box, and it can be extended for multiple checked boxes by using an array as described above by @andrewhn
One possible fix for this issue would involve the library owner following a similar approach, persisting all checked rows and rebuilding them as checked rows when render() is called, and subsequently deleting the persisted checked rows in |
Hi , |
Even if you set |
This bug makes the Edit: The |
Threw together an example component with my proposed optimistic functionality, closely mimicing code I wrote before encountering this bug: import React, { Component, PropTypes } from 'react';
import {
Table,
TableHeader,
TableHeaderColumn,
TableBody,
TableRow,
TableRowColumn
} from 'material-ui';
import ContentAddIcon from 'material-ui/svg-icons/content/add';
class MyComponent extends Component {
constructor (props) {
super(props);
this.state = {
selectedPeople: []
};
}
handleRowSelection (indices) {
if (indices === 'all') {
this.setState({ selectedPeople: this.props.people });
} else if (indices === 'none') {
this.setState({ selectedPeople: [] });
} else {
let people = indices.map(index => {
return this.props.people[index];
})
this.setState({ selectedPeople: people })
}
}
isPersonSelected (subjectPerson) {
this.state.selectedPeople.forEach(selectedPerson => {
if (subjectPerson.id === selectedPerson.id)
return true;
});
return false;
}
doSomethingWithAllSelectedPeople () {
// Whatevs
}
render() {
var doSomethingButton = null;
if (this.state.selectedPeople.length > 0) {
doSomethingButton = (
<FloatingActionButton onMouseUp={this.doSomethingWithAllSelectedPeople.bind(this)}>
<ContentAddIcon />
</FloatingActionButton>
);
}
return (
<div className="my-component">
<Table
selectable={true}
multiSelectable={true}
onRowSelection={this.handleRowSelection.bind(this)}
>
<TableHeader>
<TableRow>
<TableHeaderColumn>ID</TableHeaderColumn>
<TableHeaderColumn>Name</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody>
{this.props.people.map((person, index) => {
return (
<TableRow key={index} isSelected={this.isPersonSelected.bind(this, person)}>
<TableRowColumn>{person.id}</TableRowColumn>
<TableRowColumn>{person.name}</TableRowColumn>
</TableRow>
)
})}
</TableBody>
</Table>
{doSomethingButton}
</div>
);
}
};
MyComponent.propTypes = {
people: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number,
name: PropTypes.string
}))
};
export default MyComponent; |
Any plan on fixing this issue? |
Well, i have opened this issue almost a year ago, and so far i've used two different approaches to fix this problem:
So i was able to do something like this.refs.theTable.getValue();
and
|
Huh, not sure what fever dream I was in when I posted because I just got it to work too. I think my |
@husek Maybe it's time to close this issue :) |
@corytheboyd, indeed it's! (But i still think that the getValue() method should be part of the lib 😄 ) |
I am using version 0.16.4 and still seeing this issue. I dynamically set the selected property to true before the table is rendered but the checkboxes do not get selected.
Any ideas? Thanks. |
@rachel-singh : I found a way to hack this behaviour. Instead of managing the
Then in your handler :
It would be nice to have a documented and tested way to implement this though. |
Anyone know why the above works? |
I am still having the same problem :/ |
I used the solution of ugomeda. Important is that you set the state of the TableBody after you set your state. To achieve that, I set the state of the TableBody as callback of my own state. Following works for me: constructor() {
super();
this.state = {
selectedRows: []
};
this._onRowSelection = this._onRowSelection.bind(this);
}
render() {
return (
<Table multiSelectable onRowSelection={this._onRowSelection}>
<TableHeader>
<TableRow o>
<TableHeaderColumn>c1</TableHeaderColumn>
<TableHeaderColumn>c2</TableHeaderColumn>
<TableHeaderColumn>c3</TableHeaderColumn>
</TableRow>
</TableHeader>
<TableBody showRowHover ref={(tableBody) => { this.tableBody = tableBody; }}>
{this._renderTableRow()}
</TableBody>
</Table>
);
}
_onRowSelection(rows: Array<number>) {
this.setState({selectedRows: rows}, () => this.tableBody.setState({ selectedRows: rows }));
} |
Another late solution for arrow functional style: Using "pure" from recompose
|
Evening guys,
Let's say i have the following table:
If i do something like this, it'll work fine:
but if i attach any kind of more complex event, it'll make impossible to 'check' ANY other rows.
here's some examples of events:
Both of them (Flux and non-flux version) cause the same issue.
Just to clarify it just freeze all my rows as unchecked, and if i put an console.log with the setState, it's returning just the last selected row instead of all rows.
any idea guys?
The text was updated successfully, but these errors were encountered: