【问题标题】:Bootstrap typeahead suggestions replaced when navigation导航时替换引导输入提示
【发布时间】:2013-03-21 10:32:50
【问题描述】:

我正在使用 Bootstrap Typeahead 来建议 som 搜索结果。结果是从 ajax 资源返回的,由于该资源会造成延迟,因此我遇到了不幸的影响。

示例: 如果输入一个 4 个字母的单词,建议会出现在 2 个字母之后,然后我可以通过上/下键查看结果,但突然建议会重新加载,因为最后一个请求已经完成。

如果用户当前正在使用向上/向下键查看建议,是否有任何方法可以“取消”剩余的内容?

('#query').typeahead({
        items: 4,
        source: function (query,process) {

            map = {};
            $.getJSON('/app_dev.php/ajax/autosuggest/'+query, function (data) {
                vehicles = [];
                $.each(data, function(i,vehicle){
                    map[vehicle.full] = vehicle;
                    vehicles.push(vehicle.full);
                });
                process(vehicles);
            });
        },
        updater: function (item) {
            // do something here when item is selected
        },
        highlighter: function (item) {
            return item;
        },
        matcher: function (item) {
            return true;
        }
    });

【问题讨论】:

  • +1 个很好的问题,但你能否让你的标题更具描述性?
  • 什么版本的 Bootstrap?
  • @Skelly Bootstrap 2.3.0
  • 不是将整个数据源直接放在javascript上的选项吗?如果数据不是太大,您可以避免 ajax 超时。 tatiyants.com/…
  • @xr09 不幸的是没有

标签: ajax twitter-bootstrap bootstrap-typeahead


【解决方案1】:

认为以下内容将满足您的需求(很难准确复制):

没有简单的方法来中止延迟响应,但您可以扩展 typeahead as I figured out here(无需修改 bootstrap.js)

概念是捕捉keydown,检测事件是KEY_UP还是KEY_DOWN,设置标志is_browsing,如果is_browsing为真则中止process(即如果用户点击了KEY_UPKEY_DOWN 之后没有其他键)。

扩展预输入

// save the original function object
var _superTypeahead = $.fn.typeahead;

// add is_browsing as a new flag
$.extend( _superTypeahead.defaults, {
    is_browsing: false
});

// create a new constructor
var Typeahead = function(element, options) {
    _superTypeahead.Constructor.apply( this, arguments )
}

// extend prototype and add a _super function
Typeahead.prototype = $.extend({}, _superTypeahead.Constructor.prototype, {
    constructor: Typeahead

    , _super: function() {
        var args = $.makeArray(arguments)
        // call bootstrap core
        _superTypeahead.Constructor.prototype[args.shift()].apply(this, args)
    }

    //override typeahead original keydown
  , keydown: function (e) {
      this._super('keydown', e)
      this.options.is_browsing = ($.inArray(e.keyCode, [40,38])>-1)
    }

    //override process, abort if user is browsing
  , process: function (items) {
      if (this.options.is_browsing) return
      this._super('process', items)
    }

});

// override the old initialization with the new constructor
$.fn.typeahead = $.extend(function(option) {
    var args = $.makeArray(arguments),
    option = args.shift()

    // this is executed everytime element.modal() is called
    return this.each(function() {
        var $this = $(this)
        var data = $this.data('typeahead'),
            options = $.extend({}, _superTypeahead.defaults, $this.data(), typeof option == 'object' && option)

        if (!data) {
            $this.data('typeahead', (data = new Typeahead(this, options)))
        }
        if (typeof option == 'string') {
            data[option].apply( data, args )
        }
    });
}, $.fn.typeahead);

这个预先输入的扩展可以放在任何地方,例如<script type="text/javascript"> -section

测试扩展

<input type="text" id="test" name="test" placeholder="type some text" data-provide="typeahead">
<script type="text/javascript">
$(document).ready(function() {
    var url='typeahead.php';
    $("#test").typeahead({
        items : 10,
        source: function (query, process) {
            return $.get(url, {  query: query }, function (data) {
                return process(data.options);
            });
        }
    });
});
</script>

一个“服务器端”PHP 脚本,它返回大量带有强制延迟的随机选项,typeahead.php:

<?
header('Content-type: application/json');
$JSON='';
sleep(3); //delay execution in 3 secs
for ($count=0;$count<30000;$count++) {
    if ($JSON!='') $JSON.=',';
    //create random strings
    $s=str_shuffle("abcdefghijklmnopq");
    $JSON.='"'.$s.'"';
}
$JSON='{ "options": ['.$JSON.'] }';
echo $JSON;
?>

这似乎真的对我有用。但我不能确定它是否适用于你的情况。现在让我看看你成功与否。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-15
    • 2016-11-16
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    相关资源
    最近更新 更多