weixin_33693070 2017-09-04 07:30 采纳率: 0%
浏览 32

如何提前完成Ajax

I am newbie in Typeahead. Now I need to change typeahead from static way to ajax way. The code is like,

var countries2 = [{ labelPost: "AUSTRALIAN NATIONAL UNIVERSITY ACT 200", valuePost: 200 },
   { labelPost: "DARWIN NT  800", valuePost: 800 }, { labelPost: "DARWIN NT  801", valuePost: 801 }, { labelPost: "WAGAIT BEACH NT  803", valuePost: 803 }, 
   { labelPost: "PARAP NT  804", valuePost: 804 }, { labelPost: "ALAWA NT  810", valuePost: 810 }, { labelPost: "BRINKIN NT  810", valuePost: 810 }, { labelPost: "CASUARINA NT  810", valuePost: 810 }];

$('#txtPostcode').typeahead({
                name: 'Postcode',
                displayText: function (item) { return item.labelPost; },
                items: 10,
                source: countries2,
                updater: function (item) {
                    $('#txtPost').val(item.valuePost);
                    return item.labelPost;
                }
            });

Then I output the array to a json file "city.json" and put it under the project folder where I can visit by openning localhost/city.json, then I tried the code like.

   $('#txtPostcode').typeahead({
            name: 'Postcode',
            displayText: function (item) { return item.city; },
            items: 10,
            source: function (query, process) {
                var parameter = { query: query };
                $.get('city.json', parameter, function (data) {
                    process(data);
                });

It doesn't work, and did not throw any errors.

And then I tried this way.

    $('#txtPostcode').typeahead({
            name: 'Postcode',
            displayText: function (item) { return item.labelPost; },
            items: 10,
            source: {
                ajax: {
                    url: "/city.json",
                }
            },
            updater: function (item) {
                $('#txtPost').val(item.valuePost);
                return item.labelPost;
            }
        });

But same result.

Could somebody help me with this?

</div>
  • 写回答

1条回答 默认 最新

  • 喵-见缝插针 2017-09-04 09:11
    关注

    You can use BloodHound for make this. You must be insert function inside the Ajax request. An example working below:

    var cities = [];
    var firstnames = [];
    
    $.ajax({
      url: "your.json", // load your Json
      cache: false,
      dataType: "json",
      success: function(data) {
        $.each(data, function(i, field){ // create a table with your data
            cities.push(field);
        });
        var firstcitynames = new Bloodhound({ // use Bloodhound for create maping of your data
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            local: $.map(cities, function(cityName) { return { value: cityName }; }),
            limit:50
        });
    
        firstcitynames.initialize(); 
    
        $("#scrollable-dropdown-menu .typeahead").typeahead({
            hint: false,
            highlight: true,
            minLength: 3
        },
        {
            name: "firstcitynames",
            displayKey: "value",
            source: firstcitynames.ttAdapter()
        }).bind("typeahead:selected", function(obj, datum, name) {
            // here action after select choice
        });
      }
    });
    

    UPDATE

    Replace your each function:

    var cities = [];
    var firstnames = [];
    
    $.ajax({
      url: "http://vocab.nic.in/rest.php/country/json", 
      cache: false,
      dataType: "json",
      success: function (data) {
        $.each(data.countries, function (i, field) { 
          cities.push(field.country.country_name);
        });
    
        var firstcitynames = new Bloodhound({ // use Bloodhound for create maping of your data
          datumTokenizer: Bloodhound.tokenizers.obj.whitespace("value"),
          queryTokenizer: Bloodhound.tokenizers.whitespace,
          local: $.map(cities, function (cityName) { return { value: cityName }; }),
          limit: 50
        });
    
        firstcitynames.initialize();
    
        $(".typeahead").typeahead({
          hint: false,
          highlight: true,
          minLength: 1
        },
       {
          name: "firstcitynames",
          displayKey: "value",
          source: firstcitynames.ttAdapter()
        }).bind("typeahead:selected", function (obj, datum, name) {
          // here action after select choice
        });
      }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料