pfeds
pfeds

Reputation: 2273

Kendo UI - How to add AND select a new Tab (on TabStrip control) using javascript

I've created a function that opens a new tab in my Kendo UI TabStrip control:

function AddTab(targetUrl, title) {
        $("#tabstrip").data("kendoTabStrip").append({ text: title, contentUrl: targetUrl });
    }

This will add the tab to the end, but will not select it. How can I select it to become the active tab!? Do I need to set an id when I create the tab, and then call the select(..) function, or can I do it in one line?

I need to automatically generate a load of links, each will take in a different title and targetUrl.

Upvotes: 7

Views: 19184

Answers (4)

Ray Henry
Ray Henry

Reputation: 905

Code to append and select a new tab:

  var tabs = $('#tabs').data('kendoTabStrip');
  var tabNum = tabs.items().length;
  var closeButton = 
    "<span unselectable='on' class='k-icon k-delete'>delete</span>";

  tabs.append( {
    encoded: false, //allow HTML
    text: team.name + ' ' + closeButton,
    contentUrl: 'teamschedule.html'
  });
  // make new tab the active tab
  tabs.select(tabNum);

  var tab = $(tabs.items()[tabNum]);
  //attach delete handler to the delete icon
  tab.on('click','.k-delete', tab, $scope.removeTab);

Here is the code to remove the tab and select the previous one (if the removed tab was selected):

$scope.removeTab = function(e) {
  var tabs = $('#tabs').data('kendoTabStrip');
  if (e.data.hasClass('k-state-active')) {
    //select previous tab if the active tab is removed
    tabs.select(e.data.prev());
  }
  tabs.remove(e.data);
}

I'm using angular, hence the use of $scope.

Upvotes: 0

Mayur Kukadiya
Mayur Kukadiya

Reputation: 994

you can add and delete dynamic tab using kendoui..i hope this code help some one

<html>
<head>
<title> testing remote data </title>
<link href="styles/kendo.common.min.css" rel="stylesheet" />
<link href="styles/kendo.default.min.css" rel="stylesheet" /> 
<script src="js/jquery.min.js"></script>
<script src="js/kendo.all.min.js"></script>
</head>

<input type='text' id='tabname' name='tabname'>
<input type="button" value="Add Tab" onclick="AddTab()" />
<div id="tabstrip">
</div>

<script>
$(document).ready(function () {
 $("#tabstrip").kendoTabStrip( {
            animation:  {
                open: {
                    effects: "fadeIn"
                }
            },
    select: function(element){selecttab(element)}           
        });

        });

function selecttab(element) {
var tabStrip1 = $('#tabstrip').kendoTabStrip().data("kendoTabStrip");
var item = tabStrip1.element.find("li:contains("+$(element.item).text()+")"),
itemIdx = item.index();   
$("#tabstrip").data("kendoTabStrip").select(itemIdx);                
}

function AddTab(targetUrl) {
 var title = jQuery("#tabname").val();
 var tabStrip = $("#tabstrip").kendoTabStrip().data("kendoTabStrip");
  tabStrip.append({ text: title,
      content: "<div class='showtabcontent dhtmlgoodies_aTab' style='height: auto; display: block;' id='tabViewsubtabname'><div style='float:right;'><input type='button' value='X' onClick='closeTab($(this).closest(\"li\"));'></div><br><label class='evtgrouplables'>Description</label><br><textarea name='dynamic_other_content[]' id='dynamic_other_content' class='textareaeditor'></textarea><input type='hidden' name='dynamic_other_title[]' value=''/></div>"
  });
 tabStrip.select((tabStrip.tabGroup.children("li").length - 1));
 }
function closeTab(vari){
 var tabStrip = $('#tabstrip').kendoTabStrip().data("kendoTabStrip");
 tabStrip.remove(tabStrip.select());
 tabStrip.select((tabStrip.tabGroup.children("li").length - 1));
}
</script>

Upvotes: 3

yogi
yogi

Reputation: 19601

Try this

<div id="tabstrip">
  <ul>
    <li class="k-state-active">First Tab</li>
    <li>Second Tab</li>
  </ul>
  <div>
    First text
  </div>
  <div>
    Second text
  </div>
</div>
<input type="button" value="Add Tab" onclick="AddTab('google', 'http://google.com')" />


<script>
function AddTab(targetUrl, title) {
  var tabStrip = $("#tabstrip").kendoTabStrip().data("kendoTabStrip");
  tabStrip.append({ text: title, contentUrl: targetUrl, content: "Your content" });
  tabStrip.select((tabStrip.tabGroup.children("li").length - 1));
}
</script>

Reference link

Upvotes: 8

Ravi Gadag
Ravi Gadag

Reputation: 15861

try like this. // from documentation Kendo Ui tabstrip

var tabStrip = $("#tabStrip").data("kendoTabStrip");
tabStrip.insertAfter(
    { text: "New Tab" },
    tabstrip.tabGroup.children("li:last")
);

for selecting it

$(document).ready(function(){
    var tabstrip = $("#tabstrip").kendoTabStrip().data("kendoTabStrip");
    tabstrip.select(yourIndexoftheTab);
});

Upvotes: 3

Related Questions