Was this page helpful?

How do I... Make an ajax search suggest tool using the MindTouch API

    Table of contents
    1. 1. HTML
    2. 2. JQUERY
    3. 3. CSS
    How do I... Make an ajax search suggest tool using the MindTouch API Contributors
     
    Views: 
     

    > Full Code

    x
    pages
    files
    comments

     

     Any questions or comments should be directed to this forum thread

     

    HTML

    In order to get our search suggest working we need to layout some basic HTML.  This HTML includes the search input and the dropdown menu that will later get populated by jQuery.  You must add this HTML to a MindTouch page in source mode.

    <input type="text" id="search-input">
    
    <div id="search-suggest">
    	<a href="#" class="close">x</a>
    	<div class="search-pages">
    		<strong>pages</strong>
    	</div>
    	<div class="search-files">
    		<strong>files</strong>
    	</div>
    	<div class="search-comments">
    		<strong>comments</strong>
    	</div>
    </div>

    JQUERY

    Now that you know how to access the API and you have your base HTML you need to connect all the parts.   Again this needs to be entered in source mode.  I've added some highlighted comments to walk you through the jquery.

    <script type="text/javascript">
    	$("body").ready( function() {
    		$("#search-suggest a.close").click( function() {
    			$("#search-suggest").hide();
    			return false;
    		});
    	
    		$("#search-input").keyup( function() {
    			if ($(this).val()==''){
    				$("#search-suggest").hide();
    				return false;
    			}
    	
    			var q = $(this).val();
    			
    			clearTimeout($.data(this, "timer"));
    		    var ms = 400; //milliseconds
    		    var wait = setTimeout(function() {
    		      loadsearch(q);
    		    }, ms);
    	    
    	
    			$.data(this, "timer", wait);
    		});
    	});
    	
    function loadsearch(q) {
    	var qurl="http://developer.mindtouch.com/@api/deki/site/search?limit=10&q="+q;
    	var cntpage=0;
    	var cntfile=0;
    	var cntcomments=0;
    	jQuery.get(qurl, function(xml) {
    		$("#search-suggest .search-pages a").remove();
    		$("#search-suggest .search-files a").remove();
    		$("#search-suggest .search-comments a").remove();
    		$("#search-suggest .search-comments span").remove();
    	
    		$(xml).find('search > page').each(function(){
    			var qpath = $(this).find(" > path").text();
    			var qtitle = $(this).find("> title").text();
    		$("#search-suggest .search-pages ").append('<a class="page" href="/' + qpath + '">' + qtitle.substr(0,30) + '</a>');
    			cntpage++;
    		});
    		
    		$(xml).find('search > file').each(function(){
    			var quri = $(this).find(" > contents").attr("href");
    			var qtitle = $(this).find(" > filename").text();
    			$("#search-suggest .search-files").append('<a class="file" href="' + quri + '">' + qtitle + '</a>');
    			cntfile++;
    		});
    		
    		$(xml).find('search > comment').each(function(){
    			var quser = $(this).find("username").text();
    			var qcomment = $(this).find(" > content").text();
    			var quri = $(this).find("path").text();
    			$("#search-suggest .search-comments").append('<a href="/' + quri + '" class="comment-user">' + quser + '</a><span class="comment-text">' + qcomment.substr(0,40) + '...</span>');
    			cntcomments++;
    		});
    		
    		if(cntpage>0){
    			$("#search-suggest .search-pages").show();
    		}else {
    			$("#search-suggest .search-pages").hide();
    		}
    		
    		if(cntfile>0){
    			$("#search-suggest .search-files").show();
    		}else {
    			$("#search-suggest .search-files").hide();
    		}
    		
    		if(cntcomments>0){
    			$("#search-suggest .search-comments").show();
    		}else {
    			$("#search-suggest .search-comments").hide();
    		}
    		
    		if(cntpage>0 || cntfile>0 || cntcomments>0){
    			$("#search-suggest").show();
    		}else {
    			$("#search-suggest").hide();
    		}
    	});
    }
    </script>

    CSS

    Lastly, once you've got it working it needs to look good, otherwise it might not be legible at all.   I've put some simple CSS together but feel free customize the look & feel.

    <div>
    <style>
    #search-suggest strong{
    	margin:0 0 10px 0;
    	color:#999;
    	display:block;
    }
    #search-suggest {
    	border:1px solid #ccc;
    	display:none;
    	padding:0;
    	position:absolute;
    	overflow:hidden;
    	background:#fff;
    	font-size:11px;
    	min-width:150px;
    }
    #search-suggest .search-pages{
    	float:left;
    	padding:5px 20px;
    	max-width:200px;
    }
    #search-suggest .search-files{
    	float:left;
    	padding:5px 20px;
    	max-width:200px;
    }
    #search-suggest .search-comments{
    	float:left;
    	padding:5px 20px;
    	max-width:200px;
    }
    #search-suggest a {
    	display:block;
    	padding:0 0 0 20px;
    	height:16px;
    	overflow:hidden;
    	margin:0 0 5px 0;
    	background-image:url(/skins/common/icons/icons.gif);
    	background-repeat:no-repeat;
    }
    #search-suggest .search-pages a {
    	background-position:0 -768px;
    	
    }
    #search-suggest .search-files a {
    	background-position:0 -1040px;
    }
    #search-suggest .search-comments .comment-user {
    	display:block;
    	font-weight:bold;
    	margin-bottom:0px;
    	padding:0;
    }
    #search-suggest .search-comments .comment-text {
    	display:block;
    	font-weight:100;
    	margin-bottom:10px;
    	color:#999;
    }
    .no-show {
    	display:none;
    }
    #search-suggest a.close{
    	float:right;
    	display:block;
    	font-size:14px;
    	width:15px;
    	padding:0 0 1px 0;
    	line-height:1;
    	-moz-border-radius:7px;
    	border:2px solid #fff;
    	font-weight:bold;
    	text-align:center;
    	background:#ccc;
    	color:#fff;
    	text-decoration:none;
    }
    #search-suggest .close:hover{
    	background:#9f1313;
    	text-decoration:none;
    	color:#fff;
    }
    </style>
    </div>
    Was this page helpful?
    Tag page
    Viewing 14 of 14 comments: view all
    Very very nice. This is going on my wiki pronto.
    Posted 06:47, 10 Sep 2009
    In the JQuery code, the API URL is hardwired to this wiki. Suggest changing it to use site.api. Also, doesn't the query need to be escaped before appending to the URL (qurl). I can't tell if it's working correctly with special characters.

    For completeness it might be nice to toss in a "search" button next to the input field.

    Finally, for maximum usefulness, I'd put this in a template and add a parameter that allows you to search only within a particular tree. This would make it a very very nice local search mechanism for an area of a wiki. This should only affect the construction of the qurl variable.
    Posted 07:07, 10 Sep 2009
    @neilw good suggestions, make sure to @howleyda so gets pinged about them (not sure if he's subscribed to the page)
    Posted 09:51, 10 Sep 2009
    This was out a whole day without me knowing! Things like this should always be announced in the forums. It also provides a good place for discussion about a new item. I'll provide the announcement for this one on Howleyda's behalf. edited 13:02, 10 Sep 2009
    Posted 12:59, 10 Sep 2009
    If this page had a star rater, I would give it 5. :)

    This kind of thing should be in the core product - it's too cool to have as an add-on only!
    Posted 14:07, 10 Sep 2009
    What am I doing wrong? Unfortunately this tool doesn't work when I put all code into the source view of a page.
    Posted 02:58, 11 Sep 2009
    @crb @rberinger @neilw @steveB - I didn't realize there was a discussion going on here, I forgot to subscribe to notifications. Anyway I have already built a second version that is a little more robust and delvers a balanced number of results including images. I am going to take the advice from these comments and try to release version2 sometime next week. I'll make sure it gets in the forum too.

    Blog post to compliment this feature: http://blog.developer.mindtouch.com/2009/09/11/writing-ajax-search-suggestions-using-the-mindtouch-api/

    Thanks for the support
    Damien
    Posted 12:33, 11 Sep 2009
    @azrael - is your MindTouch private?
    Posted 12:33, 11 Sep 2009
    @howleyda - you should attach the full downloadable to this page
    Posted 16:09, 11 Sep 2009
    @Howleyda: Yes, it is private.
    Posted 22:33, 13 Sep 2009
    is there any way to use this ajax search instead of the normal search box on the top right corner?
    Posted 00:18, 14 Sep 2009
    @azrael - Because we're using jquery to retrieve these search results the API has no way of knowing your authentication. Therefor if your MindTouch is private it will not return any results. I'm going to talk to @SteveB and @RobertM to see if this problem can be solved using the DekiAPI javascript library.
    Posted 09:18, 14 Sep 2009
    mine is private too, and it *does* work
    Posted 09:23, 14 Sep 2009
    There is an enhanced version setup to install as a standard template at http://developer.mindtouch.com/DekiScript/FAQ/How_do_I..._Create_a_Search_Suggestion_Tool_using_the_MindTouch_API%3F
    Posted 20:19, 29 Oct 2009
    Viewing 14 of 14 comments: view all
    You must login to post a comment.

    Copyright © 2011 MindTouch, Inc. Powered by