updating/refreshing kendo ui autocomplete with array change

2k views Asked by At

I have a standard jquery kendo ui autocomplete, simple enough to initiate:

    let querCo = [];
    ...........
    $.each(lRsp, function(i, value) {
         querCo.push(value[id]);
    });

   $("#agent").kendoAutoComplete({
        dataSource: querCo,
        serverFiltering: true, // this was also an attempt
        select: function(e) {
                let item = e.item;
                text = item.text();
                // console.log(text);
        }
    }); 

All is fine, except when trying to update it... for instance, I have a delete button, with a goal to delete the selected item; I get the selected item fine, and update the array at querCo and remove that item from the array. But the autocomplete still shows it....

 $('#del').click(function(){
    let val = text;
    querCo = $.grep(querCo, function(value) {
      return value != val;
  });
  console.log(querCo); // correct, outputs updated array

Then I have tried both of the below; seemingly does nothing... original array, alas deleted item, still displays in auto complete?

attempt 1

$("#agent").data("kendoAutoComplete").dataSource.read();

attempt 2

$("#agent").data("kendoAutoComplete").refresh();
2

There are 2 answers

0
DontVoteMeDown On BEST ANSWER

You can also try setting new data with data method:

$("#agent").data("kendoAutoComplete").dataSource.data(querCo);

<!DOCTYPE html>
<html>
<head>
    <base href="https://demos.telerik.com/kendo-ui/autocomplete/index">
    <style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
    <title></title>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2020.3.1021/styles/kendo.default-v2.min.css" />

    <script src="https://kendo.cdn.telerik.com/2020.3.1021/js/jquery.min.js"></script>
    
    
    <script src="https://kendo.cdn.telerik.com/2020.3.1021/js/kendo.all.min.js"></script>
    
    

</head>
<body>
    
        <div id="example">
            <div class="demo-section k-content">
                <h4><label for="countries">Choose shipping countries:</label></h4>
                <input id="countries" style="width: 100%;" />
                <div class="demo-hint">Start typing the name of an European country</div>
              
              <button id="remove" type="button">Remove Countries starting with "A"</button>
            </div>

            <script>
                $(document).ready(function () {
                    var data = [
                            "Albania",
                            "Andorra",
                            "Armenia",
                            "Austria",
                            "Azerbaijan",
                            "Belarus",
                            "Belgium",
                            "Bosnia & Herzegovina",
                            "Bulgaria",
                            "Croatia",
                            "Cyprus",
                            "Czech Republic",
                            "Denmark",
                            "Estonia",
                            "Finland",
                            "France",
                            "Georgia",
                            "Germany",
                            "Greece",
                            "Hungary",
                            "Iceland",
                            "Ireland",
                            "Italy",
                            "Kosovo",
                            "Latvia",
                            "Liechtenstein",
                            "Lithuania",
                            "Luxembourg",
                            "Macedonia",
                            "Malta",
                            "Moldova",
                            "Monaco",
                            "Montenegro",
                            "Netherlands",
                            "Norway",
                            "Poland",
                            "Portugal",
                            "Romania",
                            "Russia",
                            "San Marino",
                            "Serbia",
                            "Slovakia",
                            "Slovenia",
                            "Spain",
                            "Sweden",
                            "Switzerland",
                            "Turkey",
                            "Ukraine",
                            "United Kingdom",
                            "Vatican City"
                        ];

                    //create AutoComplete UI component
                    $("#countries").kendoAutoComplete({
                        dataSource: data,
                        filter: "startswith",
                        placeholder: "Select country...",
                        separator: ", "
                    });
                  
                  $("#remove").on("click", function() {
                    let newData = data.filter(item => item[0] !== "A");
                    
                    $("#countries").data('kendoAutoComplete').dataSource.data(newData);
                  });
                });
            </script>
        </div>
</body>
</html>

Dojo

0
NigelK On

I believe the method you're after is setDataSource to replace the dataSource the autocomplete is bound to:

$("#agent").data("kendoAutoComplete").setDataSource(querCo);

https://docs.telerik.com/kendo-ui/api/javascript/ui/autocomplete/methods/setdatasource