Duplicate records coming in typeahead search

后端 未结 1 1586
执念已碎
执念已碎 2020-12-20 12:49

I am implementing typeahead search using typeahaead.js but as type in typeahead searchbox, in suggestions dropdown each records is coming twice.I checked the datasource(that

1条回答
  •  无人及你
    2020-12-20 13:29

    I found the solution,i was doing wrong in filter. My solution is

    var result = new Bloodhound({
                datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
                queryTokenizer: Bloodhound.tokenizers.whitespace,
    
                remote: {
                    url: 'abc.com&qterm=#%QUERY',
                    wildcard: '%QUERY',
                    rateLimitWait: 300 ,
                    transport: function (opts, onSuccess, onError) {
                        var url = opts.url.split("#")[0];
                        var query = opts.url.split("#")[1];
                        $.ajax({
                            url: url + query,
                            type: "POST",
                            success: onSuccess,
                            error: onError,
                        });
    
    
                    },
                    filter: function (data) {
                        if (data) {
                            var result = data.data.results.data;
                            return $.map(result, function (object) {
                                return { name: object.basicinfo.object_name, id: object.basicinfo.id };
                            });
                        } else {
                            return {};
                        }
                    }
                },
                dupDetector: function (remoteMatch, localMatch) {
                    return remoteMatch.id === localMatch.id;
                }
            });
            function onSuccess(data) {
            }
            result.initialize();
            $('input').typeahead(null, {
                hint: true,
                name: 'result',
                displayKey: 'name',
                source: result.ttAdapter(),
                templates: {
                    empty: ['
    ', 'no results found', '
    '].join('\n'), suggestion: function (data) { return '

    ' + data.name + '

    \n\r'; } }, })

    0 讨论(0)
提交回复
热议问题