react-bootstrap-table icon indicating copy to clipboard operation
react-bootstrap-table copied to clipboard

Button click triggers onRowSelect

Open aree5741 opened this issue 10 years ago • 4 comments

I have added a column with buttons in my app and the table is set to allow row select as well. However, when a button is clicked, select row is also triggered. Is there a way to avoid this and only allow for the button click when clicked on top of the button ??

aree5741 avatar Jul 13 '16 05:07 aree5741

@AchinthaReemal, sorry for lately reply. so did you hope the user can select row only by clicking the selection columns? I means clickToSelect: true is that necessary to you?

BTW, if you really need the clickToSelect, I thinks you can implements onSelect and return false if the clicking is on specified column, which means will not select the row

function onRowSelect(row, isSelected, e) {
  // if column index is 2, will not trigger selection
  if (e.target.cellIndex === 2) return false;
}

const selectRowProp = {
  mode: 'checkbox',
  clickToSelect: true,
  onSelect: onRowSelect
};

export default class SelectHookTable extends React.Component {
  render() {
    return (
      <BootstrapTable data={ products } selectRow={ selectRowProp }>
          <TableHeaderColumn dataField='id' isKey={ true }>Product ID</TableHeaderColumn>
          <TableHeaderColumn dataField='name'>Product Name</TableHeaderColumn>
          <TableHeaderColumn dataField='price'>Product Price</TableHeaderColumn>
      </BootstrapTable>
    );
  }
}

AllenFang avatar Jul 14 '16 16:07 AllenFang

Hey @ AchinthaReemal i have the same issue than you. Did you solved it?

pablocordoba11 avatar Jun 07 '17 03:06 pablocordoba11

@pablocordoba11, above example doesn't help you?

AllenFang avatar Jun 08 '17 13:06 AllenFang

@AllenFang It does not work. Well, your example does work, but if you have something like this:

const buttonFormatter = (cell, row) => {
  return <Button onClick={() => someFunction(row)}>Click me</Button>;
};

return (
  <BootstrapTable data={props.data} striped>
    <TableHeaderColumn dataField={item.dataField} dataFormat={buttonFormatter}>
      {item.dataName}
    </TableHeaderColumn>
  </BootstrapTable>
);

It will not work. Button click returns undefined in e.target.cellIndex. However, one can make it work by having some boolean flag stored in state and switching it by clicking.

MarianSulgan avatar Nov 10 '17 10:11 MarianSulgan