Category Archive: Code

Dynamically generate searchable sortable tables.

Frequently throughout my pages I like to print out reports of data. The jquery tablesorter plugin is an excellent way to take this tabular data and make it sortable by column. However, since I do this so frequently, I wanted to create a script that would set up the plugin and add some other features, like an itunes like lazy search feature and highlighted hovered rows. So, on my pages, after my table is created I include the following javascript.

Note: This requires jquery and the jquery tablesorter plugin.


/*
 * Start out by building a search filter div. After the sort table is created
 * this will be placed on the page on top of it.
 */
document.write("<div id='searchfilter'>");
document.write("<input type='text' name='filter' value='' id='filter'/>");
document.write("<span class='ui-icon ui-icon-help' title='Begin typing to filter results. [ESC] to clear.'></span>");
document.write("</div>");

$(document).ready(function() {
    // Before the FilterTable, put the search filter.
    $('#searchfilter').insertBefore('#FilterTable');

    // Convert the existing table into a grid.
    $("table").tablesorter({
        widthFixed: false,
        widgets: ['zebra']
    }) ;

    // Highlight hovered row.
    $('tbody tr').hover(function(){
        $(this).find('td').addClass('hovered');
    }, function(){
          $(this).find('td').removeClass('hovered');
    });

   //Default each row to visible
   $('tbody tr').addClass('visible');

   $('#filter').keyup(function(event) {
        //if esc is pressed or nothing is entered
        if (event.keyCode == 27 || $(this).val() == '') {
            //if esc is pressed we want to clear the value of search box
            $(this).val('');

            //we want each row to be visible because if nothing
            //is entered then all rows are matched.
            $('tbody tr').removeClass('visible').show().addClass('visible');
        }
        //if there is text, lets filter
        else {
            filter('tbody tr', $(this).val());
        }//end if
    });

    //filter results based on query
    function filter(selector, query) {
      query =   $.trim(query); //trim white space
      query = query.replace(/ /gi, '|'); //add OR for regex query

      $(selector).each(function() {
        ($(this).text().search(new RegExp(query, "i")) < 0) ? $(this).hide().removeClass('visible') : $(this).show().addClass('visible');
      });
     }//end function
});

Permanent link to this article: http://www.lukemacneil.com/2010/05/07/dynamically-generate-searchable-sortable-tables/

Bash MySQL function

I use this function to run SQL queries from bash scripts.

fRunQuery(){
# This function should always be preceeded by a SQL statement
# Loaded into the variable SQL
# ie) SQL=&quot;SELECT * FROM table&quot;; fRunQuery
# The results of the query will be loaded into the variable
# $RESULT:
RESULT=`mysql --skip-column-names -h${dbhost} $db -u${dbuser} -e&quot;${SQL}&quot; -p${dbpass}`
return 0
}

Permanent link to this article: http://www.lukemacneil.com/2010/04/26/bash-mysql-function/

Javascript to format date like mm/dd/yyyy hh:mm:ss


var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;//January is 0!
var yyyy = today.getFullYear();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();
if(dd<10){dd='0'+dd}
if(mm<10){mm='0'+mm}
d=mm+'/'+dd+'/'+yyyy+' '+hours+':'+minutes+':'+seconds

Permanent link to this article: http://www.lukemacneil.com/2010/04/26/javascript-to-format-date-like-mmddyyyy-hhmmss/

AJAX waiting indicator

The following code snippet will display a waiting indicator during any AJAX request.

//Adds a wait indicator to any ajax requests
$('body').ajaxStart(function() {
$('#loading').show();
});
$('body').ajaxStop(function() {
$('#loading').hide();
});

Permanent link to this article: http://www.lukemacneil.com/2010/04/26/ajax-waiting-indicator/

Just another IE6 layout hack.

These just seem to add up.

<!--[if IE 6]>
<style type='text/css'>
div{zoom:1}
</style>
<![endif]-->

Permanent link to this article: http://www.lukemacneil.com/2010/04/26/just-another-ie6-layout-hack/

Older posts «