Keyword

LiveSearch improvement suggestions

  • Nikola
  • Nikola's Avatar Topic Author
  • Offline
  • New Member
More
9 years 4 months ago - 9 years 4 months ago #144139 by Nikola
LiveSearch improvement suggestions was created by Nikola
Hi,

the K2 Tools module acting as a live search box worked great for me, however I decided it was time to improve it a bit to suit my needs. I can honestly say that the groundwork for massive improvement has been properly laid (the code is VERY clean and straightforward), and it does not take that much time at all to make the default module work and act as 3rd party paid extensions.

Unfortunately, the one core file I had to rework to suit my needs was the main JS: components/com_k2/js/k2.js. Here is the code I am currently using, and I would really like if the devs could offer some feedback, perhaps we can make it even more awesome :)

(I will paste only the portion of the code related to Live Search function, the comments are below):
// Live search
// (1,2) added searchTimeout and searchWord variables which I will be using to optimize the amount data sent to the server
var searchTimeout = null;
var searchWord = jQuery('div.k2LiveSearchBlock form input[name=searchword]').val();
$K2('div.k2LiveSearchBlock form input[name=searchword]').keyup(function(event){
	newSearchWord = jQuery(this).val();
	var parentElement = $K2(this).parent().parent();
	if ($K2(this).val().length>3 && (event.key != 'enter') && (searchWord != newSearchWord)) {
		if (searchTimeout != null) {
			clearTimeout(searchTimeout);
		}
		searchTimeout = setTimeout(function() {
			$K2(this).addClass('k2SearchLoading');
			// (3) ORIGINAL LINE BELOW - removed to avoid blinking
			// parentElement.find('.k2LiveSearchResults').css('display','none').empty();
			parentElement.find('input[name=t]').val($K2.now());
			// (4) ORIGINAL LINE BELOW
			// parentElement.find('input[name=format]').val('raw');
			// (4) CUSTOM: new link below now calls customized view: views/itemlist/view.myview.php
			parentElement.find('input[name=format]').val('myview');
			var url = 'index.php?option=com_k2&view=itemlist&task=search&' + parentElement.find('form').serialize();
			parentElement.find('input[name=format]').val('html');
			$K2.ajax({
				url: url,
				type: 'get',
				success: function(response) {
					parentElement.find('.k2LiveSearchResults').html(response);
					parentElement.find('input[name=searchword]').removeClass('k2SearchLoading');
					// (5) ORIGINAL LINE BELOW - removed to make things smoother using fadeIn/fadeOut
					// parentElement.find('.k2LiveSearchResults').css('display', 'block');
					// (5) CUSTOM: if results are already displayed, do nothing, else fadein the block
					if (jQuery('.k2LiveSearchResults').css('display') != 'block') {
						parentElement.find('.k2LiveSearchResults').fadeIn(250);
					}
				}
			});
		}, 250);
	}
	// (6) remove results for if search string contains less than 4 chars; ELSE doesn't work here anymore because of && searchWord up
	if ($K2(this).val().length <= 3) {
		parentElement.find('.k2LiveSearchResults').fadeOut(250).empty();
	}
	// (2) record newly entered searchWord as current
	searchWord = jQuery(this).val();
});

* please note that I am using view.myview.php because I require images, item categories, etc. The easiest way to build it is to modify view.raw.php to suit your own needs

Comments (for non-developers and people with less experience):

(1) the searchTimeout variable is used to add the timeout between keystrokes. I've set it to 250 ms, seems to work for me, but it can be tweaked further. What do we achieve? Say there's no timeout and out search word is "joomla". keyup() reacts to each keystroke immediately, so even though we don't see it, even if we type the word in 0.1s, there are 6 queries: "j", "jo", "joo", "joom", "jooml" and "joomla". By adding a 250ms timeout, the script waits for us to pause for at least 250ms before sending a request. This way, we can type relatively slowly and still send a single query to the server, instead of 6, dropping the load.

(2) the searchWord variable is one way of solving yet another keyup problem: it reacts to EACH keystroke, even if we press CTRL, or use arrows to move the cursor, in this case by sending yet another request to the server. The searchWord and newSearchWord variables are used to check if the input field contents have been changed after a keystroke; if so, the request is sent. If not, nothing happens. Please note that there is now an additional condition in the line beginning with "if ($K2(this).val().length...".

(3,4,5,6) just cosmetic improvements; instead of using .css('display', 'block') and .css('display','none'), I am now using the integrated jQuery fade() which makes things a lot nicer. The line (3) can be removed because instead of "else" at the end, we have a new condition (6) which fades the result window out and clears its contents as soon as the input box contains less than 3 chars. "Else" was not working because of the added "searchWord condition". If the result window is already displayed and we change, for example, a single character in the search box, there is no need to clear, remove and redraw it, hence (5) CUSTOM.

If you want to be able to close the result window by clicking outside of it, here's an interesting piece of code I found:
jQuery(document).mouseup(function (e) {
	var container = jQuery(".k2LiveSearchResults");
	// if the target of the click isn't the container (&&) nor a descendant of the container
	if (!container.is(e.target) && container.has(e.target).length === 0) {
		container.fadeOut(200, function() {
			jQuery(this).css('display','none');
		});
	}
});

The code can be added, say, to your custom result view or pretty much anywhere at your own convenience.

Hope somebody finds this useful, it took me a day's work, but I've saved me some 35 USD :)

Once again, kudos to the K2 team, I never forget to tell these guys how much they mean to me :)

PS the livedemo of my "improved" search should be available in a day or two and I'll be glad to post it here :)
Last edit: 9 years 4 months ago by Nikola. Reason: added PS

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 4 months ago #144155 by Krikor Boghossian
Replied by Krikor Boghossian on topic LiveSearch improvement suggestions
Great work Nikola :)

This looks really nice. I look forward to testing it.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • Nikola
  • Nikola's Avatar Topic Author
  • Offline
  • New Member
More
9 years 3 months ago #144815 by Nikola
Replied by Nikola on topic LiveSearch improvement suggestions
Hey there,

sorry it took me a while, but the website containing my modifications is up. We've had other, non-web-related troubles :(

Anyway, the address is: izdavastvo.filfak.ni.ac.rs

This website is a publications catalog for the Faculty of Philosophy in Niš, Serbia. It is cyrilic, howerer you can test the search module by typing, for example, "philo" or "годиш" (copypaste then hit space to activate keyup event). I guess there is room for improvement (live pagination etc) but I think it's a pretty good start.

The filtering module on the right is a 3rd party extension, however now that I feel more relaxed about tweaking K2, I might try to create a similar free version of this for community.

If you have any suggestions, please ... shoot :)

Cheers!

PS I've submitted the main Faculty of Philosophy website for the Showcase on getk2.org, but it never got published. I did everything according to guidelines, is there something else I should be aware of? By the way, the website relies heavily on K2 and its templating system, and features error-proof content entry forms for non-experienced users (an idea of mine I posted about some time ago - overriding userform.php and creating several different overrides in a single file using itemid).

Please Log in or Create an account to join the conversation.

  • Krikor Boghossian
  • Krikor Boghossian's Avatar
  • Offline
  • Platinum Member
More
9 years 3 months ago #144821 by Krikor Boghossian
Replied by Krikor Boghossian on topic LiveSearch improvement suggestions
It is actually pretty nice Nikola.
I actually think this something worth into turning into an actual product.

When did you submit your site to the showcase?

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum