duanmianxue2687 2019-05-03 02:42
浏览 82

我想将此js自动完成代码替换为twitter typeahead

My search query has a comma. When I give it a space, its suggestion not work. So I decided to replace it with twitter typeahead. help me with code thank you or improve this code for better result ... I do not want a delay on a search query and multiple words should suggest with ignoring comma and space.

$(function () {
    $("#dept_city").autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: $('#rootAddress').val()+"Searchflights/Autocomplete",  //
                dataType: "json",
                data: {
                    term : request.term,
                    q : $("#dept_city").val()
                },
                success: function(data) {
                    if(!data.length) {
                        var result = [{
                            match: 'No matches found',
                            matchr: ''
                        }];
                       response(result.slice(0, 10));
                    } else {
                        response($.map(data, function (valu) {
                            return {
                                label:valu.text, //text
                                value: valu.text,
                                country:valu.lbl,
                                city:valu.city,
                                Airportcode:valu.Airportcode,
                                airport_city:valu.airport_city,
                                id: valu.id
                            };
                        }));
                    }
                }
            });
        },
        minLength: 2,
        select: function (event, ui)
        {
            $("#dept_city").val(ui.item.label);
            $("#dept_city_hidden").val(ui.item.id);
        },
        autoFocus: true,
        search: function(){$(this).addClass('loading');},
        open: function (result) {
            $(this).removeClass('loading');
            if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)) {
                $('.ui-autocomplete').off('menufocus hover mouseover');
            }
        },
        change: function(event, ui) {
            if (ui.item === null) {
                $("#dept_city").val("");
                $("#dept_city_hidden").val("");
            }
        }
    }).data("ui-autocomplete")._renderItem = function (ul, item) {

        if(!item.match) {
            return $("<li></li>").data("item.autocomplete", item)
            .append("<div class='dib col-md-10 col-sm-10 pad0  col-xs-10   textOverflow'><div class='mainTxt'> <span class='mainTxt_code'>" + item.country + "</span></div><span class='sr-auto-country-name'>" + item.airport_city + "</span></div><div class='col-md-2 col-sm-2 pad0  col-xs-2'><div class='fr width100'><span class='statusTag auto fr'>" + item.Airportcode + "</span></div></div>")
            .appendTo(ul);
        } else {
            return $("<li></li>")
            .data("item.autocomplete", item)
            .append("<div class='dib col-md-10 col-sm-10 pad0  col-xs-10   textOverflow'><div class='mainTxt'><span>" + item.match + "</span></div></div>")
            .appendTo(ul);
        }
    };
});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?