SpreadJS is an extremely powerful Excel-like spreadsheet component. SpreadJS is framework agnostic, meaning SpreadJS supports being used within several JavaScript frameworks. This includes Angular!
One of the powerful features of SpreadJS is data binding. We have created a tutorial that breaks down how to use Data Binding in SpreadJS with an Angular project.
Angular Project Set-Up
We first will need to set-up an Angular project. For this tutorial we will be using an Angular 10 project.
Create new Angular 10 project: ng new project-name. For this example, the project is named spreadJS-angular:
Now, we will change the directory to our new project
Run your project:
Notice the Angular 10 welcome page is now being served to: http://localhost:4200/
Add SpreadJS and Data Binding to an Angular Project
Now we have an Angular 10 project up and running we are going to remove its welcome page contents and add SpreadJS!
First, install SpreadJS using the npm package: npm install @grapecity/spread-sheets & npm install @grapecity/spread-sheets-angular. Then, configure the SpreadJS CSS in angular.json as shown below:
Now we will use SpreadJS in our application. We will modify the module.ts for importing the SpreadJS module:
Modify the componenet.html for viewing the SpreadJS component:
Modify the component.ts to prepare the data for the SpreadJS component:
Run ng serve –open. In your browser, http://localhost:4200/ you will now see the SpreadJS component with a list of items:
Note, if you receive an error like this:
It can fix by copying the Angular Wrapper typescript source file into the project and refer as a file module.
Here are steps to follow:
- npm install, then copy the spread.sheets.angular.ts (only this file) from _nodemodules/@grapecity/spread-sheets-angular/dist folder to src/component/spread-sheets-angular folder
- Modify the module.ts:
- Use ng build –prod to build again
- Run ng serve –open
SpreadJS Data Binding with JSON Data from URL
Get JSON from URL by first storing the URL in a variable and then fetching the JSON data from the URL and have the response be returned as a JSON object.
We will create a custom function that will take specified data, then create a variable to store the JSON data and a variable for each column in the data source:
Next, set the data source using the worksheets setDataSource method and then we will bind the columns to a specified data field using the bindColumn method.
Finally, we will set the column count to 7 and set the Spread instances options scrollbarMaxAlign to True to specify for the scrollbar to align with the last row and column of the active sheet remove the additional grey space.
By using the code shared here in this tutorial, your SpreadJS instance will now have the data coming from a URL being bound to specified columns in your Angular project: