jQuery UI Autocomplete using a static json file as source

前端 未结 2 696
被撕碎了的回忆
被撕碎了的回忆 2020-12-29 12:46

I\'m using jquery-ui-autocomplete (actually, a catcomplete attached to a search box). It works vey well as long as I use a static-defined a

2条回答
  •  鱼传尺愫
    2020-12-29 13:10

    Try flipping it around, so on page-load you grab it once, then instantiate the autocomplete.

    $(function() {
        $.ajax({
            url: "/path/to/cache.json",
            dataType: "json",
            data: {term: request.term},
            success: function(data) {
                var cat_data = $.map(data, function(item) {
                    return {
                        label: item.label,
                        category: item.category,
                        desc: item.desc
                    };
                });
                $("#searchfield").catcomplete({
                    delay: 0,
                    source: cat_data,
                    minlength:0
                });
            }
        });
    });
    

提交回复
热议问题