Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing 'url' method in Jquery UI 1.9 Tabs

I am working on a web application and was making great progress using JQuery UI Tabs, specifically the 'url' method of dynamically updating the target url of the ajax request which was bound to each tab. I have created a simplified example here:

Each of the 2 Tabs, function 1 & function 2, display the results of some tests based on the parameters that are passed to the test. The tests remain the same throughout, but I was changing the variables used in the functions by manipulating the url bound to the tab. The 'url' method of updating the url being called on a lazy loading Tab allowed me to do this, but its now been deprecated and won't be supported at all soon. So I have been searching for a way to do this using the 'beforeLoad' event in JQuery UI 1.9.

The suggestion was that one could manipulate the ui.ajaxSettings to set the data parameter but there is a bug (http://bugs.jqueryui.com/ticket/8673), and it appears this won't be fixed. The workaround suggested is to manipulate the ui.ajaxSettings.url instead.

My working example is below, but I'm not sure if this is the cleanest / most appropriate way to achieve what I'm after and any suggestions would be most welcome. I struggled to find examples on how to do even this much, so hopefully it will help someone else in a similar situation.

<title> Jquery Tabs AJAX Test</title>

<script type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.9.1.custom.js"></script>
<link rel="stylesheet" type="text/css" href="css/smoothness/jquery-ui-1.9.1.custom.css">

</head>
<body >

<script type=text/javascript>
    $(document).ready(function() {

        $( '#tabs' ).tabs({  }); 

         $('button.set_variable').click(function() { 
             var variable = $(this).val(); 
             $( '#tabs' ).tabs({ 
                 beforeLoad: function( event, ui ) { 
                     ui.ajaxSettings.url += "&variable=" + variable ; 
                     } 
                 }); 
             var selected_tab = $('#tabs').tabs('option', 'selected'); 
             $( '#tabs' ).tabs("load", selected_tab); 
          }); 
     })
</script>

    <button class="set_variable" value="1">Variable = 1</button> 
    <button class="set_variable" value="2">Variable = 2</button> 
    <button class="set_variable" value="3">Variable = 3</button> 

    <div id="tabs" > 
    <ul> 
         <li><a href="ajax.php?func=ajax-func1">Function 1 </a></li> 
         <li><a href="ajax.php?func=ajax-func2">Function 2 </a></li> 
    </ul> 
    </div> 
</body></html>

N.B. The ajax.php file here just echos back what was passed to it.

<?php
    extract($_GET);
    var_dump($_GET);
?>
like image 398
Boidy Avatar asked Nov 03 '22 10:11

Boidy


1 Answers

You could do something like this too :

var variable = '';
$( '#tabs' ).tabs({  beforeLoad: function( event, ui ) { 
    if(variable != '')
         ui.ajaxSettings.url += "&variable=" + variable ; 
}}); 

$('button.set_variable').click(function() { 
     variable = $(this).val(); 
}); 

http://jsfiddle.net/DNWzY/1/

like image 104
Shikiryu Avatar answered Nov 08 '22 03:11

Shikiryu