Thursday, February 4, 2010

jQuery/Plugin : Problem with Add/Remove row for Tablesorter Plugin

For using the jQuery tablesorter (http://tablesorter.com/docs/ ), the table will not refresh correctly if you use the additional pager plugin.

To solve this problem, you have to add in 2 customised functions/events to take care of the row adding/removal.

Add the following 2 functions to the tablesoter javascript file.

function addRow(table, newRow){

//Rebuild html table from the modified rowsCopy
$.tablesorter.clearTableBody(table);

if (table.config.rowsCopy!=undefined)
$.each(table.config.rowsCopy, function() {
$(table).find('tbody:first').append($(this));
});

//Append new row to the HTML table
$(table).find('tbody:first').append(newRow);

//Update call will rebuild tablesorter.cache from the existing HTML table
$(table).trigger("update");
//With pager plugin, this will call appender function in pager
//In turn, it will redraw the table with pagination
$(table).trigger("appendCache");
//Retain sorting if any
$(table).trigger("sorton",[table.config.sortList]);
}

function removeRow(table, rowId){
//Remove the selected row from pager.rowsCopy
var newRowsCopy = $.grep(table.config.rowsCopy, function(n, i){
return ($(n).attr('id')!=rowId);
});

table.config.rowsCopy = newRowsCopy;

//Rebuild html table from the modified rowsCopy
$.tablesorter.clearTableBody(table);

if (table.config.rowsCopy!=undefined)
$.each(table.config.rowsCopy, function() {
$(table).find('tbody:first').append($(this));
});

//Update call will rebuild tablesorter.cache from the existing HTML table
$(table).trigger("update");
//With pager plugin, this will call appender function in pager
//In turn, it will redraw the table with pagination
$(table).trigger("appendCache");
}



You can bind the functions 2 customised events as follows:
   $this.bind("addRow",function(event,newRow){
addRow(this,newRow);
});

$this.bind("removeRow",function(event,rowId){
removeRow(this,rowId);
});



Finally you can call the events from your page as follows:
  $(sortTable).trigger("removeRow",[id for the row to be removed]);

var newTr = $('<tr><td>New Row</td></tr>');
$(sortTable).trigger("addRow",[newTr]);

Deploy Netbeans 6.1 Web Application to Tomcat 5.5

Netbeans 6.1 come bundles with Tomcat 6.0. So you will encounter problem if you upload the war file directly to your production Tomcat 5.5 server.

Reason being the web.xml in you war file use version 2.5 XML declaration but Tomcat 5.5 uses version 2.4. The web.xml is located at
<catalina-home>/webapps/<your apps>/WEB_INF

Look at the first two lines of the web.xml, you shall see something like the following

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">


Whereas in Tomcat 5.5, the two lines shall be replaced with the following

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

After which, you have to clear the cache of compiled JSP file.
<catalina-home>\work\Catalina\<your apps>

Remove the whole folder with name "_" (underscore). This is the compiled JSP file (java class) by tomcat and is compiled only once when the jsp page is accessed/requested for the first time.

You have to stop the tomcat before removing the folder.
The folder will be rebuild automatically after you have restarted the tomcat and the jsp is being accessed.