- On Demand rendering or lazy loading
- Dynamic rendering
- On initial rendering
Contact Support
Content render modes in EJ2 JavaScript Tab control
26 Dec 202423 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.
ej.base.enableRipple(true);
var tabObj = new ej.navigations.Tab({
items: [
{
header: { text: 'Calendar' },
content: '#Calendar'
},
{
header: { text: 'Schedule' },
content: '#Schedule',
},
]
});
tabObj.appendTo('#element');
var calendar = new ej.calendars.Calendar();
calendar.appendTo('#Calendar');
var schedule = new ej.schedule.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/29.2.4/material.css" rel="stylesheet">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></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="container">
<div id="Calendar" style="display:none">
</div>
<div id="Schedule" style="display:none">
</div>
<div id="element"></div>
</div>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</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.
ej.base.enableRipple(true);
var tabObj = new ej.navigations.Tab({
items: [
{
header: { 'text': 'Login' },
content: '.login-form',
disabled: false
},
{
header: { 'text': 'Grid' },
content: '.grid-view',
disabled: true
}
],
loadOn: 'Dynamic'
});
tabObj.appendTo('#element');
var userNameObj = new ej.inputs.TextBox({
placeholder: 'User Name',
floatLabelType: 'Auto'
});
userNameObj.appendTo('#username');
let passWordObj = new ej.inputs.TextBox({
placeholder: 'Password',
floatLabelType: 'Auto'
});
passWordObj.appendTo('#password');
let buttonObj = new ej.buttons.Button({
content: 'Log in',
isPrimary: true,
});
buttonObj.appendTo('#normalbtn');
let grid = new ej.grids.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')
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 {
debugger;
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/29.2.4/material.css" rel="stylesheet">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></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="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>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</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.
ej.base.enableRipple(true);
var tabObj = new ej.navigations.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');
var userNameObj = new ej.inputs.TextBox({
placeholder: 'Enter Name',
floatLabelType: 'Auto',
});
userNameObj.appendTo('#username');
var passWordObj = new ej.inputs.TextBox({
placeholder: 'Enter Password',
floatLabelType: 'Auto',
});
passWordObj.appendTo('#password');
let signButton = new ej.buttons.Button({
content: 'Sign in',
isPrimary: true,
});
signButton.appendTo('#sign-btn');
var skipButton = new ej.buttons.Button({
content: 'Skip',
isPrimary: true,
});
skipButton.appendTo('#skip-btn');
var signInName = new ej.inputs.TextBox({
placeholder: 'Entenr Name',
floatLabelType: 'Auto'
});
signInName.appendTo('#sign-in-name');
var mail = new ej.inputs.TextBox({
placeholder: 'Enter Mail',
floatLabelType: 'Auto',
});
mail.appendTo('#mail');
var comments = new ej.inputs.TextBox({
placeholder: 'Share your comments',
floatLabelType: 'Auto',
});
comments.appendTo('#comments');
var submit = new ej.buttons.Button({
content: 'submit',
isPrimary: true,
});
submit.appendTo('#submit');
signButton.element.onclick = () => {
let userName = userNameObj.value;
let password = passWordObj.value;
if (signInName) {
signInName.value = userName
}
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');
}
tabObj.select(1);
};
skipButton.element.onclick = () => {
tabObj.select(1);
};
submit.element.onclick = () => {
userNameObj.value = passWordObj.value = signInName.value = '';
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/29.2.4/material.css" rel="stylesheet">
<link href="index.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/29.2.4/dist/ej2.min.js" type="text/javascript"></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="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>
<script>
var ele = document.getElementById('container');
if (ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body>
</html>