2016-12-21 19 views
1

私は必要な機能をすべて備えた既存のjqueryオートコンプリートコードをいくつか持っています。オートコンプリートでは、最初の文字で始まる一致をコンボボックスで検索します。コンボボックスで複数の選択肢があるJqueryオートコンプリート

jqueryui.com/autocomplete/#multipleここ

は私のコードです::私がする必要があるのは、次のような複数選択機能を追加で

<html> 
 
<head> 
 
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
<meta name="HandheldFriendly" content="True"> 
 
<meta name="PalmComputingPlatform" content="True"> 
 
<meta http-equiv=Content-Type content="text/html; charset=windows-1252"> 
 
<title>WebFOCUS Report</title> 
 
</head> 
 
<body> 
 
<br /> 
 
<br /> 
 
<br /> 
 
<div> 
 
Please choose an option : 
 
<select size="3" id="decision2"> 
 
<option selected>1521</option> 
 
<option>3336</option> 
 
<option>5454</option> 
 
<option>7890</option> 
 
<option>2178</option> 
 
</select> 
 
</div> 
 

 
<style> 
 
    .custom-combobox { 
 
    position: relative; 
 
    display: inline-block; 
 
    } 
 
    .custom-combobox-toggle { 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    margin-left: -1px; 
 
    padding: 0; 
 
    } 
 
    .custom-combobox-input { 
 
    margin: 0; 
 
    padding: 5px 10px; 
 
    } 
 
    </style> 
 
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
    <script> 
 
    $(function() { 
 
    $.widget("custom.combobox", { 
 
     _create: function() { 
 
     this.wrapper = $("<span>") 
 
      .addClass("custom-combobox") 
 
      .insertAfter(this.element); 
 
    
 
     this.element.hide(); 
 
     this._createAutocomplete(); 
 
     this._createShowAllButton(); 
 
     }, 
 
    
 
     _createAutocomplete: function() { 
 
     var selected = this.element.children(":selected"), 
 
      value = selected.val() ? selected.text() : ""; 
 
    
 
     this.input = $("<input>") 
 
      .appendTo(this.wrapper) 
 
      .val(value) 
 
      .attr("title", "") 
 
      .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left") 
 
      .autocomplete({ 
 
      delay: 0, 
 
      minLength: 0, 
 
      source: $.proxy(this, "_source") 
 
      }) 
 
      .tooltip({ 
 
      classes: { 
 
       "ui-tooltip": "ui-state-highlight" 
 
      } 
 
      }); 
 
    
 
     this._on(this.input, { 
 
      autocompleteselect: function(event, ui) { 
 
      ui.item.option.selected = true; 
 
      this._trigger("select", event, { 
 
       item: ui.item.option 
 
      }); 
 
      }, 
 
    
 
      autocompletechange: "_removeIfInvalid" 
 
     }); 
 
     }, 
 
    
 
     _createShowAllButton: function() { 
 
     var input = this.input, 
 
      wasOpen = false; 
 
    
 
     $("<a>") 
 
      .attr("tabIndex", -1) 
 
      .attr("title", "Show All Items") 
 
      .tooltip() 
 
      .appendTo(this.wrapper) 
 
      .button({ 
 
      icons: { 
 
       primary: "ui-icon-triangle-1-s" 
 
      }, 
 
      text: false 
 
      }) 
 
      .removeClass("ui-corner-all") 
 
      .addClass("custom-combobox-toggle ui-corner-right") 
 
      .on("mousedown", function() { 
 
      wasOpen = input.autocomplete("widget").is(":visible"); 
 
      }) 
 
      .on("click", function() { 
 
      input.trigger("focus"); 
 
    
 
      // Close if already visible 
 
      if (wasOpen) { 
 
       return; 
 
      } 
 
    
 
      // Pass empty string as value to search for, displaying all results 
 
      input.autocomplete("search", ""); 
 
      }); 
 
     }, 
 
    
 
     _source: function(request, response) { 
 
     var matcher = new RegExp("\\b" + $.ui.autocomplete.escapeRegex(request.term), "i"); 
 
     response(this.element.children("option").map(function() { 
 
      var text = $(this).text(); 
 
      if (this.value && (!request.term || matcher.test(text))) 
 
      return { 
 
       label: text, 
 
       value: text, 
 
       option: this 
 
      }; 
 
     })); 
 
     }, 
 
    
 
     _removeIfInvalid: function(event, ui) { 
 
    
 
     // Selected an item, nothing to do 
 
     if (ui.item) { 
 
      return; 
 
     } 
 
    
 
     // Search for a match (case-insensitive) 
 
     var value = this.input.val(), 
 
      valueLowerCase = value.toLowerCase(), 
 
      valid = false; 
 
     this.element.children("option").each(function() { 
 
      if ($(this).text().toLowerCase() === valueLowerCase) { 
 
      this.selected = valid = true; 
 
      return false; 
 
      } 
 
     }); 
 
    
 
     // Found a match, nothing to do 
 
     if (valid) { 
 
      return; 
 
     } 
 
    
 
     // Remove invalid value 
 
     this.input 
 
      .val("") 
 
      .attr("title", value + " didn't match any item") 
 
      .tooltip("open"); 
 
     this.element.val(""); 
 
     this._delay(function() { 
 
      this.input.tooltip("close").attr("title", ""); 
 
     }, 2500); 
 
     this.input.autocomplete("instance").term = ""; 
 
     }, 
 
    
 
     _destroy: function() { 
 
     this.wrapper.remove(); 
 
     this.element.show(); 
 
     } 
 
    }); 
 
    
 
    $("#decision2").combobox(); 
 
    $("#decision2").on("click", function() { 
 
     $("#decision2").toggle(); 
 
    }); 
 
    }); 
 
    </script> 
 
</body> 
 
</html>

+0

ようこそ!最初に[ツアー](http://stackoverflow.com/tour)にアクセスし、[よくある質問をする方法](http://stackoverflow.com/help/how-to-ask)を学んで[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例を参照してください。あなたのコードを提供する、我々はgithubでそれを検索するための十分な時間がありません。私たちがあなたを助けてくれるのは簡単です。 –

+0

ありがとう@AlexandreT – dmoses

答えて

0

コードを変更しました。確認して、それがあなたが望んでいたかどうかを教えてください。スタックオーバーフローへ

<html> 
 

 
<head> 
 
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
    <meta name="HandheldFriendly" content="True"> 
 
    <meta name="PalmComputingPlatform" content="True"> 
 
    <meta http-equiv=Content-Type content="text/html; charset=windows-1252"> 
 
    <title>WebFOCUS Report</title> 
 
</head> 
 

 
<body> 
 
    <br /> 
 
    <br /> 
 
    <br /> 
 
    <div> 
 
     Please choose an option : 
 
     <select size="3" id="decision2"> 
 
     <option selected>1521</option> 
 
     <option>3336</option> 
 
     <option>5454</option> 
 
     <option>7890</option> 
 
     <option>2178</option> 
 
    </select> 
 
    </div> 
 

 
    <style> 
 
     .custom-combobox { 
 
      position: relative; 
 
      display: inline-block; 
 
     } 
 
     
 
     .custom-combobox-toggle { 
 
      position: absolute; 
 
      top: 0; 
 
      bottom: 0; 
 
      margin-left: -1px; 
 
      padding: 0; 
 
     } 
 
     
 
     .custom-combobox-input { 
 
      margin: 0; 
 
      padding: 5px 10px; 
 
     } 
 
    </style> 
 
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
    <script> 
 
     $(function() { 
 
      $.widget("custom.combobox", { 
 
       _create: function() { 
 
        this.wrapper = $("<span>") 
 
         .addClass("custom-combobox") 
 
         .insertAfter(this.element); 
 

 
        this.element.hide(); 
 
        this._createAutocomplete(); 
 
        this._createShowAllButton(); 
 
       }, 
 

 
       _createAutocomplete: function() { 
 
        var sourceArray = []; 
 
        var selectItems = this.element.context.children; 
 
        $.each(selectItems, function(i, item) { 
 
         sourceArray.push(item.value); 
 
        }); 
 

 
        var selected = this.element.children(":selected"), 
 
         value = selected.val() ? selected.text() : ""; 
 

 
        function split(val) { 
 
         return val.split(/,\s*/); 
 
        } 
 

 
        function extractLast(term) { 
 
         return split(term).pop(); 
 
        } 
 

 
        this.input = $("<input>") 
 
         .appendTo(this.wrapper) 
 
         .val(value) 
 
         .attr("title", "") 
 
         .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left") 
 
         .autocomplete({ 
 
          delay: 0, 
 
          minLength: 0, 
 
          source: function(request, response) { 
 
           // delegate back to autocomplete, but extract the last term 
 
           var term = extractLast(request.term), 
 
            matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(term), "i"); 
 
           response($.grep(sourceArray, function(item) { 
 
            return matcher.test(item); 
 
           })); 
 
          }, 
 
          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(", "); 
 
           return false; 
 
          } 
 
         }) 
 
         .tooltip({ 
 
          classes: { 
 
           "ui-tooltip": "ui-state-highlight" 
 
          } 
 
         }); 
 
       }, 
 

 
       _createShowAllButton: function() { 
 
        var input = this.input, 
 
         wasOpen = false; 
 

 
        $("<a>") 
 
         .attr("tabIndex", -1) 
 
         .attr("title", "Show All Items") 
 
         .tooltip() 
 
         .appendTo(this.wrapper) 
 
         .button({ 
 
          icons: { 
 
           primary: "ui-icon-triangle-1-s" 
 
          }, 
 
          text: false 
 
         }) 
 
         .removeClass("ui-corner-all") 
 
         .addClass("custom-combobox-toggle ui-corner-right") 
 
         .on("mousedown", function() { 
 
          wasOpen = input.autocomplete("widget").is(":visible"); 
 
         }) 
 
         .on("click", function() { 
 
          input.trigger("focus"); 
 

 
          // Close if already visible 
 
          if (wasOpen) { 
 
           return; 
 
          } 
 

 
          // Pass empty string as value to search for, displaying all results 
 
          input.autocomplete("search", ""); 
 
         }); 
 
       }, 
 

 
       _removeIfInvalid: function(event, ui) { 
 

 
        // Selected an item, nothing to do 
 
        if (ui.item) { 
 
         return; 
 
        } 
 

 
        // Search for a match (case-insensitive) 
 
        var value = this.input.val(), 
 
         valueLowerCase = value.toLowerCase(), 
 
         valid = false; 
 
        this.element.children("option").each(function() { 
 
         if ($(this).text().toLowerCase() === valueLowerCase) { 
 
          this.selected = valid = true; 
 
          return false; 
 
         } 
 
        }); 
 

 
        // Found a match, nothing to do 
 
        if (valid) { 
 
         return; 
 
        } 
 

 
        // Remove invalid value 
 
        this.input 
 
         .val("") 
 
         .attr("title", value + " didn't match any item") 
 
         .tooltip("open"); 
 
        this.element.val(""); 
 
        this._delay(function() { 
 
         this.input.tooltip("close").attr("title", ""); 
 
        }, 2500); 
 
        this.input.autocomplete("instance").term = ""; 
 
       }, 
 

 
       _destroy: function() { 
 
        this.wrapper.remove(); 
 
        this.element.show(); 
 
       } 
 
      }); 
 

 
      $("#decision2").combobox(); 
 
      $("#decision2").on("click", function() { 
 
       $("#decision2").toggle(); 
 
      }); 
 
     }); 
 
    </script> 
 
</body> 
 

 
</html>

+0

Aminurありがとうございます。これを変更する方法はありますか?提案には、containsとは対照的に最初の入力文字から始まるものだけが表示されますか?たとえば、 '2'と入力すると、 '2178'と '1521'の代わりに '2178'が表示されます。 – dmoses

+0

新しい回答が編集されました。チェックしてください。 –

+0

すごい完璧! – dmoses

0

のリンクをお試しくださいhttps://jqueryui.com/resources/demos/autocomplete/multiple.html、あなたの質問に正しい解決策があります。

+0

これは正しい機能を持っていますが、これを実装する唯一の問題は、あなたが与えたサンプルのソースが配列から来て、私のコードではHTMLのリストボックスから来るということです。 – dmoses

関連する問題