Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting multiple rows with React-Table? (Shift + Click)

does anyone know a way to select multiple rows with React-Table. Say if we wish to click a cell in a row and then press SHIFT and then select another row and perhaps color code the selected rows with CSS? Is this even possible?

like image 940
Rachel Avatar asked Jul 22 '26 07:07

Rachel


1 Answers

I found a way to do it. Let me know if you have any questions. Basically just need to implement on your own.

   state = {
    selected: null,
    selectedRows: [],
  }

  previousRow = null;

  handleClick = (state, rowInfo) => {
    if (rowInfo) {
      return {
        onClick: (e) => {
          let selectedRows = [];
          // if shift key is being pressed upon click and there is a row that was previously selected, grab all rows inbetween including both previous and current clicked rows
          if (e.shiftKey && this.previousRow) {
            // if previous row is above current row in table
            if (this.previousRow.index < rowInfo.index) {
              for (let i = this.previousRow.index; i <= rowInfo.index; i++) {
                selectedRows.push(state.sortedData[i]);
              }
            // or the opposite
            } else {
              for (let i = rowInfo.index; i <= this.previousRow.index; i++) {
                selectedRows.push(state.sortedData[i]);
              }
            }
          } else {
            // add _index field so this entry is same as others from sortedData
            rowInfo._index = rowInfo.index;
            selectedRows.push(rowInfo);
            this.previousRow = rowInfo;
          }
          this.setState({ selected: rowInfo.index, selectedRows })
        },
        style: {
          // check index of rows in selectedRows against all rowInfo's indices, to match them up, and return true/highlight row, if there is a index from selectedRows in rowInfo/the table data
          background: this.state.selectedRows.some((e) => e._index === rowInfo.index) && '#9bdfff',
        }
      }
    } else {
      return {}
    }
like image 80
Young Scooter Avatar answered Jul 24 '26 22:07

Young Scooter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!