Search code examples
jqueryjquery-ui-tabs

How to Call Jquery tabdrop() Plugin from Jquery Tab Widget?


Just as this Jquery Plugin, I have jquery tab that I would like it to behave the way that plugin does by grouping any exceeded tab into dropdown as it reaches the screen width.

example of jquery tabdrop

In my fiddle sample code , I tried call this plugin but still my tab does not move into drop down group.

$(function() {
  var tabTitle = $("#tab_title"),
    tabContent = $("#tab_content"),
    tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
    tabCounter = 2;

  var tabs = $("#tabs").tabs();
  // Modal dialog init: custom buttons and a "close" callback resetting the form inside
  var dialog = $("#dialog").dialog({
    autoOpen: false,
    modal: true,
    buttons: {
      Add: function() {
        addTab();
        $(this).dialog("close");
      },
      Cancel: function() {
        $(this).dialog("close");
      }
    },
    close: function() {
      form[0].reset();
    }
  });

  // AddTab form: calls addTab function on submit and closes the dialog
  var form = dialog.find("form").on("submit", function(event) {
    addTab();
    dialog.dialog("close");
    event.preventDefault();
  });

  // Actual addTab function: adds new tab using the input from the form above
  function addTab() {
    var label = tabTitle.val() || "Tab " + tabCounter,
      id = "tabs-" + tabCounter,
      li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{label\}/g, label)),
      tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content.";

    tabs.find(".ui-tabs-nav").append(li);
    tabs.append("<div id='" + id + "'><p>" + tabContentHtml + "</p></div>");
    tabs.tabs("refresh");
    tabCounter++;
  }

  // AddTab button: just opens the dialog
  $("#add_tab")
    .button()
    .on("click", function() {
      dialog.dialog("open");
    });

  // Close icon: removing the tab on click
  tabs.on("click", "span.ui-icon-close", function() {
    var panelId = $(this).closest("li").remove().attr("aria-controls");
    $("#" + panelId).remove();
    tabs.tabs("refresh");
  });


  $('#tabs').tabdrop(); // Here Where I Called Plugin TabDrop
});

I could not use this plugin directly because my current project is using Jquery tab instead and I don't want my tab fall down into new line as user open many tabs.

Therefore, how can I achieve that result for current project? Thanks


Solution

  • Ok i have created my own tabdrop that will work on mobile and also check for screen size.

    Have a look here and let me know what you think

    Make sure to look at function tabdrop, the css and design of the dropdown you can modify as you like as i did not really had much time with.

    The dropdown will appear when there is to many tabs that exeed the window size.