Hi guys, I stumbled upon a pretty nifty photo image search filter which searches and updates pictures to show when you start to type. Below is the full code to create your own Image search with images feed from Flickr.
jQuery Code for Live Image Search
$("#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);
});
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 the Live Search with QuickSilver Style jQuery plugin based upon the QuickSilver string ranking algorithm in JavaScript.
Quicksilver Live Search Plugin
(function($) {
var self = null;
$.fn.liveUpdate = function(list) {
return this.each(function() {
new $.liveUpdate(this, list);
});
};
$.liveUpdate = function (e, list) {
this.field = $(e);
this.list = $('#' + list);
if (this.list.length > 0) {
this.init();
}
};
$.liveUpdate.prototype = {
init: function() {
var self = this;
this.setupCache();
this.field.parents('form').submit(function() { return false; });
this.field.keyup(function() { self.filter(); });
self.filter();
},
filter: function() {
if ($.trim(this.field.val()) == '') { this.list.children('li').show(); return; }
this.displayResults(this.getScores(this.field.val().toLowerCase()));
},
setupCache: function() {
var self = this;
this.cache = [];
this.rows = [];
this.list.children('li').each(function() {
self.cache.push(this.innerHTML.toLowerCase());
self.rows.push($(this));
});
this.cache_length = this.cache.length;
},
displayResults: function(scores) {
var self = this;
this.list.children('li').hide();
$.each(scores, function(i, score) { self.rows[score[1]].show(); });
},
getScores: function(term) {
var scores = [];
for (var i=0; i < this.cache_length; i++) {
var score = this.cache[i].score(term);
if (score > 0) { scores.push([score, i]); }
}
return scores.sort(function(a, b) { return b[0] - a[0]; });
}
}
})(jQuery);
var self = null;
$.fn.liveUpdate = function(list) {
return this.each(function() {
new $.liveUpdate(this, list);
});
};
$.liveUpdate = function (e, list) {
this.field = $(e);
this.list = $('#' + list);
if (this.list.length > 0) {
this.init();
}
};
$.liveUpdate.prototype = {
init: function() {
var self = this;
this.setupCache();
this.field.parents('form').submit(function() { return false; });
this.field.keyup(function() { self.filter(); });
self.filter();
},
filter: function() {
if ($.trim(this.field.val()) == '') { this.list.children('li').show(); return; }
this.displayResults(this.getScores(this.field.val().toLowerCase()));
},
setupCache: function() {
var self = this;
this.cache = [];
this.rows = [];
this.list.children('li').each(function() {
self.cache.push(this.innerHTML.toLowerCase());
self.rows.push($(this));
});
this.cache_length = this.cache.length;
},
displayResults: function(scores) {
var self = this;
this.list.children('li').hide();
$.each(scores, function(i, score) { self.rows[score[1]].show(); });
},
getScores: function(term) {
var scores = [];
for (var i=0; i < this.cache_length; i++) {
var score = this.cache[i].score(term);
if (score > 0) { scores.push([score, i]); }
}
return scores.sort(function(a, b) { return b[0] - a[0]; });
}
}
})(jQuery);
Full Code for the Image Search
/*
* jQuery Filter Demo
* Matt Ryall
* http://www.mattryall.net/blog/2008/07/jquery-filter-demo
*
* Licensed under Creative Commons Attribution 3.0.
* http://creativecommons.org/licenses/by/3.0/
*/
jQuery(function ($) {
var thumbnailUrl = "http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_s.jpg";
var linkUrl = "http://www.flickr.com/photos/mjryall/{id}/";
$.getJSON("/flickr-photos.cgi?count=50", function (data) {
var photos = data.photos.photo;
var list = $("<ul></ul");
$.each(photos, function (i, photo) {
var url = thumbnailUrl.replace("{farm-id}", photo.farm)
.replace("{server-id}", photo.server)
.replace("{id}", photo.id)
.replace("{secret}", photo.secret);
var img = $("<img/>").attr("src", url)
.attr("title", photo.title).attr("alt", "A photo on Flickr");
var href = linkUrl.replace("{id}", photo.id);
var link = $("<a></a>").attr("href", href).append(img);
var caption = $("<a/>").attr("href", href)
.text(photo.title).addClass("caption");
var div = $("<div/>").append(link).append(caption);
$(list).append($("<li/>").append(div));
});
$("#flickr-photos .loading").remove();
$("#flickr-photos").append(list);
})
$("#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);
});
});
* jQuery Filter Demo
* Matt Ryall
* http://www.mattryall.net/blog/2008/07/jquery-filter-demo
*
* Licensed under Creative Commons Attribution 3.0.
* http://creativecommons.org/licenses/by/3.0/
*/
jQuery(function ($) {
var thumbnailUrl = "http://farm{farm-id}.static.flickr.com/{server-id}/{id}_{secret}_s.jpg";
var linkUrl = "http://www.flickr.com/photos/mjryall/{id}/";
$.getJSON("/flickr-photos.cgi?count=50", function (data) {
var photos = data.photos.photo;
var list = $("<ul></ul");
$.each(photos, function (i, photo) {
var url = thumbnailUrl.replace("{farm-id}", photo.farm)
.replace("{server-id}", photo.server)
.replace("{id}", photo.id)
.replace("{secret}", photo.secret);
var img = $("<img/>").attr("src", url)
.attr("title", photo.title).attr("alt", "A photo on Flickr");
var href = linkUrl.replace("{id}", photo.id);
var link = $("<a></a>").attr("href", href).append(img);
var caption = $("<a/>").attr("href", href)
.text(photo.title).addClass("caption");
var div = $("<div/>").append(link).append(caption);
$(list).append($("<li/>").append(div));
});
$("#flickr-photos .loading").remove();
$("#flickr-photos").append(list);
})
$("#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);
});
});




Pingback: 分享jQuery的5个动态过滤插件 | 南龙的小站