Archive for April, 2008

20
Apr

Hiding Tables with Empty Body Elements With jQuery

I needed a quick, client-side way to hide empty tables. I thought that something like this should have done the trick with jQuery:

$(document).ready(function(){
  $('tbody:empty').parents('table').hide();
});

Basically, we’re just finding all tbody elements that are empty using the :empty filter and hiding the parent table element.

But, as it turns out, the tables that were being generated had whitespace inside their tbody elements, so they weren’t technically empty since :empty considers text nodes, even whitespace, as non-empty (as per the jQuery documentation).

One solution is to simply trim the whitespace from inside the tbody elements before checking if they’re empty:

$(document).ready(function(){
  $('tbody').each(function(){
    $(this).html($.trim($(this).html()))
  });
  $('tbody:empty').parents('table').hide();
});
18
Apr

Simple Image Submit Button Rollovers with jQuery

Have you ever wanted a simple rollover technique with a form submission button? Something like this:

rollover up state

rollover over state

… without having to resort to a complicated mess of javascript form submission and cross browser compatibility issues?

With jQuery it’s really easy. All you need to do is include a standard image form submission tag, like so:

<input type="image" name="submit" id="submit" src="enter.gif">

… and add the hover behavior to the submit input tag. Something like this:

<script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
        $('#submit').hover(
            function(){ // Change the input image's source when we "roll on"
                $(this).attr({ src : 'enter_over.gif'});
            },
            function(){ // Change the input image's source back to the default on "roll off"
                $(this).attr({ src : 'enter.gif'});             }
        );
    });
</script>

This is just a simple example, but you could definitely spruce it up if you needed something more elaborate by adding/removing CSS attributes and the like. Check out the jQuery documentation for more on the hover event.

The benefit of using this method is that the form will be submitted using the standard form submission action, rather than by calling submit() directly through javascript like with other techniques I’ve seen. This has the added benefit that standard form handlers like “onSubmit” will still work whereas, using the submit() method directly will bypass these handlers.

05
Apr

MAMP Might Break Your Time Machine Backups

After many frustrating hours of trying to figure out why my Time Machine backups were failing (actually, they’d stall after being really close to finishing… they’d just sit there forever) I finally narrowed it down to one set of files: a local symfony project.

At first, I thought that perhaps something weird was happening with the symlinks that I use to link a Symfony’s “/sf” directory to the Symfony library’s web directory, but upon further investigation it boiled down to the Symfony cache files. I just happened to notice that for some reason the cache files were being generated with the user/group combination “mark/nogroup”… “mark” is my login name, so this seemed fine, but “nogroup” seemed a bit odd. After changing the group associated with these files to “staff” like the other files in the project, the Time Machine backup worked fine. So, apparently Time Machine somehow chokes on the “nogroup” group.

Now, I had just switched to using MAMP Pro instead of rolling my own Apache/PHP/MySQL because I wanted my setup to be completely portable from one machine to the next (with little effort), so I was a little bit disappointed with this, but there’s an easy fix. Open up MAMP Pro and notice the “Run Apache/MySQL server as user” dropdown:

mark/mark

I had left this as the default which uses my username “mark/mark” but since there was no “mark” group, it was using “nogroup” and causing Time Machine to explode. So, I changed this to use “www/mysql” like so:

www/mysql

…and now the cache files are being generated with the “www/www” user/group combo and Time Machine backs up as it should.

Although this fix is not necessarily Symfony specific (it really has to do with any file that’s written by the MAMP apache server process), it may save someone some trouble if they’ve got a similar Symfony set up going and they can’t figure out why their Time Machine backup isn’t working as advertised.

P.s. You can find files with a certain group by running this command in the terminal:

 find . -group nogroup

This will find any files in the current directory that belong to the “nogroup” group.