Content Render Modes

13 Dec 202417 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.Navigations
@using Syncfusion.EJ2.Schedule

@(Html.EJS().Tab("ej2Tab")
    .Items(new List<TabItem>()
    {
        new TabItem()
        {
            Header = new TabHeader { Text = "Calendar" },
            Content = ".Calendar-component",
        },
        new TabItem()
        {
            Header = new TabHeader { Text = "Schedule" },
            Content = ".Schedule-component"
        },
    })
    .ClearTemplates(true)
    .Render()
)

<div class="Calendar-component" style="display:none">
    @Html.EJS().Calendar("calendar").Render()
</div>

 <div class="Schedule-component" style="display:none">
     @Html.EJS().Schedule("schedule").Height("450px").Render()
 </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.Navigations
@using Syncfusion.EJ2.Grids

@(Html.EJS().Tab("ej2Tab")
    .Items(new List<TabItem>()
    {
        new TabItem()
        {
            Header = new TabHeader { Text = "Login" },
            Content = ".login-form",
            Disabled= false
        },
        new TabItem()
        {
            Header = new TabHeader { Text = "Grid" },
            Content = ".grid-view",
            Disabled = true
        },
    })
    .LoadOn(ContentLoad.Dynamic)
    .ClearTemplates(true)
    .Render()
)

<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">
            <div id="input-container">
                @Html.EJS().TextBox("userName").Placeholder("User Name").Render()
                <br><br>
                @Html.EJS().TextBox("Password").Placeholder("Password").Render()
            </div>
        </div>
        <br>
        <div class="button-contain" style="display: flex; justify-content: center; align-items: center; gap:10px;">
            @Html.EJS().Button("button").IsPrimary(true).Content("Log in").Render()
        </div>
    </div>

    <div class="grid-view" style="display:none">
    @(Html.EJS().Grid("Grid1").Height(250)
        .DataSource(dataManger =>
        {
            dataManger.Url("https://services.syncfusion.com/aspnet/production/api/Orders").CrossDomain(true).Adaptor("WebApiAdaptor");
        })
        .Columns(col =>
        {
            col.Field("OrderID").HeaderText("Order ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
            col.Field("CustomerID").HeaderText("Customer ID").Width("160").Add();
            col.Field("EmployeeID").HeaderText("Employee ID").Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
            col.Field("Freight").HeaderText("Freight").Width("150").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
            col.Field("ShipCountry").HeaderText("Ship Country").Width("150").Add();
        }).AllowPaging().PageSettings(page => page.PageCount(3))
        .Render()
    )
    </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>

<script type="text/javascript">
        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>

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

@(Html.EJS().Tab("ej2Tab")
    .Items(new List<TabItem>()
    {
        new TabItem()
        {
            Header = new TabHeader { Text = "Login" },
            Content = ".login-form",
        },
        new TabItem()
        {
            Header = new TabHeader { Text = "Syncfusion EJ2" },
            Content = ".over-view",
        },
        new TabItem()
        {
            Header = new TabHeader { Text = "FeedBack" },
            Content = ".feed-back",
        }
    })
    .LoadOn(ContentLoad.Init)
    .ClearTemplates(true)
    .Render()
)

<div class="login-form" style="display:none">
    <div class='wrap'>
        <br>
        <div id="input-container">
            @Html.EJS().TextBox("userName").Placeholder("User Name").Render()
            <br><br>
            @Html.EJS().TextBox("Password").Placeholder("Password").Render()
        </div>
    </div>
    <br>
    <div class="button-contain" style="display: flex; justify-content: center; align-items: center; gap:10px;">
        @Html.EJS().Button("sign-in-button").IsPrimary(true).Content("SignIn").Render()
        @Html.EJS().Button("skip-button").IsPrimary(true).Content("Skip").Render()
    </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">
            @Html.EJS().TextBox("sign-in-name").Placeholder("User Name").Render()
            <br><br>
            @Html.EJS().TextBox("e-mail").Placeholder("Password").Render()
            <br><br>
            @Html.EJS().TextBox("Comments").Placeholder("Comments").Render()
        </div>
    </div>
    <br>
    <div class="button-contain" style="display: flex; justify-content: center; align-items: center; gap:10px;">
        @Html.EJS().Button("feed-back-button").IsPrimary(true).Content("Submit").Render()
    </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;
    }
</style>

<script type="text/javascript">
    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>