dtvnnhh8992 2015-04-13 10:52
浏览 91
已采纳

typeahead.js远程返回任何内容

I can't seem to get the remote function of typeahead.js working. I'm posting the code to start:

$(document).ready(function() {
    var locations = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: baseUrl + 'restaurants/fetchZIP/%QUERY'
    });

    $('#location').typeahead({
        hint: true,
        highlight: true,
        minLength: 1,
        source: locations.ttAdapter()
    });
    /*$('#location').keyup(function() {
        $.ajax({
            method: 'POST',
            dataType: 'json',
            url: baseUrl + 'restaurants/fetchZIP/',
            data: $('#add_restaurant').serialize(),
            success: function(data) {
                console.log(data);
            }
        });
    });*/
});

This is the file, where I do the Bloodhound remote thing and I use typeahead on the desired input field. The commented section was a test, to make sure if my DB statement wasn't faulty. I had to rewrite the following code a bit to make the test, but it's basically just a switch from GET to POST:

public function fetchZIP($query)
{
    $cantons = DataLoc::find(array('zip LIKE' => '%'.$query));
    echo json_encode($cantons);
}

This is the action for the 'restaurants/fetchZIP/' page (it's written in CodeIgniter). So, I don't really know what's happening, since I cannot console.log() within the typeahead() function, so I hope anyone can lead me back to the right way.

  • 写回答

1条回答 默认 最新

  • douji3623 2015-04-14 04:06
    关注

    Firstly, you have to use initialize bloodhound by locations.initialize() right after defining it.

    This is how a typeahead is initialized

    typeahead(options, [*datasets])

    You included source(or datasets) into the options so try something like this

    $('#location').typeahead({
        hint: true,
        highlight: true,
        minLength: 1
    },{
        source: locations.ttAdapter()
    });
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条