CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

angular jQuery UI autocomplete

Angular JS is one of the most developed MVC frameworks in the javascript world. Angular UI is a huge UI-centric extension of AngularJS (it is more or less like jQuery UI to the jQuery). It uses much jQuery UI and Twitter Bootstrap and provides many own components like modal dialogs, maps, tooltips, masked inputs and much more. And all this is easy to implement in your code just by adding a directive:

<input ui-date>

Much cleaner than listening on $(document).ready, traversing the DOM and appending the datepicker in your code:

<input id="myDate">

 

$(document).ready(function() {
   var input = $("#myDate");
   input.datepicker();
});

All this code is invoked but outside your app code.

ui-autocomplete

Unfortunately Angular UI has no official autocomplete directive. There is a discussion about it on many forums. There are some working implementations. But they are not complete. I looked at how ui-date is implemented. jQuery UI Datepicker and jQuery UI Autocomplete have one in common. They are relying on a set of options. To create a directive for each option would make the html code too dirty. Here is a directive ui-autocomplete which I have written that (like ui-date) takes a options object as parameters. This options object has to be appended to the $scope variable in the angular controller:

var uiAutocomplete = function() {
	return {
		require: '?ngModel',
		link: function(scope, element, attrs, controller) {
			var getOptions = function() {
				return angular.extend({}, scope.$eval(attrs.uiAutocomplete));
			};
			var initAutocompleteWidget = function () {
				var opts = getOptions();
				element.autocomplete(opts);
				if (opts._renderItem) {
					element.data("autocomplete")._renderItem = opts._renderItem;
				}
			};
			// Watch for changes to the directives options
			scope.$watch(getOptions, initAutocompleteWidget, true);
		}
	};
};
angular.module('ui.directives').directive('uiAutocomplete', [uiAutocomplete]);

Example code

<div ng-controller="AutoCompleteCtrl" class="ui-widget">
    <input placeholder="Search..." ng-model="term" ui-autocomplete="autocompleteOptions"/>
</div>

Here is the angular controller:

function AutoCompleteCtrl($scope, $http) {
	var search = function(request, response) {
			var callback = function(data) {
				response(data);
			};
			$http.get("/api/orders/search?q=" + $scope.term)
				.success(callback);
		},

		gotoOrderDetails = function (id) {
			window.location = "/orders/" + id;
		},

		_renderItem = function (ul, item) {
			return $("<li>")
				.data("item.autocomplete", item)
				.append("<a>" + item.title + "</a>")
				.appendTo(ul);
		},

		select = function (event, ui) {
			if (ui.item) {
				gotoOrderDetails(ui.item.orderId);
			}
		};

	$scope.autocompleteOptions = {
		minLength: 1,
		source: search,
		select: select,
		delay: 500,
		_renderItem: _renderItem
	};
}

To get the options inside the directive a function called $eval is used.

Here a standard set of jQuery UI Autocomplete is used and one more: _renderItem. It is not included in the default autocomplete options, but in my case I needed to define custom rendering logic for my items.

Enjoy

Edit 2013-02-15:

I have sent a pull request angular ui. See the details here

Edit 2013-02-23:

I have added support for uiConfig and change scope.$watch invokation based on Pull Request Feedback. I also published a small gist which shows how a simple ui-autocomplete can be used.

Advertisements

4 responses to “angular jQuery UI autocomplete

  1. Gabriel Svennerberg 2013-05-15 at 11:21

    I was trying out your code and didn’t get it to work at first. I got an error where you overwrite jQuery UI’s _renderItem method. The fix was easy though. Just replace the string “autocomplete” to “ui-autocomplete”. See code below.

    element.data("ui-autocomplete")._renderItem = opts._renderItem;

  2. Cem Çapulkolig 2013-06-27 at 21:33

    element.data(“autocomplete”) ===> needs to be changed to element.data(“ui-autocomplete”) for this to work in new Jquery UI

  3. Sergio Giusti 2016-03-16 at 14:51

    I am trying to create a lookup column in one list that lookups data from another list in the same site, the lookup list has around 4000 items and the dropdown that appears is terrible, can I use your solution to solve this on the newitem.aspx form? if so, could you provide some instructions please.

    Hi, I’m a complete idiot when it comes to this stuff, I have a list called ‘Staff List’, it has a column called ‘Employee ID’. I have a document library called ‘eFiling’, it has a Lookup column called ‘Employee ID’ which is set to lookup the employee id field from the staff list. If I add a script editor to the newitem form how do I format your code so that it will work?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Discovering SharePoint

And going crazy doing it

Bram de Jager talking Office 365, SharePoint and Azure

My view and thoughts on Productivity and more.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

RealActivity - Real-time and trustworthy

Blog site of founder, RealActivty - Paul J. Swider

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points!!

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode, ya mama

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint

blksthl

Mostly what I know about SharePoint - CommunicoCuspis

%d bloggers like this: