Getting started in EJ2 TypeScript Accordion control
7 Aug 202413 minutes to read
This section briefly explains about how to create a simple Accordion using TypeScript and
configure the Accordion items using Essential JS 2 quickstart seed repository.
This application is integrated with the
webpack.config.js
configuration and uses the latest version of the webpack-cli. It requires nodev14.15.0
or higher. For more information about webpack and its features, refer to the webpack documentation.
Dependencies
The following list of dependencies are required to use the Accordion component in your application.
|-- @syncfusion/ej2-navigations
|-- @syncfusion/ej2-base
Set up development environment
Open the command prompt from the required directory, and run the following command to clone the Syncfusion JavaScript (Essential JS 2) quickstart project from GitHub.
git clone https://github.com/SyncfusionExamples/ej2-quickstart-webpack- ej2-quickstart
After cloning the application in the ej2-quickstart
folder, run the following command line to navigate to the ej2-quickstart
folder.
cd ej2-quickstart
Add Syncfusion JavaScript packages
Syncfusion JavaScript (Essential JS 2) packages are available on the npmjs.com public registry. You can install all Syncfusion JavaScript (Essential JS 2) controls in a single @syncfusion/ej2 package or individual packages for each control.
The quickstart application is preconfigured with the dependent @syncfusion/ej2 package in the ~/package.json
file. Use the following command to install the dependent npm packages from the command prompt.
npm install
Import the Syncfusion CSS styles
The Accordion CSS files are available in the ej2-navigations
package folder. This can be referenced in the ~/src/styles/styles.css
file your application using the following code.
@import '../../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-navigations/styles/material.css';
Initialize the Accordion using Items
Open the application in Visual Studio Code and add the Syncfusion JavaScript UI controls.
The Accordion can be rendered by defining an array of items
.
- Add the HTML div tag with its
id
attribute aselement
in yourindex.html
file to initialize the Accordion.
[src/index.html]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Accordion</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
<meta name="description" content="Essential JS 2" />
<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" />
</head>
<body>
<div style="margin: 50px;">
<!--element which is going to render-->
<div id="element"></div>
</div>
</body>
</html>
- Import the Accordion component to your
app.ts
file and initialize it to the#element
as shown below.
[src/app/app.ts]
import { Accordion } from '@syncfusion/ej2-navigations';
// Initialize Accordion component
let accordion: Accordion = new Accordion({
items: [
{ header: 'ASP.NET', content: 'Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services.' },
{ header: 'ASP.NET MVC', content: 'The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.' },
{ header: 'JavaScript', content: 'JavaScript (JS) is an interpreted computer programming language. It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.' },
]
});
// Render initialized Accordion
accordion.appendTo('#element');
- Run the application in the browser using the following command.
npm start
Output will be as follows:
import {Accordion} from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
let accordion: Accordion = new Accordion({
items: [
{ header: 'ASP.NET', expanded: 'true', content: 'Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications and XML Web services.' },
{ header: 'ASP.NET MVC', content: 'The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.' },
{ header: 'JavaScript', content: 'JavaScript (JS) is an interpreted computer programming language. It was originally implemented as part of web browsers so that client-side scripts could interact with the user, control the browser, communicate asynchronously, and alter the document content that was displayed.' },
]
});
accordion.appendTo('#element');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Accordion</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript Toolbar Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/27.2.2/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<div id='element'></div>
<br/><br/>
<div id='result'></div>
</div>
</body>
</html>
In the above sample code,
#element
is theid
of the HTML element in a page to which the Accordion is initialized.
Initialize the Accordion using HTML elements
The Accordion component can be rendered based on the given HTML element using id
as target
property.
You need to follow the below structure of HTML elements to render the Accordion.
<div id='accordion_html_markup'> --> Root Accordion Element
<div> --> Accordion Item Container
<div> --> Accordion Header Container
<div> </div> --> Accordion Header
</div>
<div> --> Accordion Panel Container
<div> </div> --> Accordion Content
</div>
</div>
</div>
import {Accordion} from '@syncfusion/ej2-navigations';
import { enableRipple } from '@syncfusion/ej2-base';
enableRipple(true);
let accordion: Accordion = new Accordion({});
accordion.appendTo('#accordion_html_markup');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Accordion</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Typescript Toolbar Controls" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/27.2.2/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>LOADING....</div>
<div id='container'>
<div id='accordion_html_markup'>
<div>
<div>
<div> ASP.NET </div>
</div>
<div>
<div> Microsoft ASP.NET is a set of technologies in the Microsoft .NET Framework for building Web applications
and XML Web services </div>
</div>
</div>
<div>
<div>
<div> ASP.NET MVC </div>
</div>
<div>
<div> The Model-View-Controller (MVC) architectural pattern separates an application into three main components:
the model, the view, and the controller </div>
</div>
</div>
<div>
<div>
<div> JavaScript </div>
</div>
<div>
<div> JavaScript (JS) is an interpreted computer programming language.It was originally implemented as part
of web browsers so that client-side scripts could interact with the user, control the browser </div>
</div>
</div>
</div>
<br/><br/>
<div id='result'></div>
</div>
</body>
</html>
You can add the custom class into Accordion component using
cssClass
property which is used to customize the Accordion component.
See Also
NOTE
You can refer to our JavaScript Accordion feature tour page for its groundbreaking feature representations. You can also explore our JavaScript Accordion example that shows you how to render the Accordion in JavaScript.