0

I've started using twitters typeahead couple of days ago, and it was working perfectly while I wanted from him to show JUST one value, IE username, or something like that. And when I decided I want something more, like how on Facebook in search you have in drop-down list of results picture and next to the picture persons name and some other info, I got major problems. Tried couple of tutorials and managed to make one of the work, but only with static JSON file. I was wondering could I replace that JSON file with SQL queuery?

The code below is for text only:

$(document).ready(function() {
    var users = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('username'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: 'users.php?query=%QUERY'        
    });

    users.initialize();

    $('#users').typeahead({
        hint: true,
        highlight: true     
    }, {
        name: 'users',
        displayKey: 'username',
        source: users.ttAdapter()
    }); 
});

And this code is for JSON file custom display:

$(document).ready(function () {

$('.WordpressPosts').typeahead({
    name: 'Wordpress',
    prefetch: 'test2.json',
    template: [
        '<p class="repo-tag">{{tag}}</p>',
        '<p class="repo-name">{{name}}</p>',
        '<p class="repo-description">{{description}}</p>'
    ].join(''),
    engine: Hogan
    });
});

Is there anything I can do to fix my issue? Thank you in advance.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
zmuci
  • 518
  • 1
  • 5
  • 21
  • possible duplicate of [Update JSON on every keyup for twitter typeahead](http://stackoverflow.com/questions/27347121/update-json-on-every-keyup-for-twitter-typeahead) – fedorqui Jan 18 '15 at 14:03
  • This is what I want to get, but i can't manage to make it work. https://twitter.github.io/typeahead.js/examples/#custom-templates – zmuci Jan 18 '15 at 14:44

1 Answers1

1

Ok, after I used Console to see whats wrong I figured it out. I didn't have Handlebars defined. It's working now!

I solved the problem on my own. Here is the custom java script file I made/used.

$(document).ready(function() {
    var users = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('username'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        remote: 'users.php?query=%QUERY'        
    });

    users.initialize();

    $('#users').typeahead({
        hint: true,
        highlight: true     
    }, {
        name: 'users',
        displayKey: 'username',
        source: users.ttAdapter(),
        templates: {
        empty: [
        '<div style="height:50px; width:330px;">',
        'Nope',
        '</div>'
        ].join('\n'),
        suggestion: Handlebars.compile('your html code, call items with {{username}}')
        } 
    }); 
});

And files you need to include:

<script src="js/hogan.js"></script>
<script src="js/handlebars-v2.0.0.js"></script>
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.typeahead.js"></script>

<script src="js/typeahead.js"></script>
<script src="js/global.js"></script>
fedorqui
  • 275,237
  • 103
  • 548
  • 598
zmuci
  • 518
  • 1
  • 5
  • 21
  • good to see this! You could also post your final code, so that next people viewing this question can learn from your research. – fedorqui Jan 18 '15 at 18:40
  • I just moved the answer to the answer section! This way it is clear what is the question and what is the answer. – fedorqui Jan 19 '15 at 08:59