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.
The following list of dependencies are required to use the Accordion component in your application.
|-- @syncfusion/ej2-navigations
|-- @syncfusion/ej2-base
git clone https://github.com/syncfusion/ej2-quickstart.git quickstart
cd quickstart
npm install
By default, the project is configured with all the Essential JS 2 dependencies. For better understanding, remove all the dependencies from
src/system.config.js
to get started with the Accordion component.
system.config.js
configuration file.[src/system.config.js]
System.config({
paths: {
'syncfusion:': './node_modules/@syncfusion/',
},
map: {
app: 'app',
//Syncfusion packages mapping
"@syncfusion/ej2-base": "syncfusion:ej2-base/dist/ej2-base.umd.min.js",
"@syncfusion/ej2-buttons": "syncfusion:ej2-buttons/dist/ej2-buttons.umd.min.js",
"@syncfusion/ej2-data": "syncfusion:ej2-data/dist/ej2-data.umd.min.js",
"@syncfusion/ej2-inputs": "syncfusion:ej2-inputs/dist/ej2-inputs.umd.min.js",
"@syncfusion/ej2-lists": "syncfusion:ej2-lists/dist/ej2-lists.umd.min.js",
"@syncfusion/ej2-navigations": "syncfusion:ej2-navigations/dist/ej2-navigations.umd.min.js",
"@syncfusion/ej2-popups": "syncfusion:ej2-popups/dist/ej2-popups.umd.min.js",
"@syncfusion/ej2-splitbuttons": "syncfusion:ej2-splitbuttons/dist/ej2-splitbuttons.umd.min.js"
},
packages: {
'app': { main: 'app', defaultExtension: 'js' }
}
});
System.import('app');
ej2-navigations
package folder.
This can be referenced in your application using the following code.[src/styles/styles.css]
@import '../../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-navigations/styles/material.css';
The Accordion can be rendered by defining an array of items
.
element
in your index.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" />
<!--style reference from app-->
<link href="/styles/styles.css" rel="stylesheet" />
<!--system js reference and configuration-->
<script src="node_modules/systemjs/dist/system.src.js" type="text/javascript"></script>
<script src="system.config.js" type="text/javascript"></script>
</head>
<body>
<div style="margin: 50px;">
<!--element which is going to render-->
<div id="element"></div>
</div>
</body>
</html>
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');
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="//cdn.syncfusion.com/ej2/21.2.3/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>
</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.
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="//cdn.syncfusion.com/ej2/21.2.3/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>
</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.
N> 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.