views:

10

answers:

1

Hey Members,

I need two questions answer if possible:

  1. How to set the key\value within the jQuery Autocomplete control.
  2. Retrieve the selected value from the jQuery Autocomplete control once a user selects a school name.

Thank in advance for your help.

<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>

function RetrieveSchoolsBasedOnSchoolTypeSelected() {
      //Item Selected Value
      var ItemSelectedValue = $("#selSchoolTypes: option[selected]").val();
      $("#example").val("");
      $.getJSON("http://devportal2/apps/parisinterndb/_vti_bin/ListData.svc/Schools?$filter=SchoolTypeId eq " + ItemSelectedValue + "", function(DataResults) {

          var count = 0;
          var resultDataItems = "";
          $.each(DataResults.d.results, function(i, result) {
              var title = result.Title;
              resultDataItems +=  title +",";
          });
          resultDataItems += "";
          var data = resultDataItems.split(',');
          $("#example").autocomplete(data,
                                    { delay: 10,
                                        minChars: 1,
                                        cacheLength: 10,
                                        autoFill: true
                                    });
      });
           $("#example").result(findValueCallback).next().click(function() {
            $(this).prev().search();
          });

      }

      function findValueCallback(event, data, formatted) {
          alert(formatted+" "+data);
      }
A: 

for getting the selected value,just parse the data paramter in the findValueCallback function.You may need to parse the "data" using split function and all.

    ex : if (data != null) {
                   var model = "";
                   model = data.toString().split(".")[1];
                   selectedItem= data.toString().split(".")[0];
      }

For setting the key value pair in the autosuggest dropdown,u can use the autocomplete function with a server page which can load data,

               $("#txtSearchKey").autocomplete("Lib/ajaxpages/GetModelOptions.aspx", {
               minChars: 2,
               width: 550,
               max: 4,
               highlight: false,
               scroll: true,
               scrollHeight: 300,
               formatItem: function(data, i, n, value) {
                   return "<b>" + value.split(".")[0] + "</b>";
               },
               formatResult: function(data, value) {
                   return value.split(".")[0];

               }
           });

the GetModelOptions.aspx can retun data in the form a string like 1.Alaska \n 2.Mexico \n 3.Michigan \n

and in the javascript u extract it

Shyju
Doesn't work, when I try to set the key\value pair it doesn't work. I'm using SharePoint 2010 ListData.svc that returns data in a xml format. When I try to set the values in formatItem and formatResult, I get a 'undefined' is null.
Brandon Michael Hunter