简体   繁体   English

如何停止以排序顺序显示元素的 Bootstrap-3-Typeahead 下拉列表

[英]how to stop Bootstrap-3-Typeahead dropdown displaying the elements in the sorted order

I'm using Bootstrap-3-Typeahead我正在使用Bootstrap-3-Typeahead

Is there a way to avoid the dropdown data getting sorted in natural order ?有没有办法避免下拉数据按自然顺序排序?

This will show all the data in the source in the drop down list in the onFocus event.这将在 onFocus 事件的下拉列表中显示源中的所有数据。 So the problem is it will only show limited number of elements with the natural sorted order though I don't want them to be.所以问题是它只会显示有限数量的具有自然排序顺序的元素,尽管我不希望它们如此。

How can I avoid them getting sorted.我怎样才能避免它们被排序。

$('#device').tagsinput({
      typeahead: {
        source: ['Amsterdam', 'Washington', 'Sydney', 'Beijing', 'Cairo','Amstedrdam', 'Washidngton', 'Syddney', 'Bedijing', 'Caidro','Amzstderdam', 'Washidnzgton', 'Sydzdney', 'Beijzding', 'Caidzro'],
        autoSelect: false,
        showHintOnFocus : true,
        minLength : 0
      }
});

The problem is not in the Typeahead plug-in , but in the second bootstrap-tagsinput plug-in.问题不在于Typeahead插件,而在于第二个bootstrap-tagsinput插件。 Unfortunately you can not solve it without editing the source.不幸的是,如果不编辑源代码,您将无法解决它。

Take a look to this JSFiddle看看这个JSFiddle

How you can read from source of the bootstrap-tagsinput.js at line 310:如何从第 310 行的bootstrap-tagsinput.js 的源代码中读取:

sorter: function (texts) {
            return texts.sort();
          },

The sorter option is provided by the bootstrap-tagsinput plug-in to the Typeahead plug-in and since the plug-in is executed in a immediately invoked function expression you can't overwrite the sorter method.sorter选项由提供bootstrap-tagsinput插件到Typeahead的插件并且由于插件是在立即调用函数表达式执行无法覆盖的sorter方法。 You should fork the plug-in and change the sorter option:您应该分叉插件并更改排序器选项:

sorter: function (texts) {

          var sorter = typeahead.sorter || function (items) { return items.sort(); }
          return sorter(texts);
        },

在此处输入图片说明

So your plugin initialization will be:所以你的插件初始化将是:

$('#ppp').tagsinput({
    typeahead: {
      source: ['Amsterdam', 'Washington', 'Sydney', 'Beijing', 'Cairo', 'Amstedrdam', 'Washidngton', 'Syddney', 'Bedijing', 'Caidro', 'Amzstderdam', 'Washidnzgton', 'Sydzdney', 'Beijzding', 'Caidzro'],
      sorter: function (texts) {
        return texts;
      }
    }
  });

...once you have done this then it will work ...一旦你这样做了,它就会起作用

The library provides a way to override the sorter.该库提供了一种覆盖排序器的方法。 You could simply use that to override the default sorter and return the list as-is:您可以简单地使用它来覆盖默认排序器并按原样返回列表:

sorter: function (items) { 
           return items; 
        },

Also, here is the default sorter implementation.此外, 这里是默认的排序器实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM