简体   繁体   English

跟踪typeahead.js从搜索加载新内容的时间

[英]Track when typeahead.js loaded new content from search

I have a simple typeahead from Twitters Typeahead.js, and it works great. 我有一个来自Twitters Typeahead.js的简单预输入,它很好用。 However, I need to track when the search results changes, so that I can load a function, which will load specific images inside the results div from the typeahead. 但是,我需要跟踪搜索结果何时更改,以便可以加载一个函数,该函数将从预先输入的结果div中加载特定的图像。

My typeahead code looks like this: 我的预输入代码如下所示:

$('#search').typeahead({
    remote: 'ajax-connect.php?search&q=%QUERY',
    valueKey: 'header',
    autoselect: true,
    template: [
        '<a href=\'{{link}}\' class=\'group\'>',
        '<span class=\'image\'><img style=\'width:60px;height:60px;\' data-fk=\'{{user_id}}\' data-type=\'{{fk_type}}\' data-width=\'60\' class=\'repo-avatar\' src=\'" . GetAvatar() . "\'></span>',
        '<span class=\'details\'>',
        '<p class=\'repo-name\'>{{header}}</p>',
        '<p class=\'repo-note\'>{{note}}</p>',
        '<p class=\'repo-description\'>{{description}}</p>',
        '<p class=\'rep-subfield\'>{{subfield}}</p>',
        '<p class=\'type\'>{{type}}</p>',
        '</span>',
        '</a>'
    ].join(''),
    engine: Hogan
})
.bind('typeahead:selected', function () {
    $('form').submit();
});

I have tried the following: 我尝试了以下方法:

$('.tt-dropdown-menu').on('change', function(){
    alert('it changed!');
    SetAvatarUrls();
});

But that doesn't work. 但这是行不通的。

The function setavatarurls looks like this: (maybe there is a way, so that in general, it will just simply look for any new 's and then run the function?) 函数setavatarurls如下所示:(也许有一种方法,因此通常来说,它只是简单地查找任何new,然后运行该函数?)

function SetAvatarUrls(reload) {
    $('img.avatar').each(function() {
        var fk = $(this).data('fk');
        var type = $(this).data('type');
        var width = $(this).data('width');
        var height = $(this).data('height');
        var thumb = $(this).data('thumb');
        var required = $(this).data('required');
        var auto = $(this).data('auto');
        var temp = $(this).data('temp');

        if(!$(this).data('done') || reload) {
            if(auto) {
                $(this).attr('src', root_url+'ajax-connect.php?avatar&required='+required+'&src='+$(this).attr('src'));
                var success = 1;
            } else if(fk && type) {
                $(this).attr('src', root_url+'ajax-connect.php?avatar&temp='+temp+'&fk='+fk+'&type='+type+'&w='+width+'&h='+height+'&thumb='+thumb+'&required='+required);
                var success = 1;
            }

            if(success) {
                $(this).data('done', '1');
            }
        }
    });
}

Any suggestions? 有什么建议么? I have been searching around for a long time now, but in general, theres not much information about Twitters Typeahead.js 我已经搜索了很长一段时间,但是总的来说,关于Twitters Typeahead.js的信息并不多

I have also tried the following code, but I only gives me the alert on LOAD and when you click away from the searchbar, and then click again: 我还尝试了以下代码,但是当您单击远离搜索栏然后再次单击时,我只会在LOAD时给我警报。

.bind('typeahead:opened', function () {
    alert('now!');
    SetAvatarUrls();
});

Please be aware: I am using the Twitters Typeahead.js as recommended by Bootstrap after the original typeahead was deprecated as of 3.0. 请注意:自3.0版弃用原始Typeahead后,我将按照Bootstrap的建议使用Twitters Typeahead.js。

Thanks in advance! 提前致谢!

Did you try following modified version of typeahead.js: https://gist.github.com/Yavari/1891669 ? 您是否尝试过遵循typeahead.js的修改版本: https : //gist.github.com/Yavari/1891669
It gives possibility of attaching some events, and maybe it has what you need. 它提供了附加一些事件的可能性,也许它满足您的需求。

Update: 更新:

Try this: 尝试这个:

$('#search').on('keyup', function() { alert('test'); SetAvatarUrls() });

and if you want to call SetAvatarsUrls() only when typeahead suggests something, try this instead: 如果只想在提示输入内容时才调用SetAvatarsUrls() ,请尝试以下方法:

$('#search').on('keyup', function() { 
    if($('.tt-dropdown-menu span.tt-suggestions div.tt-suggestion').length > 0) {
        alert('test');
        SetAvatarUrls();
    }
});

There are some standard events you can listen to with Typeahead. 您可以使用Typeahead收听一些标准事件。 (see https://github.com/twitter/typeahead.js/#custom-events ) (请参阅https://github.com/twitter/typeahead.js/#custom-events

typeahead:opened is the event for you. typeahead:opened是您的活动。

$('#search').on('typeahead:opened', function() { 
    if($('.tt-dropdown-menu span.tt-suggestions div.tt-suggestion').length > 0) {
        alert('test');
        SetAvatarUrls();
} });

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

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