Anna Kegley
Anna Kegley

Reputation: 59

Kendo AutoComplete search() method not working

I am using the Kendo AutoComplete widget with a minLength of 4. However, if the user clicks a "GO" button I want to force the search to execute before the minLength is reached. The line where search is called executes without errors while debugging but does nothing. I am using remote data with server filtering. The autocomplete works perfectly once the min length is reached, I just can't force the search. Here is my code:

$(function() {
    $("#txtPatientSearch").kendoAutoComplete({
        dataTextField: 'PatName',
        filter: 'contains',
        minLength: 4,
        clearButton: false,
        template: '#= data.PatName # ( #= data.CurrentAge # #= data.Gender # ) <br> Sub. ID: #= data.SubscriberID # <br> MRN: #= data.MRN #',          
        dataSource: {
            serverFiltering: true,
            transport: {
                read: {
                    url: searchUrl,
                    data: function () {
                        return {
                            searchTerm: $('#txtPatientSearch').val(),
                            patientListId: Filters.getSelectedPatientList().value
                        }
                    }
                    
                }
            },
            group: { field: "ResCategory" }
            
        }
    });

    $('#btnSearchPatients').on('click', function () {
        var searchTerm = $('#txtPatientSearch').val();
        $('#txtPatientSearch').data('kendoAutoComplete').search(searchTerm);
    });

    //Keeps the dropdown from closing when the "GO" button is clicked
    $('#ddPatientSearch').click(function (e) {
        e.stopPropagation();
    });
});
<div id="ddPatientSearch" class="dropdown-menu dropdown-menu-left border-dark" aria-labelledby="btnOpenSearch">
    <div class="demo-section k-content" style="width:400px">
        <div class="container-fluid">
            <div class="row">
                <div class="col-10 pl-1 pr-0 text-right">
                    <input id="txtPatientSearch" class="form-control form-control-sm" placeholder="Search patients" />
                </div>
                <div class="col-2 pl-1 pr-0">
                    <button id="btnSearchPatients" type="button">GO</button>
                </div>
            </div>
        </div>
        <div class="demo-hint pl-2 text-dark" style="font-size:12px">Hint: Search by name, subscriber, or MRN</div>
    </div>
</div>

Upvotes: 1

Views: 1033

Answers (1)

stWrong
stWrong

Reputation: 334

Kendo Autocomplete search seems to respect the minLength option. This can be solved by:

  1. Explicitly setting the minLength to 1 or length of searchTerm
  2. Do the search
  3. Set the minLength to default value (4)
    $('#btnSearchPatients').on('click', function () {
      var searchTerm = $('#txtPatientSearch').val();
      let a = $('#txtPatientSearch').data('kendoAutoComplete');
      a.setOptions({minLength: 1});
      // Searching twice here, because kendo does not show suggestions
      a.search(searchTerm);
      a.search(searchTerm);
      a.setOptions({minLength: 4});
    });

For some reason kendo does not respect its change in options, so we are searching twice. (Using setTimeout and Promise did not help - assuming kendo takes some time to update the widget and UI after setOptions is called)

Upvotes: 2

Related Questions