creating a tab with X button

Hi,

How can I create a tab with X button in it next to title to close the current tab(to remove the tab dynamically )

Thanks,

RE: creating a tab with X button

Hi,
We don’t currently have tabButtons with a ‘close’ icon built into the tab-set as a feature (though it is a feature we may consider adding in a future release).

For now the quickest way to do this would be to write an image directly out into the title for the tab.
You can do this by overriding getTitleHTML() on the tab in question, and using the ‘imgHTML()’ helper method to write out an image tag.
Because we want the close button to show up on all tabs, it makes sense to create a subclass of the TabSet class where this always occurs.

Here’s some code that will achieve this:

// Create a subclass of ImgTab with a close button
isc.ClassFactory.defineClass("CloseableTab", "ImgTab");

isc.CloseableTab.addMethods({
    
    // Override titleHTML to write out the close button
    getTitleHTML : function () {
        return this.title + " " + 
               isc.Canvas.imgHTML(
                   // Use the Window close button image for now
                   "[SKIN]/Window/close.png", 
                   16, 16, null, 
                   // Use the 'extraStuff' parameter to write a click handler directly
                   // into the IMG tag
                   "onclick='return " + this.getID() + ".closeTab()'"
               );
    },
    
    // closeTab - fired when the user clicks the 'close' image
    closeTab : function () {
        var tabSet = this.parentElement.parentElement;
        tabSet.removeTab(tabSet.getTabNumber(this));
        return false;
    }
})

// Create a subclass of the TabSet class that shows a close button on each tab
isc.ClassFactory.defineClass("CloseableTabSet", "TabSet");

isc.CloseableTabSet.changeDefaults("tabBarDefaults", {buttonConstructor:isc.CloseableTab});

// At this point you can define a 'CloseableTabSet' instance, and all the tabs will show up
// with the close button

A few notes on this code:

  • By default TabSets’ tabs are ImgTab instances. We’re creating a subclass of ImgTab here that will always show the close button.

  • TabSets write out a TabBar instance, which contains the actual tab buttons.
    The class of the tabs in a TabBar is determined by the ‘buttonConstructor’ attribute of the TabBar. In this code we’re using the property ‘tabBarDefaults’ on the TabSet to directly modify properties on its tabBar - in this case setting the buttonConstructor so we show CloseableTab instances rather than the default ImgTabs. We’re applying this change directly to the newly created CloseableTabSet class so every CloseableTabSet instance will write out CloseableTabs.
    You may want to take a look at the documentation on ClassFactory.defineClass() and Class.changeDefaults.

  • We’re using the Window’s close button image by default - you’ll probably want to create some custom media for this rather than re-using this image.

  • The imgHTML() method accepts a freeform ‘extra-stuff’ parameter. This accepts a string which will be written out directly into the IMG tag returned by the method. We use this to write an onclick handler for the image.
    Check the reference doc in the SDK for the full signature of this method.

  • The closeTab() method below will be run on the tab directly. Since the tab is written inside a TabBar, which is inside the TabSet, we have to call this.parentElement.parentElement to get a pointer to the TabSet (or CloseableTabSet).

I hope this works for you.
Regards
Isomorphic Software

Note that as of SmartClient 5.7, this feature is built into the library.

See:

http://www.smartclient.com/docs/5.7/a/b/c/go.html#attr..TabSet.canCloseTabs

To put the ‘close’ icon on the right side of the tab (where it belongs!), do:

isc.ImgTab.addProperties(
{
   iconOrientation: "right"
} );