|
| 1 | +--- |
| 2 | +title: Refresh Data |
| 3 | +page_title: Autocomplete Refresh Data |
| 4 | +description: Refresh Autocomplete Data using Observable Data or creating a new Collection reference. |
| 5 | +slug: autocomplete-refresh-data |
| 6 | +tags: telerik,blazor,autocomplete,observable,data,new,collection |
| 7 | +published: True |
| 8 | +position: 15 |
| 9 | +--- |
| 10 | + |
| 11 | +# Autocomplete - Refresh Data |
| 12 | + |
| 13 | + |
| 14 | +@[template](/_contentTemplates/common/observable-data.md#intro) |
| 15 | + |
| 16 | +In this article: |
| 17 | +- [Observable Data](#observable-data) |
| 18 | +- [New Collection Reference](#new-collection-reference) |
| 19 | + |
| 20 | +## Observable Data |
| 21 | + |
| 22 | +@[template](/_contentTemplates/common/observable-data.md#observable-data) |
| 23 | + |
| 24 | + |
| 25 | +>caption Bind the Autocomplete component to an ObservableCollection, so it can react to collection changes. |
| 26 | +
|
| 27 | +````CSHTML |
| 28 | +@* Add/remove a suggestion to see how the Autocomplete reacts to the change. *@ |
| 29 | +
|
| 30 | +@using System.Collections.ObjectModel |
| 31 | +
|
| 32 | +<h4>Add suggestion</h4> |
| 33 | +<TelerikTextBox @bind-Value="@ValuetoAdd"></TelerikTextBox> |
| 34 | +
|
| 35 | +<TelerikButton OnClick="@AddSuggestion">Add suggestion</TelerikButton> |
| 36 | +<br /> |
| 37 | +
|
| 38 | +<h4>Remove the last suggestion</h4> |
| 39 | +<TelerikButton OnClick="@RemoveSuggestion">Remove the last suggestion</TelerikButton> |
| 40 | +<br /> |
| 41 | +
|
| 42 | +<h4>Autocomplete suggestions: @Suggestions.Count</h4> |
| 43 | +<br /> |
| 44 | +
|
| 45 | +<TelerikAutoComplete Data="@Suggestions" ValueField="@( nameof(SuggestionsModel.Suggestion) )" @bind-Value="@TheValue" /> |
| 46 | +
|
| 47 | +@code{ |
| 48 | + string TheValue { get; set; } |
| 49 | +
|
| 50 | + string ValuetoAdd { get; set; } |
| 51 | +
|
| 52 | + void AddSuggestion() |
| 53 | + { |
| 54 | + if (!string.IsNullOrWhiteSpace(ValuetoAdd)) |
| 55 | + { |
| 56 | + Suggestions.Add( |
| 57 | + new SuggestionsModel { Suggestion = ValuetoAdd, SomeOtherField = Suggestions.Count + 1 } |
| 58 | + ); |
| 59 | + ValuetoAdd = string.Empty; |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | + void RemoveSuggestion() |
| 64 | + { |
| 65 | + if (Suggestions.Count > 0) |
| 66 | + { |
| 67 | + Suggestions.RemoveAt(Suggestions.Count - 1); |
| 68 | + } |
| 69 | + } |
| 70 | +
|
| 71 | + ObservableCollection<SuggestionsModel> Suggestions { get; set; } = new ObservableCollection<SuggestionsModel> |
| 72 | + { |
| 73 | + new SuggestionsModel { Suggestion = "first", SomeOtherField = 1 }, |
| 74 | + new SuggestionsModel { Suggestion = "second", SomeOtherField = 2 }, |
| 75 | + new SuggestionsModel { Suggestion = "third", SomeOtherField = 3 } |
| 76 | + }; |
| 77 | +
|
| 78 | + public class SuggestionsModel |
| 79 | + { |
| 80 | + public string Suggestion { get; set; }//the auto complete needs only the string field |
| 81 | + public int SomeOtherField { get; set; } |
| 82 | + } |
| 83 | +} |
| 84 | +```` |
| 85 | + |
| 86 | +@[template](/_contentTemplates/common/observable-data.md#tip-for-new-collection) |
| 87 | + |
| 88 | +## New Collection Reference |
| 89 | + |
| 90 | +@[template](/_contentTemplates/common/observable-data.md#refresh-data) |
| 91 | + |
| 92 | +>caption Create new collection reference to refresh the Autocomplete data. |
| 93 | +
|
| 94 | +````CSHTML |
| 95 | +@* Add/remove a suggestion to see how the Autocomplete reacts to the change. *@ |
| 96 | +
|
| 97 | +<h4>Add suggestion</h4> |
| 98 | +<TelerikTextBox @bind-Value="@ValuetoAdd"></TelerikTextBox> |
| 99 | +
|
| 100 | +<TelerikButton OnClick="@AddSuggestion">Add suggestion</TelerikButton> |
| 101 | +<br /> |
| 102 | +
|
| 103 | +<h4>Remove the last suggestion</h4> |
| 104 | +<TelerikButton OnClick="@RemoveSuggestion">Remove the last suggestion</TelerikButton> |
| 105 | +<br /> |
| 106 | +
|
| 107 | +<h4>Load new collection</h4> |
| 108 | +<TelerikButton OnClick="@LoadNewData">Load data</TelerikButton> |
| 109 | +<br /> |
| 110 | +
|
| 111 | +<h4>Autocomplete suggestions: @Suggestions.Count</h4> |
| 112 | +<br /> |
| 113 | +
|
| 114 | +<TelerikAutoComplete Data="@Suggestions" ValueField="@( nameof(SuggestionsModel.Suggestion) )" @bind-Value="@TheValue" /> |
| 115 | +
|
| 116 | +@code{ |
| 117 | + string TheValue { get; set; } |
| 118 | +
|
| 119 | + string ValuetoAdd { get; set; } |
| 120 | +
|
| 121 | + void AddSuggestion() |
| 122 | + { |
| 123 | + if (!string.IsNullOrWhiteSpace(ValuetoAdd)) |
| 124 | + { |
| 125 | + Suggestions.Add( |
| 126 | + new SuggestionsModel { Suggestion = ValuetoAdd, SomeOtherField = Suggestions.Count + 1 } |
| 127 | + ); |
| 128 | + Suggestions = new List<SuggestionsModel>(Suggestions); |
| 129 | + ValuetoAdd = string.Empty; |
| 130 | + } |
| 131 | + } |
| 132 | +
|
| 133 | + void RemoveSuggestion() |
| 134 | + { |
| 135 | + if (Suggestions.Count > 0) |
| 136 | + { |
| 137 | + Suggestions.RemoveAt(Suggestions.Count - 1); |
| 138 | + Suggestions = new List<SuggestionsModel>(Suggestions); |
| 139 | + } |
| 140 | + } |
| 141 | +
|
| 142 | + void LoadNewData() |
| 143 | + { |
| 144 | + var newData = new List<SuggestionsModel> |
| 145 | + { |
| 146 | + new SuggestionsModel { Suggestion = "fourth", SomeOtherField = 4 }, |
| 147 | + new SuggestionsModel { Suggestion = "fifth", SomeOtherField = 5 }, |
| 148 | + new SuggestionsModel { Suggestion = "sixth", SomeOtherField = 6 } |
| 149 | + }; |
| 150 | +
|
| 151 | + Suggestions = new List<SuggestionsModel>(newData); |
| 152 | +
|
| 153 | + Console.WriteLine("New data collection loaded."); |
| 154 | + } |
| 155 | +
|
| 156 | + List<SuggestionsModel> Suggestions { get; set; } = new List<SuggestionsModel> |
| 157 | + { |
| 158 | + new SuggestionsModel { Suggestion = "first", SomeOtherField = 1 }, |
| 159 | + new SuggestionsModel { Suggestion = "second", SomeOtherField = 2 }, |
| 160 | + new SuggestionsModel { Suggestion = "third", SomeOtherField = 3 } |
| 161 | + }; |
| 162 | +
|
| 163 | + public class SuggestionsModel |
| 164 | + { |
| 165 | + public string Suggestion { get; set; }//the auto complete needs only the string field |
| 166 | + public int SomeOtherField { get; set; } |
| 167 | + } |
| 168 | +} |
| 169 | +```` |
| 170 | + |
| 171 | + |
| 172 | +## See Also |
| 173 | + |
| 174 | + * [ObservableCollection]({%slug common-features-observable-data%}) |
| 175 | + * [INotifyCollectionChanged Interface](https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.inotifycollectionchanged?view=netframework-4.8) |
| 176 | + * [Live Demos](https://demos.telerik.com/blazor-ui/) |
0 commit comments