Saturday, February 23, 2013

jQuery UI tab v1.9 Dynamic add/ remove tab with close icon at corner Simple Example

<html>
<body>
  <button id="b_add1">Add Tab</button>
  <div id='myTab' style="display:block">
    <ul><li><a href='#tabpg0'>tabpg-0</a></li>
    </ul>
    <div id="tabpg0">content</div>
  </div>
</body>
</html>

<javascript>

l_tabpgIndex=0;

function addTab() {
  l_tabpgIndex++;

  var href='#tabpg'+l_tabpgIndex;
  var title = 'Tabpg-'+l_tabpgIndex;

  var $tabs = $('#myTab').tabs();
  $tabs.append('<div id="tabpg'+l_tabpgIndex+'"></div>');

//In order to make the closeicon works easiler, I gave an Id to the element <li>, which is different to the example the jQuery's document given.
  $('<li id="li-'+l_tabpgIndex+'"><span id="closeicon-'+l_tabpgIndex+'" class="ui-icon ui-icon-close" style="float:right; cursor: pointer;"></span><a href="'+href+'">'+title+'</a></li>').appendTo( "#myTab .ui-tabs-nav" );

  $tabs.tabs( "refresh" );

  var newSelected = $('#myTab >ul >li').size() - 1;
  $tabs.tabs("option", "active", newSelected);  //switch to the NEW tab-page which is just created.

  init_closeIcon(l_tabpgIndex); //passing an argument that is able to point to the Id.
}

function init_closeIcon(value) {
 $('#myTab ul li').on('click','#closeicon-'+value,function(){
   removeTab(value);
 });
}

function removeTab(value){
  var $tabs = $('#myTab').tabs();

  $('#li-'+value).remove();  //this removes the tab; which is different to the example you found on jQuery's document. http://jqueryui.com/upgrade-guide/1.9/#deprecated-add-and-remove-methods-and-events-use-refresh-method

  var panelId = tab.attr( "aria-controls" );
  $('#'+panelId ).remove(); //this remove the panel
  $tabs.tabs( "refresh" );
}

$('#b_add1').click(function(){ addTab(); });
</javascript>

Monday, February 18, 2013

jQuery UI 1.10.x : button - custom icon NOT show?

I upgraded jQueryUI from 1.9.x to 1.10.x and found that my buttons' custom icon did NOT show.



However, it didn't work after I upgraded to 1.10.x

After I spend a few hours, I found the solution only be found on the jQuery bug report page, http://bugs.jqueryui.com/ticket/5659; I think this should be mentioned on the Upgrade Guide as the last contributor said. All credit goes to the contributor who posted the solution there as below.



Just want the ppl, who were facing the same problem, can save up some time when google this solution.