本文介绍了类型检索中出现重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用typeahaead.js实现typeahead搜索,但是在typeahead搜索框中输入类型,在建议下拉列表中每个记录都会出现两次。我检查了数据源(即POST api调用),它只有唯一的记录。我在做什么错误?任何帮助或相关链接。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 is POST api call),it has only unique records.where am I doing wrong?Any help or relevant links.即使控制也不会重复检测器。Even control is not going to dup detector.类似问题这里讨论,但没有解决方案。Similar issue discussed here,but no solution is there. <div id="bloodhound"> <input class="typeahead" type="text" placeholder=" Search"> </div><script> var result = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { url: 'https://api1.com/idocs/api', wildcard: '%QUERY', rateLimitWait: 300 , transport: function (opts, onSuccess, onError) { var url = opts.url; $.ajax({ url: url, type: "POST", success: onSuccess, error: onError, }); }, filter: function (data) { if (data) { return $.map(data, function (object) { return data.data.results.data; }); } } }, dupDetector: function (remoteMatch, localMatch) { return remoteMatch.id === localMatch.id; } }); result.initialize(); $('input').typeahead(null, { name: 'result', displayKey: 'id', source: result.ttAdapter(), templates: { empty: ['<div>', 'no results found', '</div>'], suggestion: function (data) { return '<p>' + data.basicinfo.object_name + '</p>'; } }, });推荐答案我找到了解决方案,我在过滤器中做错了。我的解决方案是I found the solution,i was doing wrong in filter. My solution isvar 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: ['<div class="empty-message">', 'no results found', '</div>'].join('\n'), suggestion: function (data) { return '<p class="type-suggestion">' + data.name + '</p> \n\r'; } }, }) 这篇关于类型检索中出现重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-15 12:11