I am dynamically creating a table (on click of a button) and storing the current tab index when the tab is activated (clicked on). This works the first time I create the table; however, if I recreate the tab to load different data (hide this form, go back to the previous form and click the button to display) then the following does not work:
$('#dayTabs').tabs();
$('#dayTabs').bind("tabsactivate", function (event, ui) {
alert('oldTab = ' + ui.oldTab.index());
alert('newTab = ' + ui.newTab.index());
sessionStorage.setItem('ssCurrentTab', ui.newTab.index());
});
However, the following still works:
$("#menuTabs a").click(function(e){
The full context is:
$('#updateDailyProgram').click(function() {
sessionStorage.setItem('ssFromDate', $("#updateFromDate").val());
$("#menuDetails").empty();
sessionStorage.setItem('ssCurrentDate', $("#updateFromDate").val());
sessionStorage.setItem('ssCurrentTab', 0);
$("#menuDetails").append("<ul id='menuTabs' class='nav nav-tabs'></ul>").find('ul');
var newDateString = $("#updateFromDate").val();
var firstTab = "Y";
for (var i = 0; i < sessionStorage.getItem('ssProgramDays'); i++){
if (firstTab === "Y"){
$("#menuTabs").append("<li class='nav-item'><a href='#home' class='nav-link active' name='"+newDateString+"'>"+newDateString+"</a></li>");
firstTab = "N";
}else{
$("#menuTabs").append("<li class='nav-item'><a href='#home' class='nav-link' name='"+newDateString+"'>"+newDateString+"</a></li>");
}
//Add 1 day to date
newDateString = moment(newDateString, 'DD/MM/YYYY').add(1, 'days');
newDateString = newDateString.format('DD/MM/YYYY');
};
$('#showProgramUpdateForm').hide();
$('#programEditForm').show();
//Show first tab
loadProgramDailyTable($("#updateFromDate").val());
$("#menuTabs a:eq(0)").tab('show');
$('#dayTabs').tabs();
$('#dayTabs').bind("tabsactivate", function (event, ui) {
alert('oldTab = ' + ui.oldTab.index());
alert('newTab = ' + ui.newTab.index());
sessionStorage.setItem('ssCurrentTab', ui.newTab.index());
});
$("#menuTabs a").click(function(e){
e.preventDefault();
sessionStorage.setItem('ssCurrentDate', $(this).attr('name'));
loadProgramDailyTable($(this).attr('name'));
$(this).tab('show');
});
});
HTML:
<div id="dayTabs" class="container">
<span id="menuDetails">
</span>
</div>
I realised that I was mixing bootstrap tabs with jQuery ui-tabs. The solution wss to:
Delete:
$('#dayTabs').tabs();
$('#dayTabs').bind("tabsactivate", function (event, ui) {
alert('oldTab = ' + ui.oldTab.index());
alert('newTab = ' + ui.newTab.index());
sessionStorage.setItem('ssCurrentTab', ui.newTab.index());
});
And add:
sessionStorage.setItem('ssCurrentTab', $(e.target).closest('li').index());
As per:
$("#menuTabs a").click(function(e){
e.preventDefault();
sessionStorage.setItem('ssCurrentTab', $(e.target).closest('li').index());
sessionStorage.setItem('ssCurrentDate', $(this).attr('name'));
loadProgramDailyTable($(this).attr('name'));
$(this).tab('show');
});