dpftppc9674 2015-01-18 13:58
浏览 22
已采纳

Typeahead问题

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.

  • 写回答

1条回答 默认 最新

  • doukong1901 2015-01-18 15:48
    关注

    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('
    '),
            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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?