简体   繁体   English

提前问题

[英]Typeahead issues

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. 几天前,我已经开始使用twitters typea,当我希望他向他显示一个值,IE用户名或类似名称时,它运行得非常好。 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. 当我决定要更多东西时,例如在Facebook上搜索结果的方式下拉列表中,以及在人物名称和其他信息旁边的方式,我遇到了主要问题。 Tried couple of tutorials and managed to make one of the work, but only with static JSON file. 尝试了几套教程,并设法完成了其中一项工作,但仅使用静态JSON文件。 I was wondering could I replace that JSON file with SQL queuery? 我想知道我可以用SQL队列替换该JSON文件吗?

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: 这段代码用于JSON文件自定义显示:

$(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. 先感谢您。

Ok, after I used Console to see whats wrong I figured it out. 好的,在我使用Console来查看出什么问题之后,我找到了答案。 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. 这是我制作/使用的自定义Java脚本文件。

$(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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM