Search code examples
reactjsreact-data-grid

React-data-grid : Changing a row color depending on a value of the row


My component Test is called and gets in props an object containing datas from a mysql request (Select * from db.mytable). I store this.props.data in a state and render it in a ReactDataGrid.

What I'm trying to do is to update and change color of a row when a value of a cell is changed in my ReactDataGrid. For example, if a user right clicks on a rows, a context menu appear and he has the choice between Check and Uncheck. When he clicks on Check, i want to update the row and make it appear green.

I've got a database where I store the Checked state so when the user refreshes the page, the line should stay green.

How may I do this?


Solution

  • So when I click check in the context menu this function is called, it will update my state table containing the rows :

     //right click then check
      rowCheck = (rowIdx) => {
        var tableState=this.state.tableState.concat([])
        tableState[rowIdx.rowIdx].CHECKED='Y'
        this.setState({tableState})
      }
    

    the RowsRenderer function :

    RowRenderer = ({ renderBaseRow, ...props }) => {
        const color = this.state.tableState[props.idx].CHECKED==='Y' ? "blue" : "";
        return <div style={{color}}>{renderBaseRow(props)}</div>;
      };
    

    the data-grid :

    <ReactDataGrid
                columns={this.state.column}
                rowGetter={i => this.state.tableState[i]}
                rowsCount={this.state.tableState.length}
                minHeight={500} 
    
                enableCellSelect={true}
                onGridRowsUpdated={this.onGridRowsUpdated}
                cellNavigationMode={'changeRow'}
    
                rowRenderer={this.RowRenderer}
    
                contextMenu={
                  <ExampleContextMenu
                    onRowCheck={(e, {rowIdx})=>this.rowCheck({rowIdx})}
                    onRowUncheck={(e, { rowIdx }) => this.rowUncheck({rowIdx})}
                  />
                }
                RowsContainer={ContextMenuTrigger}
    
                rowSelection={{
                  showCheckbox: true,
                  enableShiftSelect: true,
                  onRowsSelected: this.onRowsSelected,
                  onRowsDeselected: this.onRowsDeselected,
                  selectBy: {
                    indexes: this.state.selectedIndexes
                  }
                }}
              />