DEV Community

Sanskar Gupta
Sanskar Gupta

Posted on

3 1

Ag-grid hacks 1 - getting updated rows

Prologue

Implementing ag-grid functionalities can be quite frustrating at times especially when you are handling the enterprise version, the ag-grid hacks series will be considerate in reducing implementation time and reducing the extent of frustration that may arise scavenging the internet for sample implementations.

Introduction

To all who are not familiar: Ag-grid is a library to implement data grids and charts in your project , where data changes can be in real time as well, the community version is free while you need to pay a hefty amount to get the enterprise version which is loaded with more features.

Problem Statement

It becomes difficult at times to get the list of all recent rows in the from of objects while dealing with an editable ag-grid as there is no straight api like getUpdatedRows() in ag grid documentation though we can trigger a method when cell value changes.
Link

Solution

Consider the following piece of code:



import React from 'react'
import { AgGridReact } from 'ag-grid-react'
import 'ag-grid-community/dist/styles/ag-grid.css'
import 'ag-grid-community/dist/styles/ag-theme-balham.css'
import { getColumnDefs } from './ColumnDefUtils'

const columnDefs=[
{
      headerName: 'City',
      field: 'type',
      filter: 'text',
      editable: true,
    },
    {
      headerName: 'Population',
      field: 'children',
      filter: 'text',
      editable: true,
    },{
      headerName: 'Currency',
      field: 'children',
      filter: 'text',
      editable: true,
    },

]



class Example extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      rowData: [],
    }
  }

  componentDidMount() {
    methodToFetchDataFromBackend()
      .then((response) => {
        this.setState({ rowData: response.data })
      })
      .catch(() => {})
  }

  getUpdatedRows = () => {
    return this.state.rowData.filter((row) => row.changed === true)
  }

  handleSave = async () => {
    // filter the row data(this.state.rowData) where the changed in true and send ony those objects to backend
    saveMethod(this.getUpdatedRows())
  }

  onCellValueChanged = (params) => {
    this.setState({ saveButtonDisableFlag: false })
    params.node.data['changed'] = true
  }

  render() {
    return (
      <AgGridReact
        columnDefs={columnDefs}
        rowData={this.state.rowData}
        onCellValueChanged={this.onCellValueChanged}
      />
    )
  }
}



Enter fullscreen mode Exit fullscreen mode

Consider the rowData being updated when the component mounts by fetching data from backend. Ag grid api provides an inbuilt method "onCellValueChanged" that is triggered when we edit the grid.
Ag grid api methods: link

The line params.node.data['changed'] = true inserts an extra field to the row object whose cell underwent change.
Note that, here params contains all information about the row and cell.
Here is the sample params object
Alt Text

It contains the oldValue , newValue , the data is the rowData for which the cell belongs.
In the above code getUpdatedRows filters the data by "changed " field and sends only the updated rows to the backend for saving.

This is how one can get hold of updated rows by a simple hack!

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly β€” using the tools and languages you already love!

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay