7

テキスト ボックス ( typeahead.js ) の 1 つで typeahead を使用しており、タブのオートコンプリートをオフにしようとしています。ドキュメントでこれを見つけることができませんでしたが、おそらく誰かがこれが可能かどうか/どのように可能かを知っていますか?

助言がありますか?

編集 :

プラグインに使用されるコード:

myTypeAhead = $('#txtTypeAhead').typeahead({
            name: 'TypeAhead',
            valueKey: "Value",
            remote: '/ServiceHandlers/myHandler.ashx?Method=Method&Query=%QUERY',
            template: ['<p>{{Value}}</p>'],
            engine: Hogan
        });


        companyCodeTypeAhead.on('typeahead:initialized', function (evt)
        {
            console.log('INIT ' + $(evt.target).val());
        });
        companyCodeTypeAhead.on('typeahead:opened', function (evt)
        {
            console.log('OPEN ' + $(evt.target).val());
        });
        companyCodeTypeAhead.on('typeahead:closed', function (evt)
        {
            console.log('CLOSE ' + $(evt.target).val());
        });
        companyCodeTypeAhead.on('typeahead:selected', function (evt, data)
        {
            console.log('SELECT data==>' + data.Key + ' - ' + data.Value); //selected datum object
        });
        companyCodeTypeAhead.on('typeahead:autocompleted', function (evt, data)
        {
            console.log('AUTOCOMPLETE data==>' + data.Key + ' - ' + data.Value); //selected datum object
        });
4

5 に答える 5

2

typeahead.js で、これを ctrl-f します。.on("tabKeyed leftKeyed rightKeyed", this._autocomplete);

tabKeyedキー イベント ハンドラのリストから削除します。

于 2013-05-29T19:20:19.830 に答える
2

In typeahead 0.11.1, which is the released version as of 12/9/15:

Locate the following function and comment out the autocomplete line as shown and add the next line, which simulates the escape key behavior causing the right events to be thrown, but needs to have $e.preventDefault(); to prevent the interception of the tab key by the browser bypassing typeahead's logic.

        _onTabKeyed: function onTabKeyed(type, $e) {
            var $selectable;
            if ($selectable = this.menu.getActiveSelectable()) {
                this.select($selectable) && $e.preventDefault();
            } else if ($selectable = this.menu.getTopSelectable()) {
//              this.autocomplete($selectable) && $e.preventDefault(); // <-- Comment out
                this.close() && $e.preventDefault();  // <--- Add this
            }
        },

Hopefully they will make that configurable in a forthcoming release.

于 2015-12-10T04:11:38.310 に答える
1

同じこと、オートコンプリートが必要でしたが、タブでリストの最初のオプションを選択しないようにしました。これは、実際のパッケージをハッキングしないと不可能であり、保守性が非常に低くなります。そこで、別のルートに進み、現在のクエリを最初の結果として返すカスタム データセットを追加しました...

var substringMatcher = function(strs) {
  return function findMatches(q, cb) {
    var matches, substrRegex;

    // an array that will be populated with substring matches
    matches = [];

    // regex used to determine if a string contains the substring `q`
    substrRegex = new RegExp(q, 'i');

    // iterate through the pool of strings and for any string that
    // contains the substring `q`, add it to the `matches` array
    $.each(strs, function(i, str) {
      if (substrRegex.test(str)) {
        // the typeahead jQuery plugin expects suggestions to a
        // JavaScript object, refer to typeahead docs for more info
        matches.push({
          value: str
        });
      }
    });

    cb(matches);
  };
};
var getLocalSource = function() {
  return function findMatches(query, callback) {
    return callback([{
      'data': query
    }]);
  }
};
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

var countries = ["Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"];

$('#the-basics .typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
}, {
  name: 'local-data',
  display: 'data',
  source: getLocalSource()
}, {
  name: 'states',
  displayKey: 'value',
  source: substringMatcher(states),
  templates: {
    header: '<div class="head">States</div>'
  }
}, {
  name: 'countries',
  displayKey: 'value',
  source: substringMatcher(countries),
  templates: {
    header: '<div class="head">Countries</div>'
  }
});
.tt-dataset .head {
  padding: 15px;
  font-weight: bold;
  background-color: lightgray;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
<link href="https://twitter.github.io/typeahead.js/css/examples.css" rel="stylesheet" />

<div id="the-basics">
  <input class="typeahead" type="text" placeholder="Locations">
</div>

于 2017-06-05T16:45:40.653 に答える
0

TAB での最初の項目選択を無効にするには、「typeahead.jquery.js」ファイルで、

関数「_onTabKeyed」を見つけて、関数を書き直します。$selectable でオートコンプリートする部分をコメント/削除し、以下に示すように空の文字列を関数 autocomplete() に渡します。

_onTabKeyed: function onTabKeyed(type, $e) {
    var $selectable;
    if ($selectable = this.menu.getActiveSelectable()) {
        this.select($selectable) && $e.preventDefault();
    } else if ($selectable = this.menu.getTopSelectable()) {
        // this.autocomplete($selectable) && $e.preventDefault();
        this.autocomplete('') && $e.preventDefault();
    }
}
于 2018-08-22T05:51:35.560 に答える