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.
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.
// Initialize the Spreadsheet component.
var data= [{
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'
}];
var sheet = [{
ranges: [{ dataSource: data }],
columns: [{ width: 80 }, { width: 80 },{ width: 80},
{ width: 160 }, { width: 100 }, {width: 150}]
}]
var spreadsheet = new ej.spreadsheet.Spreadsheet({
sheets: sheet
});
// Render initialized Spreadsheet.
spreadsheet.appendTo('#spreadsheet');
<!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/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-grids/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-spreadsheet/styles/material.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/shim.min.js"></script>
<script src="system.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<!--Element which is going to render-->
<div id="container">
<div id="spreadsheet"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
The local data source can also be provided as an instance of the
DataManager
. By default,DataManager
usesJsonAdaptor
for local data-binding.
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.
var query = new ej.data.Query().select([
'OrderID', 'CustomerID', 'ShipName', 'ShipCity', 'ShipCountry', 'Freight'
]).take(200);
//Initialize DataManager.
var data = new ej.data.DataManager({
url: 'https://js.syncfusion.com/demos/ejServices//wcf/Northwind.svc/Orders',
crossDomain: true
});
//Initialize Spreadsheet component.
var spreadsheet = new ej.spreadsheet.Spreadsheet({
sheets: [
{
name: 'Shipment Details',
rows: [{
cells: [{ value: 'Order ID' }, { value: 'Customer Name' }, { value: 'Freight' }, { value: 'Ship Name' },
{ value: 'Ship City' }, { value: 'Ship Country' }]
}],
ranges: [{ dataSource: data, query: query, showFieldAsHeader: false, startCell: 'A2' }],
columns: [{ width: 100 }, { width: 130 }, { width: 100 }, { width: 220 }, { width: 150 }, { width: 180 }]
}],
openUrl: 'https://services.syncfusion.com/js/production/api/spreadsheet/open',
saveUrl: 'https://services.syncfusion.com/js/production/api/spreadsheet/save',
});
//Render initialized Spreadsheet component.
spreadsheet.appendTo('#spreadsheet');
<!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/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-grids/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-spreadsheet/styles/material.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/shim.min.js"></script>
<script src="system.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<!--Element which is going to render-->
<div id="container">
<div id="spreadsheet"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
By default,
DataManager
uses ODataAdaptor for remote data-binding.
OData
is a standardized protocol for creating and consuming data. You can retrieve data from OData service using the DataManager. Refer to the following code example for remote Data binding using OData service.
//Initialize DataManager.
var data = new ej.data.DataManager({
url: 'https://ej2services.syncfusion.com/production/web-services/api/Orders',
adaptor: new ODataAdaptor(),
crossDomain: true
});
//Initialize Spreadsheet component.
var spreadsheet = new ej.spreadsheet.Spreadsheet({
sheets: [
{
name: 'Order details',
ranges: [{ dataSource: data }],
columns: [
{ width: 80 },
{ width: 80 },
{ width: 80 },
{ width: 80 },
{ width: 80 },
{ width: 80 },
{ width: 280 },
{ width: 180 },
{ width: 80 },
{ width: 180 },
{ width: 180 }
]
}
],
openUrl:
'https://services.syncfusion.com/js/production/api/spreadsheet/open',
saveUrl:
'https://services.syncfusion.com/js/production/api/spreadsheet/save',
created: function () {
//Applies cell and number formatting to specified range of the active sheet
spreadsheet.cellFormat(
{ fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' },
'A1:K1'
);
}
});
//Render initialized Spreadsheet component.
spreadsheet.appendTo('#spreadsheet');
<!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/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-grids/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-spreadsheet/styles/material.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/shim.min.js"></script>
<script src="system.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<!--Element which is going to render-->
<div id="container">
<div id="spreadsheet"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
You can use WebApiAdaptor to bind spreadsheet with Web API created using OData endpoint.
//Initialize DataManager.
var data = new ej.data.DataManager({
url: 'https://ej2services.syncfusion.com/production/web-services/api/Orders',
adaptor: new WebApiAdaptor(),
crossDomain: true
});
//Initialize Spreadsheet component.
var spreadsheet = new ej.spreadsheet.Spreadsheet({
sheets: [
{
name: 'Order details',
ranges: [{ dataSource: data }],
columns: [
{ width: 80 },
{ width: 80 },
{ width: 80 },
{ width: 80 },
{ width: 80 },
{ width: 80 },
{ width: 280 },
{ width: 180 },
{ width: 80 },
{ width: 180 },
{ width: 180 }
]
}
],
openUrl:
'https://services.syncfusion.com/js/production/api/spreadsheet/open',
saveUrl:
'https://services.syncfusion.com/js/production/api/spreadsheet/save',
created: function () {
//Applies cell and number formatting to specified range of the active sheet
spreadsheet.cellFormat(
{ fontWeight: 'bold', textAlign: 'center', verticalAlign: 'middle' },
'A1:K1'
);
}
});
//Render initialized Spreadsheet component.
spreadsheet.appendTo('#spreadsheet');
<!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/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-grids/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-spreadsheet/styles/material.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/shim.min.js"></script>
<script src="system.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<!--Element which is going to render-->
<div id="container">
<div id="spreadsheet"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
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.
// Initialize the Spreadsheet component.
var sheet = [{
rows: [{
index: 0,
cells: [
{ index: 0, value: 'Order ID', style: { fontWeight: 'bold' }},
{ value: 'Customer ID', style: { fontWeight: 'bold' }},
{ value: 'Employee ID', style: { fontWeight: 'bold' }},
{ value: 'Ship Name', style: { fontWeight: 'bold' }},
{ value: 'Ship City', style: { fontWeight: 'bold' }},
{ value: 'Ship Address', style: { fontWeight: 'bold' }}
]
},
{
cells: [
{ value: '10248' },
{ value: 'VINET' },
{ value: '5' },
{ value: 'Vins et alcools Chevalier' },
{ value: 'Reims' },
{ value: '59 rue de lAbbaye' }
]
},
{
cells: [
{ value: '10249' },
{ value: 'TOMSP' },
{ value: '6' },
{ value: 'Toms Spezialitäten' },
{ value: 'Münster' },
{ value: 'Luisenstr. 48' }
]
},
{
cells: [
{ value: '10250' },
{ value: 'HANAR' },
{ value: '4' },
{ value: 'Hanari Carnes' },
{ value: 'Rio de Janeiro' },
{ value: 'Rua do Paço, 67' }
]
},
{
cells: [
{ value: '10251' },
{ value: 'VICTE' },
{ value: '3' },
{ value: 'Victuailles en stock' },
{ value: 'Lyon' },
{ value: '2, rue du Commerce' }
]
}],
columns: [
{ width: 80 }, { width: 80 }, { width: 82 },
{ width: 160 }, { width: 110 }, { width: 130 }
]
}];
var spreadsheet = new ej.spreadsheet.Spreadsheet({
sheets: sheet,
});
// Render initialized Spreadsheet.
spreadsheet.appendTo('#spreadsheet');
<!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/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-grids/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-spreadsheet/styles/material.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/shim.min.js"></script>
<script src="system.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
</head>
<body>
<!--Element which is going to render-->
<div id="container">
<div id="spreadsheet"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
The cell data binding also supports formula, style, number format, and more.
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 benull
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 useedit
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 thedataSourceChanged
event with actionadd
.
var spreadsheet = new ej.spreadsheet.Spreadsheet({
showRibbon: false,
showFormulaBar: false,
sheets: [
{
ranges: [{ dataSource: data }],
columns: [
{ width: 90 }, { width: 100 }, { width: 96 },
{ width: 120 }, { width: 130 }, { width: 120 }
]
}],
dataSourceChanged: (args) => {
appendElement("Data source changed with" + "<b> " + 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) {
var span = document.createElement("span");
span.innerHTML = html;
var log = document.getElementById('EventLog');
log.insertBefore(span, log.firstChild);
}
<!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/21.2.3/ej2-base/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-inputs/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-buttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-splitbuttons/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-lists/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-navigations/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-popups/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-dropdowns/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-grids/styles/material.css" rel="stylesheet">
<link href="//cdn.syncfusion.com/ej2/21.2.3/ej2-spreadsheet/styles/material.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.4.1/shim.min.js"></script>
<script src="system.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/21.2.3/dist/ej2.min.js" type="text/javascript"></script>
<script src="es5-datasource.js" type="text/javascript"></script>
</head>
<body>
<!--Element which is going to render-->
<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>
</div><script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>