Search This Blog

Thursday, 31 May 2012

AutoComplete TextBox using JQuery in SharePoint 2010


Hi All, 

Recently I had a situation to give an Auto Complete TextBox control in my application (Visual Web part) using JQuery. I have done this by refereeing the below link

Link: Reference
 
The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.

The datasource is a simple JavaScript array, provided to the widget using the source-option.

Now get works the JQuery in SharePoint 2010 Visual webpart 

I have added two js files and one css file in my solution to work the JQuery and give proper style to the textbox and menu items. You can download the js files and css file from the below link




Then, I written the script and get the array of keywords (Programming languages) and assigned to the source option. Refer the below code snippet:

 <script src="/_layouts/1033/STYLES/jqueryCustom-1.7.1.min.js" type="text/javascript"></script>
<script src="/_layouts/1033/STYLES/jqueryCustom-ui-1.18.min.js" type="text/javascript"></script>
<SharePoint:CssRegistration ID="cssReg" runat="server" Name="/_layouts/1033/STYLES/jquery-uiCustom.css"> </SharePoint:CssRegistration>
  <script type="text/javascript">
      Sys.Application.add_load(wireEvents);

      function wireEvents()
      {
          $(function ()
          {
              var availableTags = ["ActionScript","AppleScript","Asp","BASIC","C",
                                   "C++","Clojure","COBOL","ColdFusion","Erlang",
                                  "Fortran","Groovy","Haskell","Java","JavaScript",
                                   "Lisp","Perl","PHP","Python","Ruby","Scala",                                  
                                   "Scheme"];
              function split(val)
              {
                  return val.split(/,/);
              }

              function extractLast(term)
              {
                  var nonSpaceTerm = split(term).pop().toString();
                  if (/^\s+\S*/.test(nonSpaceTerm))
                  {
                      nonSpaceTerm = nonSpaceTerm.replace(/\s+/, "");
                  }
                  return split(nonSpaceTerm).pop();
              }

              $("input[name*='txtKeyword']")
              // don't navigate away from the field on tab when selecting an item
                     .bind("keydown", function (event)
            {
                         if (event.keyCode === $.ui.keyCode.TAB &&
                                         $(this).data("autocomplete").menu.active) {
                             event.preventDefault();
                         }
                     })
                     .autocomplete(
            {
                         minLength: 0,
                         source: function (request, response)
                {
                             // delegate back to autocomplete, but extract the last term
                             response($.ui.autocomplete.filter(
                                         availableTags, extractLast(request.term)));
                         },
                         focus: function ()
                {
                             // prevent value inserted on focus
                             return false;
                         },
                         select: function (event, ui)
                {
                             var terms = split(this.value);
                             // remove the current input
                             terms.pop();
                             // add the selected item
                             terms.push(ui.item.value);
                             // add placeholder to get the comma-and-space at the end
                             terms.push("");
                             this.value = terms.join(",");
                             this.value += " ";
                             return false;
                         }
                     });
          });
      }           
        </script>

Then added a asp:TextBox control
<asp:TextBox ID="txtKeyword" runat="server"></asp:TextBox>

Done, now deployed and added the webpart in my application.

Output:
























I will post how to get the source items from SharePoint2010 list in my next post.

Cheers....................................

6 comments:

  1. hi,
    You have explained about static data.how to fill data from sql server in auto complete Textbox.
    Please send me the source code.Its really needed.

    My mail Id-k.manjari21@gmail.com

    ReplyDelete
    Replies
    1. Hi Manjari,

      I have explained, how to deal Auto complete textbox along with SharePoint List in the below post, Please refer that.

      http://sharepointwings.blogspot.com/2012/05/autocomplete-textbox-using-jquery-in_31.html

      Delete
    2. hi,
      I saw the link,but i m bit confused while fetching the data from sql and display in a visual webpart.

      can u please give me the source code.it will really helpful for me.i am struggling from last 4 days for this only.

      waiting for ur reply.

      Delete
  2. Manjari/Manikandan, are you guys able to do this? I m comparing this vs Ajax auto complete..

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete