Search results

Data Binding in JavaScript Spreadsheet control

14 Apr 2021 / 6 minutes to read

The Spreadsheet uses DataManager, which supports both RESTful JSON data services and local JavaScript object array binding to a range. The dataSource property can be assigned either with the instance of DataManager or JavaScript object array collection.

To bind data to a cell, use cell data binding support.

Local data

To bind local data to the Spreadsheet, you can assign a JavaScript object array to the dataSource property.

Refer to the following code example for local data binding.

Source
Preview
app.ts
index.html
Copied to clipboard
import { Spreadsheet } from '@syncfusion/ej2-spreadsheet';
import { data } from './datasource.ts';

//Initialize the SpreadSheet control
let spreadsheet: Spreadsheet = new Spreadsheet({
  sheets: [{
                ranges: [{ dataSource: data }],
                columns: [{ width: 80 }, { width: 80 },{ width: 80},
                          { width: 160 }, { width: 100 }, {width: 150}]
            }],
  }
});
spreadsheet.appendTo('#spreadsheet');
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
        <title>EJ2 SpreadSheet</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="description" content="Typescript UI Controls" />
        <meta name="author" content="Syncfusion" />
        <link rel="shortcut icon" href="resources/favicon.ico" />
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-lists/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-grids/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-spreadsheet/styles/material.css" rel="stylesheet" />
        <link href="styles.css" rel="stylesheet" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
        <script src="system.config.js"></script>

</head>

<body>
       <!--Element which is going to render-->
       <div id='loader'>Loading....</div>
       <div id='container'>
       <div id="spreadsheet"></div>
       </div>
</body>

</html>

The local data source can also be provided as an instance of the DataManager. By default, DataManager uses JsonAdaptor for local data-binding.

Remote data

To bind remote data to the Spreadsheet control, assign service data as an instance of DataManager to the dataSource property. To interact with remote data source, provide the service endpoint url.

Refer to the following code example for remote data binding.

Source
Preview
app.ts
index.html
Copied to clipboard
import { DataManager, ODataAdaptor } from '@syncfusion/ej2-data';
import { Spreadsheet } from '@syncfusion/ej2-spreadsheet';

class CustomAdaptor extends ODataAdaptor {
    public processResponse(): Object {
        let result: Object[] = [];
        let original: { result: Object[], count: number } = super.processResponse.apply(this, arguments);
        original.result.forEach((item: { SNo: number }, idx: number) => {
            result[idx] = {};
            Object.keys(item).forEach((key: string) => {
                if (['OrderID', 'CustomerID', 'ShipName', 'ShipCity', 'ShipCountry'].indexOf(key) > -1) {
                    result[idx][key] = item[key];
                }
            });
        });
        return { result: result, count: original.count };
    }
}
    //Initialize DataManager
    let data: DataManager = new DataManager({
        //Remote service url
        url: 'https://ej2services.syncfusion.com/production/web-services/api/Orders',
        adaptor: new CustomAdaptor,
        crossDomain: true
    });

    //Initialize Spreadsheet control
    let spreadsheet: Spreadsheet = new Spreadsheet({
        sheets: [
            {
                rows: [{
                     cells: [{ value: 'Order ID' }, { value: 'Customer Name' }, { value: 'Ship Name' },
                    { value: 'Ship City' }, { value: 'Ship Country' }]
                }],
                ranges: [{ dataSource: data, showFieldAsHeader: false, startCell: 'A2' }],
                columns: [
                    { width: 80 }, { width: 100 }, { width: 82 },
                    { width: 160 }, { width: 110 }, { width: 130 }
                ]
            }]
    });

    //Render the initialized Spreadsheet control
    spreadsheet.appendTo('#spreadsheet');
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
        <title>EJ2 SpreadSheet</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="description" content="Typescript UI Controls" />
        <meta name="author" content="Syncfusion" />
        <link rel="shortcut icon" href="resources/favicon.ico" />
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-lists/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-grids/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-spreadsheet/styles/material.css" rel="stylesheet" />
        <link href="styles.css" rel="stylesheet" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
        <script src="system.config.js"></script>

</head>

<body>
       <!--Element which is going to render-->
       <div id='loader'>Loading....</div>
       <div id='container'>
       <div id="spreadsheet"></div>
       </div>
</body>

</html>

By default, DataManager uses ODataAdaptor for remote data-binding.

Cell data binding

The Spreadsheet control can bind the data to individual cell in a sheet . To achive this you can use the value property.

Refer to the following code example for cell data binding.

Source
Preview
app.ts
index.html
Copied to clipboard
import { Spreadsheet } from '@syncfusion/ej2-spreadsheet';

//Initialize the SpreadSheet control
let spreadsheet: Spreadsheet = new Spreadsheet({
    sheets: [
        {
            name: 'Monthly Budget',
            selectedRange: 'D13',
            rows: [
                {
                    cells: [
                        { value: 'Category', style: { fontWeight: 'bold', textAlign: 'center' } },
                        { value: 'Planned cost', style: { fontWeight: 'bold', textAlign: 'center' } },
                        { value: 'Actual cost', style: { fontWeight: 'bold', textAlign: 'center' } },
                        { value: 'Difference', style: { fontWeight: 'bold', textAlign: 'center' } }
                    ]
                },
                {
                    cells: [
                        { value: 'Food' },
                        { value: '$7000' },
                        { value: '$8120' },
                        { formula: '=B2-C2', format: '$#,##0.00' }
                    ]
                },
                {
                    cells: [
                        { value: 'Loan' },
                        { value: '$1500' },
                        { value: '$1500' },
                        { formula: '=B3-C3', format: '$#,##0.00' }
                    ]
                },
                {
                    cells: [
                        { value: 'Medical' },
                        { value: '$300' },
                        { value: '$0' },
                        { formula: '=B4-C4', format: '$#,##0.00' }
                    ]
                },
                {
                    index: 5,
                    cells: [
                        { index: 2, value: 'Total Difference:', style: { fontWeight: 'bold', textAlign: 'right' } },
                        { formula: '=D2+D4', format: '$#,##0.00', style: { fontWeight: 'bold' } }
                    ]
                }
            ],
            columns: [
                { width: 110 }, { width: 115 }, { width: 110 }, { width: 100 }
            ]
        }
    ]
});

//Render the initialized SpreadSheet
spreadsheet.appendTo('#spreadsheet');
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
        <title>EJ2 SpreadSheet</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="description" content="Typescript UI Controls" />
        <meta name="author" content="Syncfusion" />
        <link rel="shortcut icon" href="resources/favicon.ico" />
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-lists/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-grids/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-spreadsheet/styles/material.css" rel="stylesheet" />
        <link href="styles.css" rel="stylesheet" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
        <script src="system.config.js"></script>

</head>

<body>
       <!--Element which is going to render-->
       <div id='loader'>Loading....</div>
       <div id='container'>
       <div id="spreadsheet"></div>
       </div>
</body>

</html>

The cell data binding also supports formula, style, number format, and more.

Dynamic data binding and Datasource change event

You can dynamically change the datasource of the spreadsheet by changing the dataSource property of the range object of the sheet. The dataSourceChanged event handler will be triggered when editing, inserting, and deleting a row in the datasource range. This event will be triggered with a parameter named action which indicates the edit, add and delete actions for the respective ones.

The following table defines the arguments of the dataSourceChanged event.

Property Type Description
action string Indicates the type of action such as edit, add, and delete performed in the datasource range.
data object[] Modified data for edit action; New data for add action; Deleted data for delete action.
rangeIndex number Specifies the range index of the datasource.
sheetIndex number Specifies the sheet index of the datasource.

For add action, the value for all the fields will be null in the data. In the case that you do not want the primary key field to be null which needs to be updated in the backend service, you can use edit action after updating the primary key field to update in the backend service.

For inserting a row at the end of the datasource range, you should insert a row below at the end of the range to trigger the dataSourceChanged event with action add.

Source
Preview
app.ts
datasource.ts
index.html
Copied to clipboard
import { Spreadsheet, DataSourceChangedEventArgs } from '@syncfusion/ej2-spreadsheet';
import { data, itemData } from './datasource.ts';

let spreadsheet: Spreadsheet = new Spreadsheet({
        showRibbon: false,
        showFormulaBar: false,
        sheets: [
            {
                ranges: [{ dataSource: data }],
                columns: [
                    { width: 90 }, { width: 100 }, { width: 96 },
                    { width: 120 }, { width: 130 }, { width: 120 }
                ]
            }],
        dataSourceChanged: (args: DataSourceChangedEventArgs) => {
            appendElement("Data source changed with" + "<b>&nbsp;" + args.action + "</b> action<hr>");
        }
});

spreadsheet.appendTo('#spreadsheet');

document.getElementById('changeDataBtn').addEventListener('click', ()=> {
    spreadsheet.sheets[0].ranges[0].dataSource = itemData;
});

document.getElementById('clearBtn').addEventListener('click', ()=> {
    document.getElementById('EventLog').innerHTML = "";
});

function appendElement(html: string): void {
     let span: HTMLElement = document.createElement("span");
     span.innerHTML = html;
     let log: HTMLElement = document.getElementById('EventLog');
     log.insertBefore(span, log.firstChild);
}
Copied to clipboard
export let data: Object[] = [{
  OrderID: 10248,
  CustomerID: 'VINET',
  EmployeeID: 5,
  ShipName: 'Vins et alcools Chevalier',
  ShipCity: 'Reims',
  ShipAddress: '59 rue de lAbbaye'
},
{
  OrderID: 10249,
  CustomerID: 'TOMSP',
  EmployeeID: 6,
  ShipName: 'Toms Spezialitäten',
  ShipCity: 'Münster',
  ShipAddress: 'Luisenstr. 48'
},
{
  OrderID: 10250,
  CustomerID: 'HANAR',
  EmployeeID: 4,
  ShipName: 'Hanari Carnes',
  ShipCity: 'Rio de Janeiro',
  ShipAddress: 'Rua do Paço, 67'
},
{
  OrderID: 10251,
  CustomerID: 'VICTE',
  EmployeeID: 3,
  ShipName: 'Victuailles en stock',
  ShipCity: 'Lyon',
  ShipAddress: '2, rue du Commerce'
},
{
  OrderID: 10252,
  CustomerID: 'SUPRD',
  EmployeeID: 4,
  ShipName: 'Suprêmes délices',
  ShipCity: 'Charleroi',
  ShipAddress: 'Boulevard Tirou, 255'
}];

export let itemData: 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
  }
];
Copied to clipboard
<!DOCTYPE html>
<html lang="en">

<head>
        <title>EJ2 SpreadSheet</title>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="description" content="Typescript UI Controls" />
        <meta name="author" content="Syncfusion" />
        <link rel="shortcut icon" href="resources/favicon.ico" />
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-base/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-inputs/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-buttons/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-lists/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-navigations/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-popups/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-dropdowns/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-grids/styles/material.css" rel="stylesheet" />
        <link href="//cdn.syncfusion.com/ej2/ej2-spreadsheet/styles/material.css" rel="stylesheet" />
        <link href="styles.css" rel="stylesheet" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
        <script src="system.config.js"></script>

</head>

<body>
       <!--Element which is going to render-->
<div id='loader'>Loading....</div>
<div id='container'>
        <div>
                <div>
                        <button id="changeDataBtn" class='e-btn'>Change Datasource</button>
                        <div id="spreadsheet"></div>
                </div>
                <div>
                        <h4><b>Event Trace</b></h4>
                        <div id="evt">
                                <div style="height:173px;overflow: auto;min-width: 250px;">
                                        <span id="EventLog" style="word-break: normal;"></span>
                                </div>
                                <button id="clearBtn" class='e-btn'>Clear</button>
                        </div>
                </div>
        </div>
</body>

</html>

See Also