The other night I spent a few hours hacking together an upload file control for this site. As part of that work, I decided to build a little file viewer which let me see all the images I had uploaded so far.
It turned out to be a pretty neat example of jQuery and a CSS grid layout, so I split out the filtering into a new demo: Filter demo.
You just type a few keystrokes and some JavaScript goes through the list of pictures and selects the ones with titles that match what you typed. Surprisingly, the JavaScript for this with jQuery is very simple:
$("#filter").keyup(function () {
var filter = $(this).val(), count = 0;
$(".filtered:first li").each(function () {
if ($(this).text().search(new RegExp(filter, "i")) < 0) {
$(this).addClass("hidden");
} else {
$(this).removeClass("hidden");
count++;
}
});
$("#filter-count").text(count);
});
It uses an input field with an ID of ‘filter’, a list contained in an element with class ‘filtered’, and uses the text inside each list item for filtering. The element with an ID of ‘filter-count’ has its value set to the number of elements remaining after the filter is applied.
This should be generic enough that it’s possible to apply it to many different situations. You could also modify the matching line to use the Quicksilver scoring algorithm.
The layout is completely CSS based, and should scale up and down and reflow nicely in browsers with decent CSS support. It took me quite a while to get that right, and I had to add one non-meaningful element to the markup to fix the width of each image-caption pair.
Have a look at the jQuery demos on http://docs.jquery.com if you need some more examples to understand how this would work.
@Jay: the JS uses this URL on my site, which randomly picks some photos from my Flickr account:
http://www.mattryall.net/flickr-photos.cgi?count=50
It could just as easily be any URL which returns XML, HTML or JSON.
An AJAX callback passed to the jQuery.get() function which receives XML or HTML will be called with an object representing the data, exactly like the jQuery.getJSON() function that I use. Have a play around with the demos in the jQuery documentation to see how this works:
http://docs.jquery.com/Ajax
IMAGES AND FLASH MOVIES
THANK YOU
I MEAN APPLYING ANY FILTER TO ALL IMAGES . THE SAME ONE TO ALL IMAGES AND VIDEO
Thank you very much!
Comments on this article have been closed.