1

I am using https://github.com/tcrosen/twitter-bootstrap-typeahead/blob/master/demo/js/demo.js

my call:

$('#SearchUser').typeahead({
                ajax: { 
                    url: '/users/ajax_finduser', 
                        triggerLength: 3, 
                        timeout: 300,
                        method: 'post',

                },
                display: 'name',
                val: 'id',
                itemSelected: updateID
            });

My new output:

[
   {"id":"5","name":"Som name"},
   {"id":"6","name":"Another name"}
]

And here is my problem the VAL and NAME the typeahead is expecting should be like this:

[
    { id: 1, name: 'Some users name' },
    { id: 2, name: 'Another users name' }
]

So how do I add the extra level to my typeahead function (The USER.name + User.id)? I have no idea what to use (){}[]?

How to fix the quotes? The typeahead does not accept the json output as it is. I read somewhere that my output is correct json. Am I missing something here?

4

1 回答 1

4

遵循@RuslanasBalčiūnas 建议的要点
这是我最终得到的:

autosep = '####';
$('.autocomplete').typeahead({
    minLength: 3
  , source: function (query, process) {
        if (!finished) {
            return;
        }
        finished = false;
        return $.get(
            'the_action'
          , the_params
          , function (response) {
                var data = new Array(); 
                for (var i in response) {
                    data.push(
                        response[i]['id']
                      + autosep
                      + response[i]['label']
                    );
                }
                finished = true;
                return process(data);
            }
        );
    }
  , highlighter: function(item) {
        var parts = item.split(autosep);
        parts.shift();
        return parts.join(autosep);
    }
  , updater: function(item) {
        var parts = item.split(autosep);
        $('#the_id').val(parts.shift());
        return parts.join(autosep);
    }
});
于 2012-09-17T13:07:19.080 回答