views:

400

answers:

1

I am creating a web application and I want to use the Tabs widget to replicate the tab functionality you find in most web browsers. I want the user to be able: to move (sort) the tabs around, create tabs dynamically, close tabs dynamically.

I am having a problem deleting tabs that have been moved around.

Lets say there are three tabs named:

one, two and three.

If I move "one" so the tabs are like:

two, three and one

When I delete "one", which has an index of 2, tab "three" is deleted. So the tabs are now:

two, and one.

I have tested many different scenarios and it appears to me when I remove a tab, JQueryUI removes the wrong tab that initially had the index value, and not the tab that currently has the index value.

A: 

You're right that the tabs keep their old index values when you reorder them, leading to unexpected behavior when you try to remove one. You can force it to update the indexes by reinitializing the tabs before deleting, like so:

$('#tabs').tabs('destroy').tabs();
$('#tabs').tabs('remove', 2);

This works because the indexes are generated when the tabs are set up based on the positions of the li elements in the DOM. When you move the tabs around, the li positions are updated in the DOM, even if their index values don't change in the Tabs plugin.

Of course, if you have a very complicated setup, this might have negative performance implications, in which case you could figure out some other way to update the tab indexes, or you could maintain an array that maps the original indexes to the current indexes.

No Surprises
I was hoping there was a way to "refresh" the indexes. Thank you.
AshleyS
You're welcome!
No Surprises
This technique is actually causing me a bug now - When I create new tabs at runtime, the destroy method removes all runtime tabs as well as the intended tab.
AshleyS
yeah same here. it removes everything.
Neo