Hide and show in React Spreadsheet component
10 Jul 202618 minutes to read
You can show or hide the rows and columns in the spreadsheet through property binding, method, and context menu.
Row
The rows can be hidden or shown through the following ways,
- Using
hiddenproperty in row, you can hide/show the rows at initial load. - Using
hideRowmethod, you can hide the rows by specifying the start and end row index, set the last argumenthideasfalseto unhide the hidden rows. - Right-click on the row header and select the desired option from context menu
Column
The columns can be hidden or shown through following ways,
- Using
hiddenproperty in columns, you can hide/show the columns at initial load. - Using
hideColumnmethod, you can hide the columns by specifying the start and end column index, set the last argumenthideasfalseto unhide the hidden columns. - Right-click on the column header and select the desired option from context menu
The following code example shows the hide/show rows and columns operation in the spreadsheet.
import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent, SheetsDirective, SheetDirective, RangesDirective, RowsDirective, RowDirective } from '@syncfusion/ej2-react-spreadsheet';
import { RangeDirective, ColumnsDirective, ColumnDirective } from '@syncfusion/ej2-react-spreadsheet';
import { data } from './datasource';
function App() {
const spreadsheetRef = React.useRef(null);
React.useEffect(() => {
let spreadsheet = spreadsheetRef.current;
if (spreadsheet) {
spreadsheet.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:H1');
// Unhide the 2nd index hidden column
spreadsheet.hideColumn(1, 1, false);
// Unhide the 3rd index hidden row
spreadsheet.hideRow(3, 3, false);
// Hiding the 6th index column
spreadsheet.hideColumn(6);
// Hiding the 8th and 9th index row
spreadsheet.hideRow(8, 9);
spreadsheet.cellFormat({ textAlign: 'center' }, 'D2:H11');
}
}, []);
return (
<div>
<SpreadsheetComponent ref={spreadsheetRef} showSheetTabs={false} showRibbon={false} showFormulaBar={false} >
<SheetsDirective>
<SheetDirective>
<RangesDirective>
<RangeDirective dataSource={data}></RangeDirective>
</RangesDirective>
<RowsDirective>
<RowDirective index={2} hidden={true}></RowDirective>
<RowDirective hidden={true}></RowDirective>
</RowsDirective>
<ColumnsDirective>
<ColumnDirective width={150}></ColumnDirective>
<ColumnDirective width={100} hidden={true}></ColumnDirective>
<ColumnDirective width={100} hidden={true}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
</ColumnsDirective>
</SheetDirective>
</SheetsDirective>
</SpreadsheetComponent>
</div>
);
};
export default App;
const root = createRoot(document.getElementById('root'));
root.render(<App />);import * as React from 'react';
import { createRoot } from 'react-dom/client';
import { SpreadsheetComponent, SheetsDirective, SheetDirective, RangesDirective, RowsDirective, RowDirective } from '@syncfusion/ej2-react-spreadsheet';
import { RangeDirective, ColumnsDirective, ColumnDirective } from '@syncfusion/ej2-react-spreadsheet';
import { data } from './datasource';
function App() {
const spreadsheetRef = React.useRef<SpreadsheetComponent>(null);
React.useEffect(() => {
let spreadsheet = spreadsheetRef.current;
if (spreadsheet) {
spreadsheet.cellFormat({ fontWeight: 'bold', textAlign: 'center' }, 'A1:H1');
// Unhide the 2nd index hidden column
spreadsheet.hideColumn(1, 1, false);
// Unhide the 3rd index hidden row
spreadsheet.hideRow(3, 3, false);
// Hiding the 6th index column
spreadsheet.hideColumn(6);
// Hiding the 8th and 9th index row
spreadsheet.hideRow(8, 9);
spreadsheet.cellFormat({ textAlign: 'center' }, 'D2:H11');
}
}, []);
return (
<div>
<SpreadsheetComponent ref={spreadsheetRef} showSheetTabs={false} showRibbon={false} showFormulaBar={false} >
<SheetsDirective>
<SheetDirective>
<RangesDirective>
<RangeDirective dataSource={data}></RangeDirective>
</RangesDirective>
<RowsDirective>
<RowDirective index={2} hidden={true}></RowDirective>
<RowDirective hidden={true}></RowDirective>
</RowsDirective>
<ColumnsDirective>
<ColumnDirective width={150}></ColumnDirective>
<ColumnDirective width={100} hidden={true}></ColumnDirective>
<ColumnDirective width={100} hidden={true}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
<ColumnDirective width={80}></ColumnDirective>
</ColumnsDirective>
</SheetDirective>
</SheetsDirective>
</SpreadsheetComponent>
</div>
);
};
export default App;
const root = createRoot(document.getElementById('root')!);
root.render(<App />);/**
* Hide/show data source
*/
export let data = [
{ 'Item Name': 'Casual Shoes', 'Date': '02/14/2019', 'Time': '11:34:32 AM', 'Quantity': 10, 'Price': 20, 'Amount': '=D2*E2', 'Discount': 1, 'Profit': 10 },
{ 'Item Name': 'Sports Shoes', 'Date': '06/11/2019', 'Time': '05:56:32 AM', 'Quantity': 20, 'Price': 30, 'Amount': '=D3*E3', 'Discount': 5, 'Profit': 50 },
{ 'Item Name': 'Formal Shoes', 'Date': '07/27/2019', 'Time': '03:32:44 AM', 'Quantity': 20, 'Price': 15, 'Amount': '=D4*E4', 'Discount': 7, 'Profit': 27 },
{ 'Item Name': 'Sandals & Floaters', 'Date': '11/21/2019', 'Time': '06:23:54 AM', 'Quantity': 15, 'Price': 20, 'Amount': '=D5*E5', 'Discount': 11, 'Profit': 67 },
{ 'Item Name': 'Flip- Flops & Slippers', 'Date': '06/23/2019', 'Time': '12:43:59 AM', 'Quantity': 30, 'Price': 10, 'Amount': '=D6*E6', 'Discount': 10, 'Profit': 70 },
{ 'Item Name': 'Sneakers', 'Date': '07/22/2019', 'Time': '10:55:53 AM', 'Quantity': 40, 'Price': 20, 'Amount': '=D7*E7', 'Discount': 13, 'Profit': 66 },
{ 'Item Name': 'Running Shoes', 'Date': '02/04/2019', 'Time': '03:44:34 AM', 'Quantity': 20, 'Price': 10, 'Amount': '=D8*E8', 'Discount': 3, 'Profit': 14 },
{ 'Item Name': 'Loafers', 'Date': '11/30/2019', 'Time': '03:12:52 AM', 'Quantity': 31, 'Price': 10, 'Amount': '=D9*E9', 'Discount': 6, 'Profit': 29 },
{ 'Item Name': 'Cricket Shoes', 'Date': '07/09/2019', 'Time': '11:32:14 AM', 'Quantity': 41, 'Price': 30, 'Amount': '=D10*E10', 'Discount': 12, 'Profit': 166 },
{ 'Item Name': 'T-Shirts', 'Date': '10/31/2019', 'Time': '12:01:44 AM', 'Quantity': 50, 'Price': 10, 'Amount': '=D11*E11', 'Discount': 9, 'Profit': 55 }
];/**
* Hide/show data source
*/
export let data: Object[] = [
{ 'Item Name': 'Casual Shoes', 'Date': '02/14/2019', 'Time': '11:34:32 AM', 'Quantity': 10, 'Price': 20, 'Amount': '=D2*E2', 'Discount': 1, 'Profit': 10 },
{ 'Item Name': 'Sports Shoes', 'Date': '06/11/2019', 'Time': '05:56:32 AM', 'Quantity': 20, 'Price': 30, 'Amount': '=D3*E3', 'Discount': 5, 'Profit': 50 },
{ 'Item Name': 'Formal Shoes', 'Date': '07/27/2019', 'Time': '03:32:44 AM', 'Quantity': 20, 'Price': 15, 'Amount': '=D4*E4', 'Discount': 7, 'Profit': 27 },
{ 'Item Name': 'Sandals & Floaters', 'Date': '11/21/2019', 'Time': '06:23:54 AM', 'Quantity': 15, 'Price': 20, 'Amount': '=D5*E5', 'Discount': 11, 'Profit': 67 },
{ 'Item Name': 'Flip- Flops & Slippers', 'Date': '06/23/2019', 'Time': '12:43:59 AM', 'Quantity': 30, 'Price': 10, 'Amount': '=D6*E6', 'Discount': 10, 'Profit': 70 },
{ 'Item Name': 'Sneakers', 'Date': '07/22/2019', 'Time': '10:55:53 AM', 'Quantity': 40, 'Price': 20, 'Amount': '=D7*E7', 'Discount': 13, 'Profit': 66 },
{ 'Item Name': 'Running Shoes', 'Date': '02/04/2019', 'Time': '03:44:34 AM', 'Quantity': 20, 'Price': 10, 'Amount': '=D8*E8', 'Discount': 3, 'Profit': 14 },
{ 'Item Name': 'Loafers', 'Date': '11/30/2019', 'Time': '03:12:52 AM', 'Quantity': 31, 'Price': 10, 'Amount': '=D9*E9', 'Discount': 6, 'Profit': 29 },
{ 'Item Name': 'Cricket Shoes', 'Date': '07/09/2019', 'Time': '11:32:14 AM', 'Quantity': 41, 'Price': 30, 'Amount': '=D10*E10', 'Discount': 12, 'Profit': 166 },
{ 'Item Name': 'T-Shirts', 'Date': '10/31/2019', 'Time': '12:01:44 AM', 'Quantity': 50, 'Price': 10, 'Amount': '=D11*E11', 'Discount': 9, 'Profit': 55 }
];