正在加载微调typeaheadjs猎犬标签输入

问题描述 投票:0回答:1

我已经设置了带有猎犬和标签输入的自动完成搜索表单。

在远程环境中效果很好,但建议使用Wordpress API大约需要1或2秒。

我想在输入中添加加载微调器。这样一来,对于人们来说,他们很明显必须等待建议被加载。

我知道这篇文章https://github.com/twitter/typeahead.js/issues/166,但这仅涉及typeaheadjs,而不涉及tagsinput。老实说,当我使用猎犬和标签输入时,我不太了解typeaheadjs的目的。

我使用2个js文件:typeahead.bundle.js和bootstrap-tagsinput.js

这是我的工作代码(不包括微调器):

var transform_terms = function( terms ) {
            return $.map( terms, function( term ) {
                return {
                    id: term.id,
                    name: term.name,
                    count: term.count
                };
            } );
        };

        var localisations = new Bloodhound( {
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace( [ 'name', 'id' ] ),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            identify: function( term ) {
                return {
                    label: term.name,
                    value: term.id,
                };
            },
            sufficient: 5,              
            remote: {
                url: '/wp-json/wp/v2/localisation?_fields=name,id&orderby=count&order=desc&hide_empty=false&search=%QUERY',
                wildcard: '%QUERY',
                transform: transform_terms
            },
            indexRemote: true
        } );
        localisations.initialize();


        $('#localisation').tagsinput({
            itemValue: 'id',
            itemText: 'name',
            maxTags: 5,
            confirmKeys: [13, 9],
            typeaheadjs: {
                name: 'localisations',
                displayKey: 'name',
                source: localisations.ttAdapter()
            }
        });

有人可以在查询过程中帮助我添加微调器吗?在此先多谢!

jquery typeahead.js bloodhound bootstrap-tags-input
1个回答
0
投票

由于这篇很好的文章https://digitalfortress.tech/tutorial/smart-search-using-twitter-typeahead-bloodhound/,我找到了一个似乎可靠的解决方案。但是我不得不说,使用Bootstrap Typeahead并不明显,文档也很差...

这是我的新工作代码:

var transform_terms = function( terms ) {
            return $.map( terms, function( term ) {
                return {
                    id: term.id,
                    name: term.name,
                    count: term.count
                };
            } );
        };

        var localisations = new Bloodhound( {
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace( [ 'name', 'id' ] ),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            identify: function( term ) {
                return {
                    label: term.name,
                    value: term.id,
                };
            },
            sufficient: 5,

            remote: {
                url: '/wp-json/wp/v2/localisation?_fields=name,id&orderby=count&order=desc&hide_empty=false&search=',
                prepare: function (query, settings) {
                    console.log('load');
                    $("#search-loader").fadeIn();
                    settings.url = this.url + query;
                    return settings;
                },
                filter: function (data) {
                    console.log('finish');
                    $("#search-loader").fadeOut();
                    return data;
                }
            },
            identify: function (response) {
                return response.name;
            },
            indexRemote: true
        } );
        localisations.initialize();


        $('#localisation').tagsinput({
            itemValue: 'id',
            itemText: 'name',
            maxTags: 5,
            confirmKeys: [13, 9],
            typeaheadjs: [{
                    highlight: true
                },{
                    name: 'localisations',
                    displayKey: 'name',
                    source: localisations.ttAdapter()
                }]
        });
© www.soinside.com 2019 - 2024. All rights reserved.