2014-03-30 28 views
5

我正在使用多個遠程數據集來獲取Typeahead插件的數據。一切正常。Global Typeheadhead&No Match部分

現在我想實現兩件事情

  1. 如果所有遠程數據集並沒有取得任何結果,「沒有找到結果」應該顯示。這不應該顯示任何1個遠程源是否有數據。

  2. 我想顯示一個靜態鏈接在打開容器的頁腳,如果有結果。我怎樣才能在頁腳顯示鏈接?

我該如何做到這一點?我不知道如何繼續。

有些示例顯示每個部分的頁腳,而不是使用「空」和「頁腳」類的整個容器。但它們處於數據集級而不是全局。

鏈接:https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#datasets

其他SO問題與此類似:

Global footer in typeahead dropdown

var nbaTeams = new Bloodhound({ 
    datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.value); }, 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    remote: '../data/nba.json' 
}); 

var nhlTeams = new Bloodhound({ 
datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.value); }, 
queryTokenizer: Bloodhound.tokenizers.whitespace, 
remote: '../data/nhl.json' 
}); 

nbaTeams.initialize(); 
nhlTeams.initialize(); 

$('#multiple-datasets .typeahead').typeahead({ 
    highlight: true 
}, 
{ 
    name: 'nba-teams', 
    displayKey: 'team', 
    source: nbaTeams.ttAdapter(), 
    templates: { 
    header: '<h3 class="league-name">NBA Teams</h3>' 
    } 
}, 
{ 
    name: 'nhl-teams', 
    displayKey: 'team', 
    source: nhlTeams.ttAdapter(), 
    templates: { 
    header: '<h3 class="league-name">NHL Teams</h3>' 
    } 
}); 

回答

3

你可以做

$('.typeahed').typeahed(null, { 
    name: 'suggestions', 
    templates: { 
     footer: Handlebars.compile('Results for {{ query }}'), 
     empty: Handlebars.compile('<strong>Not Results for found.</strong>') 

    } 
}); 
+0

你有說明的jsfiddle如何聯繫與代碼普魯斯與這個問題有關嗎? – Phil

+0

我會嘗試添加一個jsfiddle。無論如何,我使它在[鏈接](http://likefolio.com)上工作 – poramo

+0

我沒有看到你的工作,在所提供的鏈接。如果沒有找到結果,我在鏈接中看到的前端不會顯示任何消息 – Phil

0

或者你可以自定義的監聽器,並改變鏈接重新輸入typehead時的href屬性挑起東西。例如

$('.typeahead').bind('typeahead:render', function() { 
     $('.typehead__show-all-link').attr('href', '/search?q='+ $('.tt-input').val()); 
    }); 

其中.typehead - 是您typehead輸入, .typehead__show - 所有鏈接 - 用於顯示的鏈接類的更多結果