alien.ninja
alien.ninja

Reputation: 95

Loading tab content one tab at a time

I am needing to load the content of each tab, one at a time and not until the user clicks the submit button on each form.

I know this has been addressed several times, but I cannot find a specific answer to how I am loading my content in tabs. So here it goes:

PARENT PAGE HTML

<!--Content Area -->
<div id="tab-container">
<!--Tabs Area -->
<div id="tabs">
    <ul>
                <li><a href="#tabs-1">PAGE1</a></li>
                <li><a href="#tabs-2">PAGE2</a></li>
                <li><a href="#tabs-3">PAGE3</a></li>
                <li><a href="#tabs-4">PAGE4</a></li>
                <li><a href="#tabs-5">PAGE5</a></li>
                <li><a href="#tabs-6">PAGE6</a></li>
    </ul>
<!--End Tabs Area -->
<!--Tabs Content Area -->
    <div id="tab-content">
            <div id="tabs-1">
            </div>
            <div id="tabs-2">
            </div>
            <div id="tabs-3">
            </div> 
            <div id="tabs-4">
            </div>        
            <div id="tabs-5">
            </div>  
            <div id="tabs-6">
            </div>  
    </div>            
</div>
<!--End Tabs Content Area-->
</div>
<!--End Content Area-->
</div>

JAVASCRIPT

$(function() {

    $("#tabs").tabs({disabled: [0,1,2,3,4,5]});

)};

        $('#tabs-1').load('data/PAGE1.htm');
        $('#tabs-2').load('data/PAGE2.htm');
        $('#tabs-3').load('data/PAGE3.htm');
        $('#tabs-4').load('data/PAGE4.htm');
        $('#tabs-5').load('data/PAGE5.htm');
        $('#tabs-6').load('data/PAGE6.htm');

CHILD PAGE WITH FORM

JAVASCRIPT

$(function() {
    $('#submit1').click(function() {
        var name= $("#firstname").val();
        var genderx= $("#gender").val();
        $.ajax({
            async : "false",
            type: "POST", 
            url: "http://localhost/test/ActionServlet",
            data: { step : 1, firstname: name, gender: genderx },
            success: function(data) {
                var $emptabs = $('#tabs').tabs();
                var selected = $emptabs.tabs('option', 'selected');
                $emptabs.tabs("option", "disabled", []);
                $emptabs.tabs('select', selected+1);
                $emptabs.tabs("option", "disabled", [0,2,3,4,5]);
                $("h2").html(data);
            }

        });
    });
});

Upvotes: 1

Views: 1770

Answers (1)

alien.ninja
alien.ninja

Reputation: 95

Okay, I have found a solution for this and will share it with you all. This is actually - from what I have seen others doing - a much simpler way to do this.

Just to recap - there is a form for each tab and only the tab that has focus is enabled - all others are disabled. There is a callback from the server each time the form is submitted to verify the data was received and a message showing the information that was received is displayed - this is to make sure duplicate information is not submitted to the database but no information is added to the database at this time. At the end of the process there is a final submit button that will submit all the forms for final inclusion to the database.

All of this worked fine until we tried to load one page at a time instead of all the pages at the same time. There was a serious delay from the time the parent page loaded until the content of the first tab showed up - that's what started all of this. For whatever reason though, solutions that would work in HTML did not work in JSP but we finally came up with the following solution that works in both.

Parent page -

JAVASCRIPT - top of page in HEAD tag

var $emptabs;
$(function() {
    $("#tabs").tabs({disabled: [0,1,2,3,4,5]});
    $emptabs = $('#tabs').tabs();
});

HTML

<!--Tabs Area -->
    <div id="tabs">
        <ul>
                <li><a href="#tabs-1">Personal</a></li>
                <li><a href="#tabs-2">Emergency</a></li>
                <li><a href="#tabs-3">Job Related</a></li>
                <li><a href="#tabs-4">Salary</a></li>
                <li><a href="#tabs-5">Miscellaneous</a></li>
                <li><a href="#tabs-6">Dependents</a></li>
        </ul>
<!--End Tabs Area -->
<!--Tabs Content Area -->
    <div id="tab-content">
            <div id="tabs-1">
            </div>
            <div id="tabs-2">
            </div>
            <div id="tabs-3">
            </div>
            <div id="tabs-4">
            </div>       
            <div id="tabs-5">
            </div> 
            <div id="tabs-6">
            </div> 
        </div>           
    </div>
<!--End Tabs Content Area--> 

JAVASCRIPT - bottom of the page has to be after the tabs code

 $('#tabs-1').load('tabeedata/EmployeeGenInfo.htm');

Here is the code for the child pages - pages with the form.

JAVASCRIPT -

$(function() {
    $('#submit1').click(function() {
        var name= $("#firstname").val();
        var genderx= $("#gender").val();
        $.ajax({
            async : "false",
            type: "POST",
            url: "http://localhost/test/ActionServlet",
            data: { step : 1, firstname: name, gender: genderx },
            success: function(data) {       
                var selected = $emptabs.tabs('option', 'selected');
                $emptabs.tabs("option", "disabled", []);
                $('#tabs-2').load('tabeedata/EmployeeEmergInfo.htm');
                $emptabs.tabs('select', selected+1);
                $emptabs.tabs("option", "disabled", [0,2,3,4,5]);   
                $("h2").html(data);
            }     
        });
    });
});

As it was suggested by addams to put

$('#tabs-1').load('page.htm');

within the success handler it wouldn't work until we got rid of the individual

$emptabs = $('#tabs').tabs();

calls and made a global call on the parent page that looks like this:

var $emptabs;
$(function() {
    $("#tabs").tabs({disabled: [0,1,2,3,4,5]});
    $emptabs = $('#tabs').tabs();
});

then we were able to call each page on the submit button.

Upvotes: 2

Related Questions