DCSIMG
<-- +1 script --> May 2011 - Posts - .Net && Beyond

.Net && Beyond

Taking about .NET and much more

May 2011 - Posts

Search Facebook Friends With Jquery Autocomplete

Just made a small autocomplete textbox that search a user friends from Facebook.

it is a pretty simple implementation that allow the user to search for his friends. In order to use it you will need to get a valid access_token from Facebook (but you can read more in how to use the Facebook graph API at http://developers.facebook.com/docs/reference/api/ – or get a sample one).

There are a few tricks in the implantation

 

So here is the code

   1: <script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
   2: <script src="../../Scripts/jquery-ui.min.js" type="text/javascript"></script>
   3: <script type="text/javascript" src="../../Scripts/jquery.autocomplete.min.js"></script> 
   4: <script>
   5:  
   6:     //Get the user image from the user id
   7:     function getImage(id) {
   8:         return "http://graph.facebook.com/" + id + "/picture?type=square";
   9:     }
  10:  
  11:     var accessToken = "PUT A VALID ACCESS TOKEN HERE INORDER TO MAKE IT WORK";
  12:     $(document).ready(function () {
  13:         $("#example").autocomplete("https://graph.facebook.com/me/friends?access_token=" + accessToken + "&callback=?", {
  14:         
  15:             width: 250,
  16:             height: 400,
  17:             max: 8,
  18:             dataType: 'jsonp',
  19:             cacheLength: 10,
  20:             minChars: 1,
  21:             parse: function (data) {
  22:                 var rows = new Array();
  23:                 data = data.data;
  24:                 for (var i = 0; i < data.length; i++) {
  25:                     rows[i] = { data: data[i], value: data[i].name, result: data[i].name };
  26:                 }
  27:                 return rows;
  28:             },
  29:             formatItem: function (data, i, n, value, text, a, b, c, d) {
  30:                 var x = getImage(data.id);
  31:                 return "<div class='test2'><img class='test' width='32px' height='32px' src='" + x + "'></img><span>" + data.name + "</span></div>";
  32:             },
  33:             
  34:         }
  35:           ).result(function (evnet, item) {
  36:               alert(item.id);       
  37:           });
  38:     });
  39: </script>
  40: Write a friend name
  41: <input id="example" />

The final result will look something like that:

image

All you need to do now is change the style so it will fit in your site

You can read more about jquery autocomplete plugin here: http://docs.jquery.com/Plugins/Autocomplete/autocomplete 

 

Update: There is an update to this post here

Keep Writing, Compiling, and Debugging

Alon Nativ

The King is dead. Long live the King

A few weeks ago redgate announced that the lovely .NET reflector  is going to cost money, that was a very sad news for every .NET developer out there.

But the good news are that a few days after JetBrains announced that they are releasing there own reflector called dotPeek. I personally hate the name but It is a great tool!

Much faster great UI and the best thing it has all of the ReSharper shortcuts!

So even if you decided to pay for the .NET reflector I think that you should throw it away and start using dotPeek

The king (.NET reflector) is dead. Long live the King (dotPeek)!

 

Keep Writing, Compiling, and Debugging.

Alon Nativ