Skip to content

selected rows are lost when the component re-render #151

Description

@bjrmatos

Hi, i've found another bug in #145, i've got this:

var Griddle = require('griddle-react');

var data = [{
  id: 1,
  name: 'Boris'
}, {
  id: 2,
  name: 'Junior'
}];

var App = React.createClass({
  _onSomeEvent: function() {
     console.log(this.refs.griddleTable.getSelectedRowIds()); // selected ids
   },

  render: function() {
    return (
      <button onClick={this._onSomeEvent}>Click Me</button>
      <Griddle ref="griddleTable" isMultipleSelection={true} results={someData} />
    );
  }
});

the only way to get access to the selected row ids is via a ref in the griddle component (i think this is problematic, because makes harder to sync the state of App component with the selected rows, maybe we could find a nice way to keep in sync App state), the problem that i have found is if my App component re-render for what-ever reason (a call to setState in another part) the selected rows are lost.

I've found that the problem is this logic in componentWillReciveProps:

componentWillReceiveProps: function(nextProps) {
        this.setMaxPage(nextProps.results);

        if(nextProps.columns !== this.columnSettings.filteredColumns){
            this.columnSettings.filteredColumns = nextProps.columns;
        }

        // NEW CODE IN #145. THIS CAUSES AN OUT-OF-SYNC BETWEEN STATE MANAGER (App component) AND THE TABLE (Griddle) 
        // BECAUSE WHEN APP RE-RENDER THERE IS NO `selectedRowIds` PROPERTY, THIS CAUSES THAT THE PREVIOUS ROWS ARE RESETTED
        if(nextProps.selectedRowIds) {
             var visibleRows = this.getDataForRender(this.getCurrentResults(), this.columnSettings.getColumns(), true);

             this.setState({
                 isSelectAllChecked: this._getAreAllRowsChecked(nextProps.selectedRowIds, _.pluck(visibleRows, this.props.uniqueIdentifier)),
                 selectedRowIds: nextProps.selectedRowIds
             });
         }
    },

any ideas how to improve this feature?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions