We want all of our AJAX calls in our web app to receive JSON-encoded content. In most places this is already done (e.g. in modals) and works fine.
However, when using jQueryUI's tabs (http://jqueryui.com/demos/tabs/) and their ajax functionality, only plaintext HTML can be returned (i.e. from the URLs specified in the a tags below). How do I get the tab function to recognize that on each tab's click, it will be receiving JSON-encoded data from the specified URL, and to load in the .content index of that JSON?
$(function() {
$('div#myTabs').tabs();
});
<div id="mytabs" class="ui-tabs ui-widget ui-widget-content ui-corner-all">
<ul class="ui-tabs-nav ui-helper-reset ui-helper-clearfix ui-widget-header ui-corner-all">
<li class="ui-state-default ui-corner-top"><a href="/url/one">Tab one</a></li>
<li class="ui-state-default ui-corner-top"><a href="/url/two">Tab two</a></li>
</ul>
</div>
You can use the dataFilter option of the ajax call to convert your json response to the html you want to be inserted in to the panel of the tab.
Something like this:
If you JSON response looked like this:
I got stuck with this problem recently, so just in case someone has been looking for help with this kind of issue more recently, I thought it would be useful to re-open this discussion. I'm working with jQuery 1.8.2 and jQuery UI 1.9.2. I found the best way to do this with the latest version of jQuery UI was to return
false
from thebeforeLoad
event handler and send agetJSON
request with the URL specified in the relevant tab.HTML markup:
Tabs invocation code:
When the
beforeLoad
handler returns false, the normal functionality of displaying the HTML retrieved from the URL defined in the tab is disabled. However, you still have access to theevent
andui
objects passed to thebeforeLoad
handler. You get the URL from the tab with this syntaxui.ajaxSettings.url
and then display the data returned by thegetJSON
request as shown in the Content via Ajax example:In my case, it's data.text because the JSON I'm retrieving contains two properties
header
andtext
.I had to add:
to get this to work.
In context: