Getting started in EJ2 TypeScript Tab control
15 Dec 202424 minutes to read
This section briefly explains about how to create a simple JavaScript Tab using TypeScript and configure the Tab header content 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 is the list of dependencies required to use the Tab component in your application.
|-- @syncfusion/ej2-navigations
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-buttons
|-- @syncfusion/ej2-popups
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 Tab CSS files are available in the ej2-navigations
package folder. This can be referenced in the ~/src/styles/styles.css
file of your application using the following code.
@import '../../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-buttons/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-popups/styles/material.css';
@import '../../node_modules/@syncfusion/ej2-navigations/styles/material.css';
Initialize the Tab using JSON items collection
The Tab can be rendered by defining a JSON array. The item is rendered with header
text and content
for each Tab using items
property.
- Add the HTML div tag with its id attribute as
element
in yourindex.html
file to initialize the Tab.
[src/index.html]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Tab</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;">
<div id="element"></div>
</div>
</body>
</html>
- Import the Tab component to your
app.ts
file and initialize it to the#element
as shown below.
[src/app/app.ts]
import { Tab } from '@syncfusion/ej2-navigations';
let tabObj: Tab = new Tab({
items: [
{
header: { 'text': 'Twitter' },
content: 'Twitter is an online social networking service that enables users to send and read short 140-character ' +
'messages called "tweets". Registered users can read and post tweets, but those who are unregistered can only read ' +
'them. Users access Twitter through the website interface, SMS or mobile device app Twitter Inc. is based in San ' +
'Francisco and has more than 25 offices around the world. Twitter was created in March 2006 by Jack Dorsey, ' +
'Evan Williams, Biz Stone, and Noah Glass and launched in July 2006. The service rapidly gained worldwide popularity, ' +
'with more than 100 million users posting 340 million tweets a day in 2012.The service also handled 1.6 billion ' +
'search queries per day.'
},
{
header: { 'text': 'Facebook' },
content: 'Facebook is an online social networking service headquartered in Menlo Park, California. Its website was ' +
'launched on February 4, 2004, by Mark Zuckerberg with his Harvard College roommates and fellow students Eduardo ' +
'Saverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes.The founders had initially limited the website\'\s ' +
'membership to Harvard students, but later expanded it to colleges in the Boston area, the Ivy League, and Stanford ' +
'University. It gradually added support for students at various other universities and later to high-school students.'
},
{
header: { 'text': 'WhatsApp' },
content: 'WhatsApp Messenger is a proprietary cross-platform instant messaging client for smartphones that operates ' +
'under a subscription business model. It uses the Internet to send text messages, images, video, user location and ' +
'audio media messages to other users using standard cellular mobile numbers. As of February 2016, WhatsApp had a user ' +
'base of up to one billion,[10] making it the most globally popular messaging application. WhatsApp Inc., based in ' +
'Mountain View, California, was acquired by Facebook Inc. on February 19, 2014, for approximately US$19.3 billion.'
}
]
});
tabObj.appendTo('#element');
- Run the application in the browser using the following command.
npm start
Output will be as follows:
import { Tab } from '@syncfusion/ej2-navigations';
let tabObj: Tab = new Tab({
items: [
{
header: { 'text': 'Twitter' },
content: 'Twitter is an online social networking service that enables users to send and read short 140-character ' +
'messages called "tweets". Registered users can read and post tweets, but those who are unregistered can only read ' +
'them. Users access Twitter through the website interface, SMS or mobile device app Twitter Inc. is based in San ' +
'Francisco and has more than 25 offices around the world. Twitter was created in March 2006 by Jack Dorsey, ' +
'Evan Williams, Biz Stone, and Noah Glass and launched in July 2006. The service rapidly gained worldwide popularity, ' +
'with more than 100 million users posting 340 million tweets a day in 2012.The service also handled 1.6 billion ' +
'search queries per day.'
},
{
header: { 'text': 'Facebook' },
content: 'Facebook is an online social networking service headquartered in Menlo Park, California. Its website was ' +
'launched on February 4, 2004, by Mark Zuckerberg with his Harvard College roommates and fellow students Eduardo ' +
'Saverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes.The founders had initially limited the website\'\s ' +
'membership to Harvard students, but later expanded it to colleges in the Boston area, the Ivy League, and Stanford ' +
'University. It gradually added support for students at various other universities and later to high-school students.'
},
{
header: { 'text': 'WhatsApp' },
content: 'WhatsApp Messenger is a proprietary cross-platform instant messaging client for smartphones that operates ' +
'under a subscription business model. It uses the Internet to send text messages, images, video, user location and ' +
'audio media messages to other users using standard cellular mobile numbers. As of February 2016, WhatsApp had a user ' +
'base of up to one billion,[10] making it the most globally popular messaging application. WhatsApp Inc., based in ' +
'Mountain View, California, was acquired by Facebook Inc. on February 19, 2014, for approximately US$19.3 billion.'
}
]
});
tabObj.appendTo('#element');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Tab</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 Tab" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/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>
<style>
.e-content .e-item {
font-size: 12px;
margin: 10px;
text-align: justify;
}
.container {
min-width: 350px;
max-width: 500px;
margin: 0 auto;
}
</style>
<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>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
In the above sample code,
#element
is theid
of the HTML element in a page to which the Tab is initialized.
Initialize the Tab using HTML elements
The Tab component can be rendered based on the given HTML element using id
as target
.
Header section must be enclosed with in a wrapper element using e-tab-header
class and corresponding content must be mapped with e-content
class.
You need to follow the below structure of HTML elements to render the Tab,
<div id='tab_html_markup'> --> Root Tab element
<div class="e-tab-header"> --> Tab header
<div> --> Header Item
</div>
</div>
<div class="e-content"> --> Tab content
<div> --> Content Item
</div>
</div>
</div>
- Add the HTML template data with its id attribute and add it in your
index.html
file to initialize the Tab.
[src/index.html]
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Tab</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="tab_html_markup">
<div class="e-tab-header">
<div>Twitter </div>
<div>Facebook </div>
<div>WhatsApp </div>
</div>
<div class="e-content">
<div>
Twitter is an online social networking service that enables users to send and read short 140-character messages called 'tweets'. Registered users can read and post tweets, but those who are unregistered can only read them. Users access Twitter through the website interface, SMS or mobile device app Twitter Inc. is based in San Francisco and has more than 25 offices around the world. Twitter was created in March 2006 by Jack Dorsey, Evan Williams, Biz Stone, and Noah Glass and launched in July 2006. The service rapidly gained worldwide popularity, with more than 100 million users posting 340 million tweets a day in 2012.The service also handled 1.6 billion search queries per day.
</div>
<div>
Facebook is an online social networking service headquartered in Menlo Park, California. Its website was launched on February 4, 2004, by Mark Zuckerberg with his Harvard College roommates and fellow students Eduardo Saverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes.The founders had initially limited the website's membership to Harvard students, but later expanded it to colleges in the Boston area, the Ivy League, and Stanford University. It gradually added support for students at various other universities and later to high-school students.
</div>
<div>
WhatsApp Messenger is a proprietary cross-platform instant messaging client for smartphones that operates under a subscription business model. It uses the Internet to send text messages, images, video, user location and audio media messages to other users using standard cellular mobile numbers. As of February 2016, WhatsApp had a user base of up to one billion,[10] making it the most globally popular messaging application. WhatsApp Inc., based in Mountain View, California, was acquired by Facebook Inc. on February 19, 2014, for approximately US$19.3 billion.
</div>
</div>
</div>
</div>
</body>
</html>
- Import the tab component to your
app.ts
file and initialize it to the element#tab_html_markup
as shown below.
[src/app/app.ts]
import { Tab } from '@syncfusion/ej2-navigations';
let tab: Tab;
tab = new Tab();
tab.appendTo('#tab_html_markup');
import { Tab } from '@syncfusion/ej2-navigations';
let tab: Tab;
tab = new Tab();
tab.appendTo('#tab_html_markup');
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 Tab</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 Tab" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/28.1.33/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>
<style>
.e-content .e-item {
font-size: 12px;
margin: 10px;
text-align: justify;
}
.container {
min-width: 350px;
max-width: 500px;
margin: 0 auto;
}
</style>
<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='tab_html_markup'>
<div class="e-tab-header">
<div> Twitter </div>
<div> Facebook </div>
<div> WhatsApp </div>
</div>
<div class="e-content">
<div>
Twitter is an online social networking service that enables users to send and read short 140-character messages called 'tweets'. Registered users can read and post tweets, but those who are unregistered can only read them. Users access Twitter through the website interface, SMS or mobile device app Twitter Inc. is based in San Francisco and has more than 25 offices around the world. Twitter was created in March 2006 by Jack Dorsey, Evan Williams, Biz Stone, and Noah Glass and launched in July 2006. The service rapidly gained worldwide popularity, with more than 100 million users posting 340 million tweets a day in 2012.The service also handled 1.6 billion search queries per day.
</div>
<div>
Facebook is an online social networking service headquartered in Menlo Park, California. Its website was launched on February 4, 2004, by Mark Zuckerberg with his Harvard College roommates and fellow students Eduardo Saverin, Andrew McCollum, Dustin Moskovitz and Chris Hughes.The founders had initially limited the website's membership to Harvard students, but later expanded it to colleges in the Boston area, the Ivy League, and Stanford University. It gradually added support for students at various other universities and later to high-school students.
</div>
<div>
WhatsApp Messenger is a proprietary cross-platform instant messaging client for smartphones that operates under a subscription business model. It uses the Internet to send text messages, images, video, user location and audio media messages to other users using standard cellular mobile numbers. As of February 2016, WhatsApp had a user base of up to one billion,[10] making it the most globally popular messaging application. WhatsApp Inc., based in Mountain View, California, was acquired by Facebook Inc. on February 19, 2014, for approximately US$19.3 billion.
</div>
</div>
</div>
<br/><br/>
<div id='result'></div>
</div>
</body>
</html>
#container {
visibility: hidden;
}
#loader {
color: #008cff;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
See Also
NOTE
You can also explore our JavaScript Tabs example that shows you how to configure the Tabs in JavaScript.