- On Demand rendering or lazy loading
- Dynamic rendering
- On initial rendering
Contact Support
Content render modes in EJ2 TypeScript Tab control
26 Dec 202422 minutes to read
Tabs support rendering content based on different scenarios. The content of the tabs can be rendered in three different ways, as outlined below.
On Demand rendering or lazy loading
This mode is the default, where only the content of the currently selected tab is initially loaded and available in the DOM, with subsequent tab content rendered upon selection. Once a tab’s content is loaded, it remains in the DOM. This ensures that the state of the tabs, such as scroller positions, form values, etc., is preserved.
In the following code example, the Calendar and Scheduler are rendered in the first and second tabs, respectively. Initially, the Scheduler is not available, but it will be rendered once the second tab is selected. Both the Calendar and Scheduler are maintained in the DOM.
import { Tab } from '@syncfusion/ej2-navigations';
import { Calendar } from '@syncfusion/ej2-calendars';
import { Schedule, Day } from '@syncfusion/ej2-schedule';
Schedule.Inject(Day);
//Initialize Tab component
let tabObj: Tab = new Tab({
items: [
{
header: { text: 'Calendar' },
content: '#Calendar'
},
{
header: { text: 'Schedule' },
content: '#Schedule',
},
]
});
tabObj.appendTo('#element');
let calendar: Calendar = new Calendar();
calendar.appendTo('#Calendar');
let schedule: Schedule = new Schedule({
width: '100%',
height: '450px'
});
schedule.appendTo('#Schedule');
<!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="https://cdn.syncfusion.com/ej2/28.2.3/material.css" rel="stylesheet" />
<link href="index.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>
<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="Calendar" style="display:none">
</div>
<div id="Schedule" style="display:none">
</div>
<div id='element'></div>
</div>
</body>
</html>
Dynamic rendering
This mode can be applied to Tabs by setting the loadOn
property to Dynamic
. In this mode, only the content of the currently selected tab is loaded and available in the DOM initially. When a different tab is selected, its content replaces the current content. Since this mode ensures the DOM maintains only the content of the active tab, page loading performance is improved. However, the Tabs do not retain their state, as each time a tab is selected, it loads its content again.
In the following code example, there are two tabs. The first tab contains a login page, and the second tab contains a Grid component. The Grid component in the second tab will only be rendered in the DOM after the login is completed. Upon successful login, the second tab will replace the first tab in the DOM.
import { enableRipple } from '@syncfusion/ej2-base';
import { Tab } from '@syncfusion/ej2-navigations';
import { TextBox } from '@syncfusion/ej2-inputs';
import { Button } from '@syncfusion/ej2-buttons';
import { Grid } from '@syncfusion/ej2-grids';
enableRipple(true);
//Initialize Tab component
let tabObj: Tab = new Tab({
items: [
{
header: { 'text': 'Login' },
content: '.login-form',
disabled: false
},
{
header: { 'text': 'Grid' },
content: '.grid-view',
disabled: true
}
],
loadOn: 'Dynamic'
});
tabObj.appendTo('#element');
let grid: Grid = new Grid({
dataSource: [
{ OrderID: 10248, CustomerID: 'ALFKI', OrderDate: '2024-12-01', Freight: 32.38 },
{ OrderID: 10249, CustomerID: 'ANATR', OrderDate: '2024-12-02', Freight: 11.61 },
{ OrderID: 10250, CustomerID: 'ANTON', OrderDate: '2024-12-03', Freight: 65.83 },
{ OrderID: 10251, CustomerID: 'AROUT', OrderDate: '2024-12-04', Freight: 41.34 }
],
allowPaging: true,
columns: [
{ field: 'OrderID', headerText: 'Order ID', width: 120, textAlign: 'Right' },
{ field: 'CustomerID', headerText: 'Customer Name', width: 130 },
{ field: 'OrderDate', headerText: 'Order Date', width: 120, format: 'yMd', textAlign: 'Right' },
{ field: 'Freight', headerText: 'Freight', width: 120, format: 'C2', textAlign: 'Right' }
]
});
grid.appendTo('#grid')
let userNameObj: TextBox = new TextBox({
placeholder: 'User Name',
floatLabelType: 'Auto'
});
userNameObj.appendTo('#username');
let passWordObj: TextBox = new TextBox({
placeholder: 'Password',
floatLabelType: 'Auto'
});
passWordObj.appendTo('#password');
let buttonObj: Button = new Button({
content: 'Log in',
isPrimary: true,
});
buttonObj.appendTo('#normalbtn');
buttonObj.element.onclick = () => {
let userName = userNameObj.value;
let password = passWordObj.value;
if (!userName && !password) {
window.alert('Enter both username and password');
} else if (!userName) {
window.alert('Enter the username');
} else if (!password) {
window.alert('Enter the password');
} else if (userName.length < 4) {
window.alert('Username must be at least 4 characters long');
} else {
userNameObj.value = "";
passWordObj.value = "";
tabObj.items[0].disabled = true;
tabObj.items[1].disabled = false;
tabObj.dataBind();
tabObj.select(1);
}
}
<!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="https://cdn.syncfusion.com/ej2/28.2.3/material.css" rel="stylesheet" />
<link href="index.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>
<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 class="login-form" style="display:none">
<div class='wrap'>
<div id="heading">Sign in to view the Grid</div>
<br>
<div id="input-container">
<input id="username" />
<br><br>
<input id="password" />
</div>
</div>
<br>
<div class="button-contain" style="display: flex; justify-content: space-around;">
<button id="normalbtn"></button>
</div>
</div>
<div class="grid-view" style="display:none">
<div id=grid></div>
</div>
<div id='element'></div>
</div>
</body>
</html>
On initial rendering
This mode can be applied to Tabs by setting the loadOn
property to Init
. In this mode, the content of all the tabs is rendered on initial load and maintained in the DOM. This mode is ideal when you have a small number of tabs and need to preserve the state of each tab. It also allows you to access the references of components rendered in other tabs.
In the following example, all three tabs are rendered on the initial load, and the data entered in the first tab will be maintained even when the second or third tab is active.
import { enableRipple } from '@syncfusion/ej2-base';
import { Tab } from '@syncfusion/ej2-navigations';
import { TextBox } from '@syncfusion/ej2-inputs';
import { Button } from '@syncfusion/ej2-buttons';
enableRipple(true);
//Initialize Tab component
let tabObj: Tab = new Tab({
items: [
{
header: { text: 'SignIn' },
content: '.login-form',
},
{
header: { text: 'Syncfusion EJ2' },
content: '.over-view',
},
{
header: { text: 'FeedBack' },
content: '.feed-back',
},
],
loadOn: 'Init',
});
tabObj.appendTo('#element');
let userNameObj: TextBox = new TextBox({
placeholder: 'Enter Name',
floatLabelType: 'Auto',
});
userNameObj.appendTo('#username');
let passWordObj: TextBox = new TextBox({
placeholder: 'Enter Password',
floatLabelType: 'Auto',
});
passWordObj.appendTo('#password');
let signButton: Button = new Button({
content: 'Sign in',
isPrimary: true,
});
signButton.appendTo('#sign-btn');
let skipButton: Button = new Button({
content: 'Skip',
isPrimary: true,
});
skipButton.appendTo('#skip-btn');
let signInName: TextBox = new TextBox({
placeholder: 'Entenr Name',
floatLabelType: 'Auto'
});
signInName.appendTo('#sign-in-name');
let mail: TextBox = new TextBox({
placeholder: 'Enter Mail',
floatLabelType: 'Auto',
});
mail.appendTo('#mail');
let comments: TextBox = new TextBox({
placeholder: 'Share your comments',
floatLabelType: 'Auto',
});
comments.appendTo('#comments');
let submit: Button = new Button({
content: 'submit',
isPrimary: true,
});
submit.appendTo('#submit');
signButton.element.onclick = () => {
let userName = userNameObj.value;
let password = passWordObj.value;
if (!userName && !password) {
window.alert('Enter both username and password');
} else if (!userName) {
window.alert('Enter the username');
} else if (!password) {
window.alert('Enter the password');
} else if (userName.length < 4) {
window.alert('Username must be at least 4 characters long');
}
};
skipButton.element.onclick = () => {
tabObj.select(1);
};
submit.element.onclick = () => {
tabObj.select(0);
};
<!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="https://cdn.syncfusion.com/ej2/28.2.3/material.css" rel="stylesheet" />
<link href="index.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>
<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 class="login-form" style="display:none">
<div class='wrap'>
<div id="input-container">
<input id="username" />
<br><br>
<input id="password" />
</div>
</div>
<br>
<div class="button-contain" style="display: flex; justify-content: center; align-items: center; gap:10px;">
<button id="sign-btn"></button>
<button id="skip-btn"></button>
</div>
</div>
<div class="over-view" style="display: none;">
<p>
You can check out our Syncfusion Ej2 demo
<a href="https://ej2.syncfusion.com/demos/" target="_blank">here</a>.
</p>
<br />
<p>
The user guide is available
<a href="https://ej2.syncfusion.com/documentation/introduction" target="_blank">here</a>.
</p>
</div>
<div class="feed-back" style="display:none">
<div class='wrap'>
<div id="input-container">
<input id="sign-in-name" />
<br><br>
<input id="mail" />
<br><br>
<input id="comments" />
</div>
</div>
<br>
<div class="button-contain" style="display: flex; justify-content: center; align-items: center; gap:10px;">
<button id="submit"></button>
</div>
</div>
<div id='element'></div>
</div>
</body>
</html>