Typeahead |如何从建议中删除重复项?

时间:2017-05-01 06:57:11

标签: javascript laravel duplicates typeahead.js

我已成功使用laravel实现了typeahead自动完成功能。 但是我注意到一个奇怪的事情,重复项在我的建议中显示。现在我的数据库不包含任何重复项(如果感兴趣,我使用softdeletes。)

我认为我必须对本地缓存和远程问题采取一些措施。 但仍然 这是我的控制器逻辑:

public function loanCustomer(Request $request)
{
    return customer::search($request->get('q'))->where('loan_status','!=',false)->get();
}


public function loanCustomerPrefetch()
{

   $all_customers= customer::All()->where('loan_status','!=',false);
   return \Response::json($all_customers);
}

这是 JavaScript代码:

var customerSugestion = function () {
    var firstName = $('#first_name');
    var lastName = $('#last_name');
    var mobileNo = $('#mobile_no');
    var adharNo = $('#adhar_no');
    var customerId = $('#customer_id');
    // Set the Options for "Bloodhound" suggestion engine
    var engine = new Bloodhound({
    prefetch: '/find_loan_customer_all',
    remote: {
    url: '/find_loan_customer?q=%QUERY%',
    wildcard: '%QUERY%'
    },
    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('first_name'),
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    //try removing duplicates
    dupDetector: function(remoteMatch, localMatch) {
    return remoteMatch.name === localMatch.name;
    }
    });
    firstName.typeahead({
    hint: true,
    highlight: false,
    minLength: 1
    }, {
    source: engine.ttAdapter(),
    // This will be appended to "tt-dataset-" to form the class name of the suggestion menu.
    name: 'usersList',
    display: function(data) {
        var name =  data.first_name +' '+ data.last_name;
    return name;
    },
    // display: function(data){ return data.first_name + ' ' + data.last_name}
    // the key from the array we want to display (name,id,email,etc...)
    templates: {
    empty: [
    '<a class="list-group-item">Customer not found.</a>'
    ],

    suggestion: Handlebars.compile([
    '<div class="media">',
                '<div class="pull-left">',
                    '<div class="media-object">',
                        '<img src="/images/{{imagePath}}" width="50" height="50"/>',
                    '</div>',
                '</div>',
                '<div class="media-body">',
                    '<h4 class="media-heading">{{first_name}} {{last_name}}</h4>',
                    '<label><i class="fa fa-phone" aria-hidden="true"></i> {{mobile_no}}</label>',
                    '<label style="float: right; align-content: left;"><i class="fa fa-passport" aria-hidden="true"></i> ID: {{id}}</label>',
                '</div>',
          '</div>',
    ].join(''))
    }
    });
    var firstNameItemSelectedHandler = function (eventObject, suggestionObject, suggestionDataset) {
    /* According to the documentation the following should work https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#jquerytypeaheadval-val.
    However it causes the suggestion to appear which is not wanted */
    //employeeIdTypeahead.typeahead('val', suggestionObject.id);
    $('#profile').load('/pay_profile/'+suggestionObject.id);


    lastName.val(suggestionObject.last_name);
    mobileNo.val(suggestionObject.mobile_no);
    adharNo.val(suggestionObject.adhar_no);
    customerId.val(suggestionObject.id);
                };
        firstName.on('typeahead:selected', firstNameItemSelectedHandler);
}

正如您所看到的,我已尝试使用dupDetector,但仍然无效

修改

到目前为止,我找不到任何解决办法,只能禁用预取。

0 个答案:

没有答案
相关问题