Search results

Load Tab items dynamically

Tabs can be added dynamically by passing array of items and index value to the addTab method.

    // New tab title and content inputs are fetched and stored in local variable
    let title: string = document.getElementById('tab-title').value;
    let content: string = document.getElementById('tab-content').value;

    // Required tab item object formed by using textbox inputs
    let item: Object =  { header: { text: title }, content: createElement('pre', { innerHTML: content.replace(/\n/g, '<br>\n') }).outerHTML };

    // Item object and the index argument passed into the addTab method to add a new tab
    tabObj.addTab([item], index);

In the following demo, you can add the tab content by clicking the +. This + icon is added on the tab header using iconCss property. Enter the new Tab heading and content details in the available text boxes, click ‘Add Tab’ button to pass the details as an array and here last index is calculated to append the new tab at the end.

Source
Preview
index.js
index.html
ej.base.enableRipple(true);

var totalItems = 0;

//Initialize Tab component
var tabObj = new ej.navigations.Tab({
    selected: tabSelected,
    items: [
        {
            header: { 'text': 'Twitter' },
            content: '#tab1_content'
        },
        {
            header: { 'iconCss': 'e-add-icon' },
            content: '#form-container'
        }
    ]
  });
//Render initialized Tab component
tabObj.appendTo('#element');

var addBtn = document.querySelectorAll(".e-ileft.e-icon");
addBtn[0].setAttribute("title", "Add Tab");

function tabSelected(args) {
    if (args.selectedIndex === document.querySelectorAll('#element .e-toolbar-item').length -1) {
        document.getElementById('tab-title').value = '';
        document.getElementById('tab-content').value = '';
    }
}

document.getElementById('btn-add').onclick = function(e) {
    var title = document.getElementById('tab-title').value;
    var content = document.getElementById('tab-content').value;
    // Required tab item object formed by using textbox inputs
    var item = { header: { text: title }, content: ej.base.createElement('pre', { innerHTML: content.replace(/\n/g, '<br>\n') }).outerHTML };

    totalItems = document.querySelectorAll('#element .e-toolbar-item').length;
    // Item object and the index argument passed into the addTab method to add a new tab
    tabObj.addTab([item], totalItems-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="//cdn.syncfusion.com/ej2/material.css" rel="stylesheet">
    <link href="index.css" rel="stylesheet">
    
    
<script src="https://cdn.syncfusion.com/ej2/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div id="tab1_content" style="display: none">
            <ul>
                <li>Click on the &quot;+&quot; header to add dynamic tab items. </li>
                <li>It displays input elements to get the new tab information. </li>
                <li>Add details and click the &quot;Add Tab&quot; button to open the newly added tab.</li>
            </ul>
        </div>
        <div id="form-container" style="display: none">
            <div class="e-float-input">
                <input type="text" id="tab-title" required>
                <span class="e-float-line"></span>
                <label class="e-float-text">Enter header title</label>
            </div>
            <br>
            <div class="e-float-input">
                <textarea rows="5" type="text" id="tab-content" required></textarea>
                <span class="e-float-line"></span>
                <label class="e-float-text">Enter content</label>
            </div>
            <br>
            <div class="btn-section">
                <button id="btn-add" class="e-btn">Add Tab</button>
                <br>
                <br>
                <span class="info"> * Title is mandatory to add a new Tab</span>
            </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>