Twitter Bootstrap Typeahead - Id & Label

前端 未结 10 2156
情书的邮戳
情书的邮戳 2020-12-12 13:06

I\'m using Bootstrap 2.1.1 and jQuery 1.8.1 and trying to use Typeahead\'s functionality.

I try to display a label and use an id li

10条回答
  •  悲&欢浪女
    2020-12-12 13:54

    I've been struggling with this problem myself, here is the solution I came up with, for data of the type:

    [{'id':an_id, 'name':a_name}]
    

    Was:

    $("#memberSearch").typeahead({
                source: function (query, process) {
                    var $this = this //get a reference to the typeahead object
                    return $.get('/getSwimmerListJSON',function(data){
                        var options = [];
                        $this["map"] = {}; //replace any existing map attr with an empty object
                        $.each(data,function (i,val){
                            options.push(val.name);
                            $this.map[val.name] = val.id; //keep reference from name -> id
                        });
                        return process(options);
                    });
                },
                updater: function (item) {
                    console.log(this.map[item],item); //access it here
    
                }
    
            });
    

提交回复
热议问题