Here's what you need
The HTML snippet is as follows:
<div id="tabs">
<ul>
<li><a href="#actionTab1"><span>One</span></a></li>
<li><a href="#actionTab2"><span>Two</span></a></li>
<li><a href="#actionTab3"><span>Three</span></a></li>
</ul>
</div>
The script snippet is as follows:
<script> //search tab index for value matching specific tab
var displayTab = $('#tabs a[href="#actionTab2"]').parent().index();
$("#tabs").tabs({active: displayTab});
</script>
In accommodating our server-side logic, a modification was done thus,
//variable set from server
var displayTab = $('#tabs a[href="#${svTab}"]').parent().index();
And because it may fail to find the item and return -1,
var displayTab = $('#tabs a[href="#actionTab2"]').parent().index();
if(displayTab == -1) {
displayTab = 0; //default to first tab
}
"A negative value selects panels going backward from the last panel."
The final version I used:
<script>
//search tab index for value matching specific tab
var displayTab = $('#tabs a[href="#${svTab}"]').parent().index();
if(displayTab == -1) {
displayTab = 0; //default to first tab
}
$("#tabs").tabs({active: displayTab});
</script>
In short:
- Identify the <div> of the tab heading,
- Filter for its <a> children with the matching href attribute,
- Return the (zero-based) index of the parent <li>
- Create a JSON object setting the index into the name "active"
- Call the widget with the JSON object as the argument
And that was it. Have fun!
No comments:
Post a Comment