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

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本