Apply conditional formatting for specific row or column in React
13 Sep 20257 minutes to read
Conditional formatting allows you to visually highlight important data and identify patterns in your React Pivot Table by applying formatting to specific rows or columns based on certain conditions. This feature helps to emphasize particular data values that meet defined criteria.
Implementing Conditional Formatting
You can apply conditional formatting to specific rows or columns using the label
option in the Pivot Table. The formatting is configured through the conditionalFormatSettings
option during initial rendering.
The following configuration options are required:
-
label
: Specifies the header name to apply conditions for a specific row or column -
conditions
: Defines the type of comparison operator such asEquals
,GreaterThan
, orLessThan
. -
value1
: Sets the comparison start value -
value2
: Sets the comparison end value (used forBetween
andNotBetween
conditions). -
style
: Defines the CSS styling to apply when the condition is met (background color, font color, etc.)
To use the conditional formatting feature, You need to inject the ConditionalFormatting
module in pivot table.
The code example below shows how to apply conditional formatting to the Germany row, highlighting cells with values between 500 and 50,000 in a specific style.
import { ConditionalFormatting, Inject, PivotViewComponent } from '@syncfusion/ej2-react-pivotview';
import * as React from 'react';
import { pivotData } from './datasource';
function App() {
let dataSourceSettings = {
dataSource: pivotData,
expandAll: false,
enableSorting: true,
drilledMembers: [{ name: 'Country', items: ['France', 'Germany'] }],
columns: [{ name: 'Year' }, { name: 'Order_Source', caption: 'Order Source' }],
rows: [{ name: 'Country' }, { name: 'Products' }],
values: [{ name: 'In_Stock', caption: 'In Stock' },
{ name: 'Sold', caption: 'Units Sold' }],
filters: [{ name: 'Product_Categories', caption: 'Product Categories' }],
conditionalFormatSettings: [
{
label: 'Germany',
conditions: 'Between',
value1: 500,
value2: 50000,
style: {
backgroundColor: '#f48fb1',
color: 'black',
fontFamily: 'Tahoma',
fontSize: '12px'
}
}
]
};
let pivotObj;
return (<PivotViewComponent ref={d => pivotObj = d} id='PivotView' height={350} dataSourceSettings={dataSourceSettings} allowConditionalFormatting={true}><Inject services={[ConditionalFormatting]}/> </PivotViewComponent>);
};
export default App;
import { ConditionalFormatting, IDataSet, Inject, PivotViewComponent } from '@syncfusion/ej2-react-pivotview';
import { DataSourceSettingsModel } from '@syncfusion/ej2-pivotview/src/model/datasourcesettings-model';
import * as React from 'react';
import { pivotData } from './datasource';
function App() {
let dataSourceSettings: DataSourceSettingsModel = {
dataSource: pivotData,
expandAll: false,
enableSorting: true,
drilledMembers: [{ name: 'Country', items: ['France', 'Germany'] }],
columns: [{ name: 'Year' }, { name: 'Order_Source', caption: 'Order Source' }],
rows: [{ name: 'Country' }, { name: 'Products' }],
values: [{ name: 'In_Stock', caption: 'In Stock' },
{ name: 'Sold', caption: 'Units Sold' }],
filters: [{ name: 'Product_Categories', caption: 'Product Categories' }],
conditionalFormatSettings: [
{
label: 'Germany',
conditions: 'Between',
value1: 500,
value2: 50000,
style: {
backgroundColor: '#f48fb1',
color: 'black',
fontFamily: 'Tahoma',
fontSize: '12px'
}
}
]
}
let pivotObj: PivotViewComponent;
return (<PivotViewComponent ref={ (d: PivotViewComponent) => pivotObj = d } id='PivotView' height={350} dataSourceSettings={dataSourceSettings} allowConditionalFormatting={true} ><Inject services={[ConditionalFormatting]}/> </PivotViewComponent>);
};
export default App;