Tip: Click lines to highlight, hold ctrl/cmd to multi-select

JQuery Autocomplete (parse method) JSON Object (1-Feb @ 15:20)

brentg

Syntax Highlighted Code

  1. <script>
  2.        $(function() {
  3.                $.ajaxSetup ({ cache: false });
  4.                var ajaxLoadingImg = '<img
  5. src='${Url.Content("/content")}/img/ui/icons/spinner1.gif' />';
  6.  
  7.                $("#select1").attr("disabled", "disabled");
  8.  
  9.                //Multiple select option cloning
  10.                $('#add').click(function() {
  11.                        return !$('#select1 option:selected').remove().appendTo('#select2');
  12.                });
  13.                $('#remove').click(function() {
  14.                        return !$('#select2 option:selected').remove().appendTo('#select1');
  15.                });
  16.  
  17.  
  18.                //Autocomplete
  19.                $("#instrument").autocomplete("/RegulatorSet/InstrumentSearchAutocomplete", {
  20.                        delay: 150,                             // millisecond delay after keyup/down event
  21.                        minChars: 2,                            // min no of chars to activate
  22.                        mustMatch: false,
  23.                        max: 100,                               // max no of items
  24.                        dataType: 'json',
  25.                        parse: function(json) {
  26.                                var resultList = new Array();
  27.  
  28.                                $("#instrument").after('<span class="ajax-load">' + ajaxLoadingImg
  29. + 'Loading Insrument Group' + '</span>');
  30.                                var optionsHtml = '';
  31.                                for(var i=0; i<json.length; i++){
  32.                                        optionsHtml += '<option value="' + json[i].optionValue + '">' +
  33. json[i].optionDisplay + '</option>';
  34.                                }
  35.                                $("#select1").removeAttr("disabled");
  36.                                $("span.ajax-load").remove();
  37.                                $("#select1").html(optionsHtml);
  38.  
  39.                                return resultList;
  40.                        },
  41.                        formatItem: function(resultList){
  42.                                // No ul required
  43.                        }
  44.                });
  45.  
  46.        });
  47. </script>

Plain Code

<script>
       $(function() {
               $.ajaxSetup ({ cache: false });
               var ajaxLoadingImg = '<img
src='${Url.Content("/content")}/img/ui/icons/spinner1.gif' />';

               $("#select1").attr("disabled", "disabled");

               //Multiple select option cloning
               $('#add').click(function() {
                       return !$('#select1 option:selected').remove().appendTo('#select2');
               });
               $('#remove').click(function() {
                       return !$('#select2 option:selected').remove().appendTo('#select1');
               });


               //Autocomplete
               $("#instrument").autocomplete("/RegulatorSet/InstrumentSearchAutocomplete", {
                       delay: 150,                             // millisecond delay after keyup/down event
                       minChars: 2,                            // min no of chars to activate
                       mustMatch: false,
                       max: 100,                               // max no of items
                       dataType: 'json',
                       parse: function(json) {
                               var resultList = new Array();

                               $("#instrument").after('<span class="ajax-load">' + ajaxLoadingImg
+ 'Loading Insrument Group' + '</span>');
                               var optionsHtml = '';
                               for(var i=0; i<json.length; i++){
                                       optionsHtml += '<option value="' + json[i].optionValue + '">' +
json[i].optionDisplay + '</option>';
                               }
                               $("#select1").removeAttr("disabled");
                               $("span.ajax-load").remove();
                               $("#select1").html(optionsHtml);

                               return resultList;
                       },
                       formatItem: function(resultList){
                               // No ul required
                       }
               });

       });
</script>

Codedump Run

Permalink: http://codedumper.com/jquery-autocomplete-parse-method-json-object