tags:

views:

1563

answers:

2

I'm using jquery tabs (http://docs.jquery.com/UI/API/1.7.2/Tabs). Jquery version 1.3.2 and Jquery UI version 1.7.2 and Ive been testing in firefox 3.5.6.

When selecting a tab I just add the current date to the content area html. I also have a link with the class name "Button". When this link is clicked I want to reload the content of the currently selected tab. But with the solution I've tried I can't get it to work. I can see that the "button clicked" event is loaded but the following code isn't reloading my data:

$(".Tabs").tabs('load', $(".Tabs").tabs('option', 'selected'));

I have also been testing with:

var selected = $(".Tabs").tabs('option', 'selected');
$(".Tabs").tabs('select', null);
$(".Tabs").tabs('select', selected);

But that doesn't work either, my select method never gets called when pushing the button

This is my jquery code:

    $(function() {
    var $tabs = $(".Tabs").tabs({
        selected: null,
        select: function() {
            alert("this doesn't run on button click");
            //Sets Content's html to current date
            $("#Content").html(new Date);
        }
    });

    $('.Button').click(function() {
        alert("this runs on button click");
        //Here I want to reload the current selected tab
        $(".Tabs").tabs('load', $(".Tabs").tabs('option', 'selected'));
        return false;
    });
    $('.Tabs').tabs('select', 0); // Select first tab
});

This is the html:

<div class="Tabs">
    <ul>
        <li><a href="#Content">Tab1</a></li>
        <li><a href="#Content">Tab2</a></li>
    </ul>
    <div id="Content">

    </div>
</div>
<a class='Button' href='#'>Load Content Again</a>
A: 

Two things:

  1. Firefox + Firebug plugin are your friends. Use them.
  2. Look for an onClick routine that is not doing a return false; By saying return 'false you tell the browser to not actually go to the URL in the href attribute.
Peter Rowell
+1  A: 

if your .Button class is inside the content that is loaded, you will need to use the live functionality of jQuery.

$('.Button').live('click', function() {
 //Here I want to reload the current selected tab
 $tabs.tabs('load', $tabs.tabs('option', 'selected'));
 return false;
});

Also, since .Button is a link, you'll need to add return false; inside that function.


From looking over your code, I'm not sure why you have it set up to not load a tab until after you click on one. Also clicking on any tab will always load the same content (the url doesn't change). And lastly, you shouldn't need to use eval() the script (could this be the problem?).

Besides these issues, it looks like your code should work.

I'm also not sure how your json is formatted, so I rewrote this assuming the following json format:

({
 "items": [
  { "title": "example 1" },
  { "title": "example 2" },
  { "title": "example 3" },
  { "title": "example 4" },
  { "title": "example 5" },
  { "title": "example 6" },
  { "title": "example 7" },
  { "title": "example 8" },
  { "title": "example 9" },
  { "title": "example 10" }
 ]
})

Script

$(function() {
 var $tabs = $(".Tabs").tabs({
  selected: null,
  select: function(event, ui) {
   loadTab( ui.index ); // ui.index = index of the clicked tab
  }
 });
 $('.Button').live('click', function() {
  //Here I want to reload the current selected tab
  loadTab( $(".Tabs").tabs('option', 'selected') );
  return false;
 });
 $('.Tabs').tabs('select',0);
});

function loadTab(indx){
 $.ajax({
  type: "GET",
  url: "http://domain.com/Service.svc/get",
  dataType: "json",
  success: function(data) {
   var content = "";
   $.each(data.items, function(items){
    content += "<a class='Button' href='#'>" + this.title + "</a><br>";
   });
   $("#Content").html(content + "<br />Tab Index #" + indx + " on " + (new Date()).toUTCString());
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
   if (!$('#error').length) $("#Content").prepend('<div id="error"></div>');
   $('#error').html(textStatus + '<br /><br />');
  }
 })
}
fudgey
Thanks! But it didn't work.. I updated the question, I tried adding the return false and changing to live but the content is still not loaded. I can see that the "button clicked" event runs but then I'm calling load but my content is not loaded. As you can see in the code my content loads in select: function(e, ui). Maybe select doesn't run when calling load? What am I doing wrong?
Martin
Also I can see that the select: function(e, ui) doesn't run at all. So I tried adding $tabs.tabs('select', $tabs.tabs('option', 'selected')); before I load the data but that doesn't make the select function load either. What can be wrong?
Martin
Actually the function from my answer should be outside of the tabs function, but still inside the `$(function() { ... })`. I'll have to look at it a bit more to see why the select function isn't being called.
fudgey
Thanks a lot! I have it set up to just load the data when the tab is clicked because I don't want to load all the content at once. My code works if I click the tabs so I don't think the eval() could be problems with that code but the select function is not called, which I think is strange. Yes right now I'm calling the same web service but will change that later when I got this working. Can't see why the select function isn't called, what can be wrong?
Martin
AHHH... change the `$tabs` to `$('.Tabs')` in the click function... I'll update my answer
fudgey
But that didn't help either? The live-click function runs but then when calling .tabs('load'..) the select function never gets called. Does it work for you?
Martin
Well for me, the initial tab is empty. When I click on tab 2, it loads the json data and displays the links inside of tab 2. When I click back on tab 1, I see the same data (I'm not using php, it's just a static file to test this). If I add (this is probably not the right way to do this) `$('.Tabs').tabs('select',1).tabs('select',0);` it selects the second tab, loads the data there, then switches back to the first tab with all the data displaying. I'm not quite sure what you are trying to do, and hopefully your script provides different json data depending on the tab.
fudgey
Just in case you did want to send some data to your server scripting.. you can use `data` in the ajax function (http://docs.jquery.com/Ajax/jQuery.ajax#options).
fudgey
Yes, select and then select again works for me too. But it doesnt seem like the right solution. There must be an easier way? This must be quite standard, having a list inside a tab and the content of the list comes from an ajax call. Then when clicking a button in the list, it should reload from ajax.
Martin
Try changing `select: function() {` to `show: function() {`
fudgey
still the same. If I select another tab first then select the old tab again it works.
Martin
That is very odd, I changed my code above to use `show`. I'm about out of ideas. So your inital tab remains completely blank? In my testing (on my desktop) it loads and parses the json file. Can you add an alert inside the `show` or `select` function and see if it even triggers. I tested my files in FF, IE and Chrome and they all work.
fudgey
Hmmm, actually looking through SO, I found this answer (http://stackoverflow.com/questions/970838/selecting-loading-a-jquery-tab-programatically/970877#970877)... try using this: `.tabs( { selected: null } );` then initializing the tab `.tabs("select", 0);`... I'll update my answer above.
fudgey
Thanks (again)! :) Yes, the initial null was nice, now it loads the content in the first tab the first time. But $(".Tabs").tabs('load', $(".Tabs").tabs('option', 'selected')) doesn't call select, it does for you? Instead I tried using .tabs("select", null) and then .tabs("select", the_index_of_the_tab_that_was_selected_before) but that doesn't call select function either. The only way seems to be .tabs("select", 0) and then .tabs("select", the_index_of_the_tab_that_was_selected_before) but that doesn't work for the first tab (0) and seems very strange...
Martin
I accidentally marked your answer as "Solution" however its not working for me. I updated my question with a much simplier example and also added what versions I'm using. Can you please test the code I provided in my question and see if it works for you?
Martin
Ok, now that I see what you want... I think the addon ignores clicks and calls on the curretn tab, so I think the easiest solution would be to pull the ajax function out of the tabs and make the button click function call it. I've updated my answer above with these changes. I hope that fixes it!
fudgey
Thanks really much! That did it! :-)
Martin
That's great! :)
fudgey