0% found this document useful (0 votes)
11 views2 pages

AgGrid React Community Example

This document defines custom formatters and filters for columns in an AgGridReact table. It defines a numberValueFormatter to format numbers to two decimal places, and a saleValueFormatter to return 'N/A' for the value 1. It also defines custom filter parameters for number and sale columns to allow filtering of formatted numeric values. The GridExample component renders an AgGridReact table with this custom formatting and filtering applied.

Uploaded by

rohit kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

AgGrid React Community Example

This document defines custom formatters and filters for columns in an AgGridReact table. It defines a numberValueFormatter to format numbers to two decimal places, and a saleValueFormatter to return 'N/A' for the value 1. It also defines custom filter parameters for number and sale columns to allow filtering of formatted numeric values. The GridExample component renders an AgGridReact table with this custom formatting and filtering applied.

Uploaded by

rohit kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

'use strict';

import React, { useCallback, useMemo, useRef, useState } from 'react';


import { createRoot } from 'react-dom/client';
import { AgGridReact } from 'ag-grid-react';
import 'ag-grid-community/styles/[Link]';
import 'ag-grid-community/styles/[Link]';

var numberValueFormatter = function (params) {


return [Link](2);
[Link](params)
};

var saleFilterParams = {
allowedCharPattern: '\\d\\-\\,\\$',
numberParser: (text) => {
return text == null
? null
: parseFloat([Link](',', '.').replace('$', ''));
},
};
var customNumberFilterParams = {
applyCustomNumberFilter: (filterValue, cellValue) => {
// Parse the filter value and cell value as numbers
if(parseFloat(filterValue)===1){
return cellNumber === 'N/A'
}
const filterNumber = parseFloat(filterValue);
const cellNumber = parseFloat(cellValue);

// Check if the cell value passes the filter condition


// For example, filter out values greater than the filter number

return cellNumber === filterNumber;


}
};

var saleValueFormatter = function (params) {


if([Link]===1){
return 'N/A'
}
var formatted = [Link]
return formatted
};

const GridExample = () => {


const containerStyle = useMemo(() => ({ width: '100%', height: '100%' }), []);
const gridStyle = useMemo(() => ({ height: '100%', width: '100%' }), []);
const [rowData, setRowData] = useState(getData());
const [columnDefs, setColumnDefs] = useState([
{
field: 'sale',
headerName: 'Sale ($)',
filter: 'agNumberColumnFilter',
floatingFilter: true,

valueFormatter: saleValueFormatter,
filterParams: customNumberFilterParams
},
{
field: 'sale',
headerName: 'Sale',
filter: 'agNumberColumnFilter',
floatingFilter: true,
filterParams: saleFilterParams,

},
]);
const defaultColDef = useMemo(() => {
return {
flex: 1,
minWidth: 150,
};
}, []);

return (
<div style={containerStyle}>
<div style={gridStyle} className="ag-theme-alpine">
<AgGridReact
rowData={rowData}
columnDefs={columnDefs}
defaultColDef={defaultColDef}
></AgGridReact>
</div>
</div>
);
};

const root = createRoot([Link]('root'));


[Link](<GridExample />);

Common questions

Powered by AI

Having duplicate fields specified in the columnDefs, such as multiple 'sale' fields, might lead to unexpected behaviors because the grid would attempt to apply multiple configurations to the same column data. This can result in conflicts in filtering, sorting, and rendering operations, potentially degrading user experience. To mitigate, ensure unique field names or carefully manage their configurations to prevent conflicts .

Using strings such as '\d\-\,\$' in saleFilterParams could result in unintended parsing errors since they define a character pattern that indicates allowed characters for parsing. Misinterpretations or errors in pattern design can cause data mismatches or incorrect parsing outcomes. Mitigation strategies include thorough testing with diverse data inputs, and possibly using regular expressions judiciously to capture the intent accurately .

The GridExample component ensures responsiveness using containerStyle and gridStyle, both set to use 100% width and height. These style configurations are memoized with useMemo, allowing the AgGridReact component's layout to adapt flexibly to different screen sizes or container dimensions without inducing unnecessary re-renders due to style recalculations .

The saleValueFormatter function returns 'N/A' when the value is exactly 1. This implies that the literal value 1 is considered a special case where data is presented differently, potentially to indicate missing or default data. By formatting certain values differently, the grid can clearly communicate special conditions or statuses to users .

The defaultColDef property specifies common configurations applied to all columns, such as flex and minWidth. This approach contributes to the flexibility and maintainability of the grid layout by centralizing default styling and behavior configurations, thus reducing redundancy and ensuring consistency across the grid's column definitions, simplifying overall development and future modifications .

The useMemo hook in the provided code is used to create memoized versions of the containerStyle and gridStyle objects. By memoizing these styles, React avoids recalculating them on every render if their dependencies haven't changed, thus improving performance by reducing unnecessary recalculations and re-renders .

Floating filters provide a user-friendly way for quick access to filtering options directly in the grid header, enhancing interactivity and usability by allowing users to filter data on-the-fly. However, they can also clutter the interface and may create a steeper learning curve for users unfamiliar with filter parameters, especially in grids with numerous columns or complex data sets .

The createRoot function initializes a React application by creating a root to which the component will be rendered, reflecting a shift in React 18 towards improved rendering capabilities such as concurrent rendering. It replaces the deprecated ReactDOM.render, setting up the application for more efficient and feature-rich rendering processes .

The useState hook is used to manage the state of rowData and columnDefs in the GridExample component. This state management allows the component to dynamically update its grid data and column configurations without unmounting and remounting the component, providing a responsive interface to changes such as data fetching or user interactions .

The customNumberFilterParams object provides a custom number filtering mechanism for the AgGridReact component. It includes the applyCustomNumberFilter function, which parses and compares the filter value and cell value as numbers, allowing for specific filtering logic like filtering out cells by exact matches (e.g., values equal to the filter number).

You might also like