question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to customize expanded row that click on save button unselect the row

See original GitHub issue

screen shot 2017-01-03 at 5 59 58 pm I have a customized table implementing with react-bootstrap-table. When I click the row, it expands with red highlight on the row. In the expanded row, I can edit fields and save accordingly. I want to close the expanded row on successful save, and remove the red color highlight. Currently, I can successfully save the record and collapse the expanded row, and here is the code: `class ClubsTable extends Component {

constructor(props) {
    super(props);
}

render() {

    const onRowSelect = ({_id}, isSelected) => {
        // add to selectedRow array if selected
        if (isSelected) {
            this.props.selectClub(_id);
        } else {
            // delete from selectedRow array if unselected
            this.props.unSelectClub(_id);
        }
    };

    const options = {
        expandRowBgColor: 'gray-lighter', // background color
        sizePerPageList: [10, 15, 20, 50, 100], // Dropdown Options for rows per page
        sizePerPage: 10,
        sortName: 'club_name',
        sortOrder: 'asc'
    };

    const selectRow = {
        mode: 'checkbox', //radio or checkbox
        className: 'clicked-row', // red color
        clickToSelect: true,
        onSelect: onRowSelect,
        // selected: this.props.selectedClubs
    };

    const isExpandableRow = (row) => {
        // console.log(this.props.closeExpandedRow);
        return (row._id && (_.contains(this.props.selectedClubs, row._id)))
    };

    return (
        <BootstrapTable
            data={ this.props.clubs }
            options={ options }
            selectRow={ selectRow }
            expandableRow={ isExpandableRow }
            expandComponent={ (row) => {return (<ClubDetail club={ row }/>)} }
            exportCSV
            search
            pagination>
            <TableHeaderColumn dataField='club_name' isKey={ true } dataSort={ true }>Club
                Name</TableHeaderColumn>
            <TableHeaderColumn dataField='organization_name' dataSort={ true }>Organization
                Name</TableHeaderColumn>
            <TableHeaderColumn dataField='post_code' dataSort={ true }>Postal Code</TableHeaderColumn>
            <TableHeaderColumn dataField='city' dataSort={ true }>City</TableHeaderColumn>
            <TableHeaderColumn dataField='phone' dataSort={ true }>Phone Number</TableHeaderColumn>
            <TableHeaderColumn dataField='contact_name' dataSort={ true }>Contact Name</TableHeaderColumn>
        </BootstrapTable>
    )
}

}`

However, I could not figure out how to also unselect the row so that the red color would be removed. I think the isSelected is not updated when I programmatically unselect the the row. And also, when I preselect rows with ‘selected: this.props.selectedClubs’, the row color will not be updated.

Any ideas?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

9reactions
AllenFangcommented, Jan 3, 2017

HI @ZhuQinglei, this is a similar issue that you can check it out.

First, I think you can not expand the row when click on row, because you dont enable clickToExpand in selectRow props.

Second, following is a example:

class ExpandRow extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: []
    };
  }

  isExpandableRow(row) {
    if (row.id < 3) return true;
    else return false;
  }

  handleSaveBtnClick = id => {
    // unselect row when click save button
    this.setState({ selected: this.state.selected.filter(it => it !== id) });
  }

  expandComponent = row => {
    return (
      <div>
        <button onClick={ () => this.handleSaveBtnClick(row.id) }>Save me</button>
      </div>
    );
  }

  render() {
    const onRowSelect = ({ id }, isSelected) => {
      if (isSelected) {
        this.setState({
          selected: [ ...this.state.selected, id ]
        });
      } else {
        this.setState({ selected: this.state.selected.filter(it => it !== id) });
      }
      return false;  //important
    };

    const selectRowProp = {
      mode: 'checkbox',
      clickToSelect: true,
      clickToExpand: true,   // you should add this to trigger selection and expand both on clicking
      onSelect: onRowSelect,  // manage the selection state
      bgColor: 'red',
      selected: this.state.selected   // set selected as a state data
    };

    const options = {
      expandRowBgColor: 'rgb(242, 255, 163)'
    };
    return (
      <BootstrapTable data={ products }
        options={ options }
        selectRow={ selectRowProp }
        expandableRow={ this.isExpandableRow }
        expandComponent={ this.expandComponent }>
        <TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
        <TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
        <TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
      </BootstrapTable>
    );
  }
}

Third, it’s important, the above example can not collapse the row on click save button, I can only unselect the row, I think it’s a enhancement for expand row, user should have the ability to handle which row is expandable just like row selection. so I’ll do it quickly. As I mentioned in #927, expandable row is a new feature, so there’s must be a lots of thing I need to improve, so welcome to tell me if you have any concern.

BTW, you forgot to add react-bootstrap-table css(react-bootstrap-table-all.min.css) into your repo, check the README, because I saw a gap between the header and body(Actually, react-bootstrap-table construct by two different and separated table).

Please let me know if you still have any trouble and I’ll improve expandable row as soon as possible. Thanks

1reaction
AllenFangcommented, Jun 14, 2017

@nikodonnell, I’ll add to docs ASAP and let me know if you got some trouble, thanks. but please open another issue and give more detail or example codes, thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

Programatically close an expanded row with a button
Problem is that I cannot get a row to expand or close without clicking on it. For example : When I click on...
Read more >
Select or Deselect Items on Row Click - Kendo UI Grid for jQuery
To deselect a row or select multiple rows by row clicking and without holding the Ctrl key, use the following approach. Edit Preview...
Read more >
Documentation: DevExtreme - JavaScript Data Grid Methods
To expand a group row, call this method with an array (each member of which is a grouping value). To expand a master...
Read more >
Work with a List, Record, or Table structured column ...
Expand a List structured column · To create a row for each list value, select Expand to New Rows. · To create a...
Read more >
Child rows (show extra / detailed information)
on('click', 'td.dt-control', function () { var tr = $(this).closest('tr'); var row = table.row(tr); if ( ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found