How to display data in a specific column order in React Spreadsheet?
This article explains how to display data in a specific column order in React Spreadsheet. Initially, specify the order of the data fields that you want to display on the sheet. Then, using the updateCell method, set the column header value in the specified order.
[index.js]
import { createRoot } from 'react-dom/client';
import './index.css';
import * as React from 'react';
import {
SpreadsheetComponent,
SheetsDirective,
SheetDirective,
ColumnsDirective,
ColumnDirective,
getCellAddress,
getSheetName
} from '@syncfusion/ej2-react-spreadsheet';
import { defaultData } from './data';
function App() {
let spreadsheet;
const onCreated = () => {
// Specify the order that you want to display on the sheet.
let fieldMapping = [
'Customer Name',
'Model',
'Color',
'Payment Mode',
'Delivery Date',
'Amount',
'2023',
];
updateCellValue(defaultData, fieldMapping);
spreadsheet.cellFormat({ fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' }, 'A1:G1');
}
const updateCellValue = (data, field) => {
let sheetIdx = spreadsheet.activeSheetIndex;
// Set the column header using specified field.
field.forEach(function (field, i) {
// UpdateCell method is used to update the properties of a cell.
spreadsheet.updateCell({ value: field }, getSheetName(spreadsheet, sheetIdx) + '!' + getCellAddress(0, i));
});
// Loop the data source and get and set the proper cell value based on specified field mapping.
data.forEach(function (item, i) {
for (let j = 0; j < field.length; j++) {
spreadsheet.updateCell({ value: item[field[j]] }, getSheetName(spreadsheet, sheetIdx) + '!' + getCellAddress(i + 1, j));
}
});
// To refresh the sheet with updated cell values.
spreadsheet.resize();
}
return (
<div classname="control-section spreadsheet-control">
<spreadsheetcomponent ref="{(ssObj)" ==""> {
spreadsheet = ssObj;
}}
created={onCreated}
>
<sheetsdirective>
<sheetdirective name="Car Sales Report">
<columnsdirective>
<columndirective width="{180}"></columndirective>
<columndirective width="{130}"></columndirective>
<columndirective width="{130}"></columndirective>
<columndirective width="{180}"></columndirective>
<columndirective width="{130}"></columndirective>
<columndirective width="{120}"></columndirective>
</columnsdirective>
</sheetdirective>
</sheetsdirective>
</spreadsheetcomponent>
</div>
);
}
export default App;
const root = createRoot(document.getElementById('sample'));
root.render(<app>);
Sample Link:
Refer to the working sample for additional details and implementation: Sample
Output:
Conclusion
We hope you enjoyed learning about how to display data in a specific column order in React Spreadsheet.
You can refer to our React Spreadsheet’s feature tour page to learn about its other groundbreaking feature representations and documentation. You can also explore our React Spreadsheet example to understand how to present and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our React Spreadsheet and other components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!