Bootstrap typeahead ajax result format - Example

前端 未结 2 1324
独厮守ぢ
独厮守ぢ 2020-12-23 18:27

I am using Bootstrap typeahead with an ajax function, and want to know what is the correct Json result format, to return an Id and a descripcion. I need the Id to bind the t

2条回答
  •  抹茶落季
    2020-12-23 18:41

    Note: I saw @EralpB`s comment "Is this still the best way to accomplish this task?"

    Yes, @Gonzalo solution works, but it seems strange (like crutch,especially after using jQuery-Autocomplete) - it should work "from box".Better is to use this - Bootstrap-3-Typeahead. It supports an array of objects as result: format - [{id: .. ,name:...},...,{id: .. ,name:...}]. Example for OP`s issue:

    ....
     source: function (query, process) {
        return $.ajax({
            ......
            success: function (result) {            
                var resultList = [];
    
                $.map(result,function (Id,value) {
                    var aItem = { id: Id, name: value};
                    resultList.push(aItem);
                });
    
                return process(resultList);
            }
        });
      },
    

提交回复
热议问题