Content Render Modes

13 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.

@using Syncfusion.EJ2
@using Syncfusion.EJ2.Navigations
@using Syncfusion.EJ2.Calendars
@using Syncfusion.EJ2.Schedule
@{

    var headerText0 = new TabHeader { Text = "Calendar" };
    var headerText1 = new TabHeader { Text = "Schedule" };

    var content0 = "#calendar-component";
    var content1 = "#schedule-component";
}

<ejs-tab id="ej2Tab"  >
    <e-tab-tabitems>
        <e-tab-tabitem header="@headerText0" content="@content0"></e-tab-tabitem>
        <e-tab-tabitem header="@headerText1" content="@content1"></e-tab-tabitem>
    </e-tab-tabitems>
</ejs-tab>

<div id="calendar-component" style="display:none">
    <ejs-calendar></ejs-calendar>
</div>

<div id="schedule-component" style="display:none">
    <ejs-schedule>
        <e-schedule-views>
            <e-schedule-view option="Day"></e-schedule-view>
        </e-schedule-views>
    </ejs-schedule>
</div>

<style>
    #container {
        visibility: hidden;
    }

    #loader {
        color: #008cff;
        height: 40px;
        left: 45%;
        position: absolute;
        top: 45%;
        width: 30%;
    }

    .e-content .e-item {
        font-size: 12px;
        margin: 10px;
        text-align: justify;
    }

    .container {
        min-width: 350px;
        max-width: 500px;
        margin: 0 auto;
    }

    #form-container {
        margin: 0 auto;
        max-width: 300px;
    }

    .btn-section {
        text-align: center;
    }

    .add-tab-btn-section td {
        padding: 10px;
    }

    .info {
        font-weight: bold;
    }
</style>

Dynamic rendering

This mode can be applied to Tabs by setting the LoadOn property to ContentLoad.Dynamic using LoadOn. 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.

@using Syncfusion.EJ2
@using Syncfusion.EJ2.Navigations
@using Syncfusion.EJ2.Grids
@using Syncfusion.EJ2.Buttons

@{

    var headerText0 = new TabHeader { Text = "Login" };
    var headerText1 = new TabHeader { Text = "Grid" };
    var content0 = ".login-form";
    var content1 = ".grid-view";

}

<ejs-tab id="ej2Tab"  loadOn="@ContentLoad.Dynamic">
    <e-tab-tabitems>
        <e-tab-tabitem header='headerText0' content="@content0" disabled="false"></e-tab-tabitem>
        <e-tab-tabitem header="headerText1" content="@content1" disabled="true"></e-tab-tabitem>
    </e-tab-tabitems>
</ejs-tab>

    <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">
            <ejs-textbox id="userName" placeholder="User Name"></ejs-textbox>
               <br><br>
            <ejs-textbox id="Password" placeholder="Password"></ejs-textbox>
            </div>
        </div>
        <br>
        <div class="button-contain" style="display: flex; justify-content: space-around;">
        <ejs-button id="button" isPrimary="true" content="Log in"></ejs-button>
        </div>
    </div>

<div class="grid-view" style="display:none">
    <ejs-grid id="Grid" dataSource="@ViewBag.DataSource">
        <e-grid-columns>
            <e-grid-column field='OrderID' headerText='Order ID' textAlign='Right' width=120></e-grid-column>
            <e-grid-column field="CustomerID" headerText="Customer ID" width="140" textAlign='Left'></e-grid-column>
            <e-grid-column field="Freight" headerText="Freight" textAlign="Right" format='C' width="120"></e-grid-column>
            <e-grid-column field="OrderDate" headerText="Order Date" textAlign="Right" type='date' format="yMd" width="140"></e-grid-column>
        </e-grid-columns>
    </ejs-grid>
</div>

<style>
    #container {
        visibility: hidden;
    }

    #loader {
        color: #008cff;
        height: 40px;
        left: 45%;
        position: absolute;
        top: 45%;
        width: 30%;
    }

    .e-content .e-item {
        font-size: 12px;
        margin: 10px;
        text-align: justify;
    }

    .container {
        min-width: 350px;
        max-width: 500px;
        margin: 0 auto;
    }

    #form-container {
        margin: 0 auto;
        max-width: 300px;
    }

    .btn-section {
        text-align: center;
    }

    .add-tab-btn-section td {
        padding: 10px;
    }

    .info {
        font-weight: bold;
    }

    .e-add-icon::before {
        content: '\e7d5';
    }
</style>

<script>
    document.getElementById("button").addEventListener("click", handleLogin);
    function handleLogin() {
        let userName = document.getElementById("userName").value;
        let password = document.getElementById("Password").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 {
            var tabObj = document.getElementById("ej2Tab").ej2_instances[0];
            document.getElementById("userName").value = "";
            document.getElementById("Password").value = "";
            tabObj.items[0].disabled = true;
            tabObj.items[1].disabled = false;
            tabObj.dataBind();
            tabObj.select(1);
        }
    }
</script>
public ActionResult Index()
{
    List<OrdersDetails> order = new List<OrdersDetails>();
    int code = 10000;
    for (int i = 1; i < 5; i++)
    {
        order.Add(new OrdersDetails(code + 1, "ALFKI", i + 0, 2.3 * i, false, new DateTime(1991, 05, 15), "Berlin", "Simons bistro", "Denmark", new DateTime(1996, 7, 16), "Kirchgasse 6"));
        order.Add(new OrdersDetails(code + 2, "ANATR", i + 2, 3.3 * i, true, new DateTime(1990, 04, 04), "Madrid", "Queen Cozinha", "Brazil", new DateTime(1996, 9, 11), "Avda. Azteca 123"));
        order.Add(new OrdersDetails(code + 3, "ANTON", i + 1, 4.3 * i, true, new DateTime(1957, 11, 30), "Colchester", "Frankenversand", "Germany", new DateTime(1996, 10, 7), "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"));
        order.Add(new OrdersDetails(code + 4, "BLONP", i + 3, 5.3 * i, false, new DateTime(1930, 10, 22), "Marseille", "Ernst Handel", "Austria", new DateTime(1996, 12, 30), "Magazinweg 7"));
        order.Add(new OrdersDetails(code + 5, "BOLID", i + 4, 6.3 * i, true, new DateTime(1953, 02, 18), "Tsawwassen", "Hanari Carnes", "Switzerland", new DateTime(1997, 12, 3), "1029 - 12th Ave. S."));
    }
    ViewBag.DataSource = order;
    return View();
}

public class OrdersDetails
{
    public OrdersDetails(int OrderID, string CustomerId, int EmployeeId, double Freight, bool Verified, DateTime OrderDate, string ShipCity, string ShipName, string ShipCountry, DateTime ShippedDate, string ShipAddress)
    {
        this.OrderID = OrderID;
        this.CustomerID = CustomerId;
        this.EmployeeID = EmployeeId;
        this.Freight = Freight;
        this.ShipCity = ShipCity;
        this.Verified = Verified;
        this.OrderDate = OrderDate;
        this.ShipName = ShipName;
        this.ShipCountry = ShipCountry;
        this.ShippedDate = ShippedDate;
        this.ShipAddress = ShipAddress;
    }
    public int? OrderID { get; set; }
    public string CustomerID { get; set; }
    public int? EmployeeID { get; set; }
    public double? Freight { get; set; }
    public string ShipCity { get; set; }
    public bool Verified { get; set; }
    public DateTime OrderDate { get; set; }
    public string ShipName { get; set; }
    public string ShipCountry { get; set; }
    public DateTime ShippedDate { get; set; }
    public string ShipAddress { get; set; }
}

On initial rendering

This mode can be applied to Tabs by setting the LoadOn property to ContentLoad.Init using LoadOn. 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.

@using Syncfusion.EJ2
@using Syncfusion.EJ2.Navigations
@using Syncfusion.EJ2.Buttons

@{

    var headerText0 = new TabHeader { Text = "Login" };
    var headerText1 = new TabHeader { Text = "Syncfusion EJ2" };
    var headerText2 = new TabHeader { Text = "FeedBack" };
 
    var content0 = ".login-form";
    var content1 = ".over-view";
    var content2 = ".feed-back";

}

<ejs-tab id="ej2Tab"  loadOn="@ContentLoad.Init">
    <e-tab-tabitems>
        <e-tab-tabitem header='headerText0' content="@content0"></e-tab-tabitem>
        <e-tab-tabitem header="headerText1" content="@content1"></e-tab-tabitem>
        <e-tab-tabitem header="headerText2" content="@content2"></e-tab-tabitem>
    </e-tab-tabitems>
</ejs-tab>

    <div class="login-form" style="display:none">
        <div class='wrap'>
            <br>
            <div id="input-container">
            <ejs-textbox id="userName" placeholder="User Name"></ejs-textbox>
               <br><br>
            <ejs-textbox id="Password" placeholder="Password"></ejs-textbox>
            </div>
        </div>
        <br>
    <div class="button-contain" style="display: flex; justify-content: center; align-items: center; gap:10px;">
        <ejs-button id="sign-in-button" isPrimary="true" content="SignIn"></ejs-button>
        <ejs-button id="skip-button" isPrimary="true" content="skip"></ejs-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">
            <ejs-textbox id="sign-in-name" placeholder="User Name"></ejs-textbox>
            <br><br>
            <ejs-textbox id="e-mail" placeholder="Enter Email"></ejs-textbox>
            <br><br>
            <ejs-textbox id="comments" placeholder="Enter Comments"></ejs-textbox>
        </div>
    </div>
    <br>
    <div class="button-contain" style="display: flex; justify-content: center; align-items: center; gap:10px;">
        <ejs-button id="feed-back-button" isPrimary="true" content="Submit"></ejs-button>
    </div>
</div>

<style>
    #container {
        visibility: hidden;
    }

    #loader {
        color: #008cff;
        height: 40px;
        left: 45%;
        position: absolute;
        top: 45%;
        width: 30%;
    }

    .e-content .e-item {
        font-size: 12px;
        margin: 10px;
        text-align: justify;
    }

    .container {
        min-width: 350px;
        max-width: 500px;
        margin: 0 auto;
    }

    #form-container {
        margin: 0 auto;
        max-width: 300px;
    }

    .btn-section {
        text-align: center;
    }

    .add-tab-btn-section td {
        padding: 10px;
    }

    .info {
        font-weight: bold;
    }

    .e-add-icon::before {
        content: '\e7d5';
    }
</style>

<script>
    document.getElementById("sign-in-button").addEventListener("click", handleLogin);
    document.getElementById("skip-button").addEventListener("click", handleSkip);
    document.getElementById("feed-back-button").addEventListener("click", handlefeedBack);
    var userName;
    function handleLogin() {
        var tabObj = document.getElementById("ej2Tab").ej2_instances[0];
        userName = document.getElementById("userName").value;
        let password = document.getElementById("Password").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 {
            document.getElementById("sign-in-name").value = userName;
            tabObj.select(1);
        }
    }
    function handleSkip() {
        var tabObj = document.getElementById("ej2Tab").ej2_instances[0];
        tabObj.select(1);
    }
    function handlefeedBack() {
        var tabObj = document.getElementById("ej2Tab").ej2_instances[0];
        document.getElementById("userName").value = '';
        document.getElementById("Password").value = '';
        tabObj.select(0);
    }
</script>