Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (152).NET Core  (26)Angular  (49)ASP.NET  (48)ASP.NET Core  (64)ASP.NET MVC  (86)Azure  (29)Blazor  (106)DocIO  (20)Essential JS 2  (72)Essential Studio  (163)File Formats  (20)Flutter  (114)JavaScript  (150)Microsoft  (102)PDF  (59)React  (33)Succinctly series  (124)Syncfusion  (489)TypeScript  (30)Uno Platform  (3)UWP  (4)Vue  (26)Webinar  (27)Windows Forms  (55)WinUI  (26)WPF  (122)Xamarin  (132)XlsIO  (24)Other CategoriesBarcode  (4)BI  (29)Bold BI  (3)Build conference  (6)Business intelligence  (53)Button  (4)C#  (108)Chart  (53)Cloud  (10)Company  (445)Dashboard  (6)Data Science  (3)Data Validation  (5)DataGrid  (49)Development  (263)Doc  (7)DockingManager  (1)eBook  (94)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (20)Extensions  (15)File Manager  (4)Gantt  (9)Gauge  (7)Git  (4)Grid  (27)HTML  (9)Installer  (2)Knockout  (2)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (232)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (24)Performance  (7)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (6)Road Map  (9)Scheduler  (28)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (19)Solution Services  (2)Spreadsheet  (10)SQL  (7)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (11)Tips and Tricks  (65)UI  (139)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (1)Visual Studio  (19)Visual Studio Code  (13)Web  (225)What's new  (145)Windows 8  (19)Windows App  (1)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
4 Easy Steps to Embed a JavaScript Widget into Blazor App

4 Easy Steps to Embed a JavaScript Control into a Blazor App

Blazor is an open-source, single-page web application development framework developed by Microsoft. Even though it depends on NuGet packages, it also supports the use of npm packages.

npm contains a broader range of libraries, frameworks, and web components. Furthermore, the web components are downloaded as independent modules and styles. npm allows us to construct the required control with only the necessary modules and CSS, resulting in less memory utilization.

In this blog, we will see how to import npm packages as well as declare and embed a JavaScript control into a Blazor application in detail. To demonstrate, we will download the Syncfusion JavaScript Spreadsheet npm package and add it to the Razor page.

Step 1: Initializing npm and other necessary packages.

To begin, launch Visual Studio and create a Blazor WebAssembly application. The npm option is not available by default in this new application. So, create a new folder named npm_packages to initialize the npm and other options to hold the downloaded packages and the control definition.

Creating NPM packages folderTo initialize npm and download the prerequisite packages, run the command prompt and navigate to the npm_packages folder directory. Then, execute the following commands in the specified order:

  1. Install npm. This will automatically create the package.json file inside the npm_packages folder.
  2. Install the webpack utility. This will assist in module bundler and JavaScript conversion.
  3. Install the latest NPM package of the Syncfusion JavaScript Spreadsheet control.
    npm init -y
    npm install webpack webpack-cli –save-dev
    npm install @syncfusion/ej2-spreadsheet

Initializing NPM and other necessary packages

Step 2: Initializing and defining the Syncfusion JavaScript Spreadsheet control.

Once npm is configured, you can see the package.json file inside the npm packages folder. Now, create a folder named src within it create the index.js file. Then, import the Syncfusion JavaScript Spreadsheet modules and define the widget in the index.js file.

Create a folder named src within which create the index.js fileTo define the Syncfusion JavaScript Spreadsheet control, first, add a div container with the ID element to the Index.razor page.

Refer to the following code.

[Index.razor]

@page "/"

<h2>Syncfusion JavaScript Spreadsheet control</h2>

<div id="element"></div>

Then, define the control in the index.js file with the necessary module(s) and add it to the div container using its ID.

[index.js]

import { Spreadsheet } from '@syncfusion/ej2-spreadsheet';

// Define the Syncfusion JavaScript Spreadsheet component.
var numberFormat = '$#,##0.00';
var 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', format: '$#,##0.00' },
                        { value: '8120', format: '$#,##0.00' },
                        { formula: '=B2-C2', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Loan' },
                        { value: '1500', format: '$#,##0.00' },
                        { value: '1500', format: '$#,##0.00' },
                        { formula: '=B3-C3', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Medical' },
                        { value: '300', format: '$#,##0.00' },
                        { value: '0', format: '$#,##0.00' },
                        { formula: '=B4-C4', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Clothing' },
                        { value: '400', format: '$#,##0.00' },
                        { value: '140', format: '$#,##0.00' },
                        { formula: '=B5-C5', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Education' },
                        { value: '900', format: '$#,##0.00' },
                        { value: '750', format: '$#,##0.00' },
                        { formula: '=B6-C6', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Insurance' },
                        { value: '30', format: '$#,##0.00' },
                        { value: '30', format: '$#,##0.00' },
                        { formula: '=B7-C7', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Utilities' },
                        { value: '130', format: '$#,##0.00' },
                        { value: '160', format: '$#,##0.00' },
                        { formula: '=B8-C8', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Enterainment' },
                        { value: '500', format: '$#,##0.00' },
                        { value: '730', format: '$#,##0.00' },
                        { formula: '=B9-C9', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Maintainance' },
                        { value: '50', format: '$#,##0.00' },
                        { value: '70', format: '$#,##0.00' },
                        { formula: '=B10-C10', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Transportation' },
                        { value: '250', format: '$#,##0.00' },
                        { value: '400', format: '$#,##0.00' },
                        { formula: '=B11-C11', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { value: 'Gifts/Donations' },
                        { value: '0', format: '$#,##0.00' },
                        { value: '100', format: '$#,##0.00' },
                        { formula: '=B12-C12', format: numberFormat }
                    ]
                },
                {
                    cells: [
                        { index: 2, value: 'Total Difference:', style: { fontWeight: 'bold', textAlign: 'right' } },
                        { formula: '=D2+D12', format: numberFormat, style: { fontWeight: 'bold' } }
                    ]
                }
            ],
            columns: [
                { width: 110 }, { width: 115 }, { width: 110 }, { width: 100 }
            ]
        }
    ]
});

// Render the defined spreadsheet.
spreadsheet.appendTo('#element');

Step 3: Adding build script to the application.

Now, let’s include the following build script in the package.json file. It uses webpack to bundle the JavaScript file. This script tells the webpack to use the index.js file in the src folder as the source file.

The output directory of the bundled file is then set to be created under the js folder in the wwwroot folder directory as index.bundle.js.

Refer to the following code.

[package.json]

"scripts": {
  "build": "webpack ./src/index.js --output-path ../wwwroot/js --output-filename index.bundle.js"
  }

To bundle the module(s) and control definition periodically, we have to add a prebuild step to the *.csproj file. Bundling occurs when you build or rebuild the application.

[*.csproj]

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

	………  

	<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
		<Exec Command="npm install" WorkingDirectory="npm_packages"   />
		<Exec Command="npm run build" WorkingDirectory="npm_packages " />
	</Target>

</Project>

Step 4: Adding necessary JS and CSS for the control.

Finally, when the application is built, the index.bundle.js file will be generated. We have to add it to the application via the index.html file in the wwwroot folder. Copy and paste the appropriate CSS stylesheet from the node_modules\@syncfusion\~ to the wwwroot\css folder path, then add it to the application via the index.html file.

Note: Since it is a JavaScript control, we have to initialize the div container within the Index.razor file before the scripts containing the module(s) and definition. To achieve this, make the following changes to the index.html code, which will load the scripts after the Blazor starts.

[index.html]

<head>
    
    ......
    ......

    <link href="css/ej2-base/styles/material.css" rel="stylesheet" />
    <link href="css/ej2-inputs/styles/material.css" rel="stylesheet" />
    <link href="css/ej2-buttons/styles/material.css" rel="stylesheet" />
    <link href="css/ej2-splitbuttons/styles/material.css" rel="stylesheet" />
    <link href="css/ej2-lists/styles/material.css" rel="stylesheet" />
    <link href="css/ej2-navigations/styles/material.css" rel="stylesheet" />
    <link href="css/ej2-popups/styles/material.css" rel="stylesheet" />
    <link href="css/ej2-dropdowns/styles/material.css" rel="stylesheet" />
    <link href="css/ej2-grids/styles/material.css" rel="stylesheet" />
    <link href="css/ej2-spreadsheet/styles/material.css" rel="stylesheet" />

    <script src="js/index.bundle.js"></script>

</head>

<body>
    
    ......
    ......
    <script src="_framework/blazor.webassembly.js" autostart="false"></script>
    <script>
        Blazor.start().then(function () {
            var customScript = document.createElement('script');
            customScript.setAttribute('src', 'js/index.bundle.js');
            document.head.appendChild(customScript);
        });
    </script>
</body>

Now, run the application. Then, you can see the JavaScript Spreadsheet control in your Blazor WebAssembly application.

Refer to the following screenshot.

Embedding a JavaScript Spreadsheet into Blazor app
Embedding a JavaScript Spreadsheet into Blazor app

References

For more information, refer to the following reference links:

Conclusion

Thanks for reading! In this blog post, we have seen how to embed a JavaScript control into a Blazor application using npm packages. As the npm packages allow us to construct the required control with only the necessary modules and CSS, we can considerably reduce memory utilization. Try out the steps given in this blog post and leave your feedback in the comments section given below!

The Syncfusion Blazor suite offers over 70 components that work with both Blazor server- side and client-side (Blazor WebAssembly) projects seamlessly. Use them to build astonishing applications!

For existing customers, the newest version of Essential Studio is available for download from the License and Downloads page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out the available features.

For questions, you can contact us through our support forumDirect-Trac, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin
Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top