forked from syncfusion/blazor-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBinding.razor
More file actions
235 lines (217 loc) · 12 KB
/
Copy pathDataBinding.razor
File metadata and controls
235 lines (217 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
@page "/AutoComplete/Data-Binding"
@using System.Collections.ObjectModel;
@using Syncfusion.Blazor.DropDowns
@using Syncfusion.Blazor.Data
@using System.Dynamic
@*Hidden:Lines*@
@inherits SampleBaseComponent
@*End:Hidden*@
<SampleDescription>
<p>
This example demonstrates the AutoComplete's data binding support. Type a character(s) in the AutoComplete component and the remaining characters will be automatically filled based on the first matched item from the suggestion list.
When you type the remote data AutoComplete, the loader icon will appear until the remote request retrieves the data from the server and displays it.
</p>
</SampleDescription>
<ActionDescription>
<p>
The AutoComplete filter the data either from local data sources or remote data services through the <a href="https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.DropDowns.IDropDownBase-1.html#Syncfusion_Blazor_DropDowns_IDropDownBase_1_DataSource" target="_blank"> DataSource</a> property.
It supports the data type of <code>Array</code>, <code>Observable Collection</code>, <code>ExpandoObject</code>, <code>DynamicObject</code> and <code>DataManager</code>.
</p>
<p>The <a href="https://blazor.syncfusion.com/documentation/data/getting-started" aria-label="Blazor DataManager Getting Started documentation">DataManager</a>, that acts as an interface between the service endpoint and AutoComplete will require the following minimal information to interact with the service endpoint properly.</p>
<ul>
<li><a href="https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.DataManager.html#Syncfusion_Blazor_DataManager_Url" target="_blank" aria-label="Class reference of Url property in DataManager">DataManager.Url</a> - Defines the service endpoint to fetch data.</li>
<li>
<p>
<a href="https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.DataManager.html#Syncfusion_Blazor_DataManager_Adaptor" target="_blank" aria-label="Class refernce of Adaptor property in DataManager">DataManager.Adaptor</a> - Defines the adaptor option.
By default, <code>ODataAdaptor</code> is used for remote binding.
The adaptor is responsible for processing responses and requests from/to the service endpoint.
<a href="https://www.nuget.org/packages/Syncfusion.Blazor.Data/">Syncfusion.Blazor.Data</a> package provides some predefined adaptors that are designed to interact with particular service endpoints.
They are:
</p>
<ul>
<li><code>UrlAdaptor</code> - Use this to interact with any remote services.</li>
<li><code>ODataAdaptor</code> - Use this to interact with OData endpoints.</li>
<li><code>ODataV4Adaptor</code> - Use this to interact with OData V4 endpoints.</li>
<li><code>WebApiAdaptor</code> - Use this to interact with Web API created under OData standards.</li>
<li><code>WebMethodAdaptor</code> - Use this to interact with web methods.</li>
</ul>
</li>
</ul>
<p>Below are the different types of data binding used in this sample.</p>
<ul>
<li>Local Data AutoComplete is bound to a collection of countries data.</li>
<li>Remote Data AutoComplete is bound to a collection of customer data using <code>DataManager</code>.</li>
<li>Observable Collection AutoComplete is bound to a <code>ObservableCollection</code> of colors data.</li>
<li>ExpandObject AutoComplete is bound to the collection of vehicles data of type <code>ExpandObject</code>.</li>
<li>DynamicObject AutoComplete is bound to the collection of customers data of type <code>DynamicObject</code>.</li>
</ul>
<p><b>See also</b></p>
<ul>
<li><a href="https://blazor.syncfusion.com/documentation/autocomplete/data-binding" target="_blank" aria-label="Blazor AutoComplete Data Binding documentation">Blazor AutoComplete - Data Binding</a></li>
<li><a href="https://blazor.syncfusion.com/documentation/data/getting-started" target="_blank" aria-label="Blazor Data Manager Getting Started documentation">Blazor Data Manager</a></li>
</ul>
</ActionDescription>
<div class="control-section col-lg-12">
<div class="row">
<div class="col-lg-6">
<div class="control-wrapper">
<label class="example-label">Local Data</label>
<SfAutoComplete TValue="string" TItem="Countries" PopupHeight="200px" Placeholder="e.g. Australia" DataSource="@Country" Autofill=true @bind-Value="@CountryValue">
<AutoCompleteFieldSettings Value="Name"/>
</SfAutoComplete>
</div>
</div>
<div class="col-lg-6">
<div class="control-wrapper">
<label class="example-label">Remote Data</label>
<SfAutoComplete TValue="string" TItem="EmployeeData" PopupHeight="200px" Placeholder="Select a name" Query="@RemoteQuery" Autofill="true" @bind-Value="@EmployeeValue">
<SfDataManager Url="https://blazor.syncfusion.com/services/production/api/Employees" Offline="true" CrossDomain="true" Adaptor="Syncfusion.Blazor.Adaptors.WebApiAdaptor"></SfDataManager>
<AutoCompleteFieldSettings Value="FirstName"/>
</SfAutoComplete>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="control-wrapper">
<label class="example-label">Observable Collection</label>
<SfAutoComplete TValue="string" TItem="Colors" PopupHeight="200px" Placeholder="Select a color" DataSource="@ColorsData" @bind-Value="@ColorValue">
<AutoCompleteFieldSettings Value="Color"/>
</SfAutoComplete>
</div>
</div>
<div class="col-lg-6">
<div class="control-wrapper">
<label class="example-label">ExpandoObject</label>
<SfAutoComplete TItem="ExpandoObject" TValue="string" PopupHeight="200px" Placeholder="Select a vehicle" DataSource="@VehicleData" @bind-Value="@VehicleValue">
<AutoCompleteFieldSettings Value="Text"/>
</SfAutoComplete>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="control-wrapper">
<label class="example-label">DynamicObject</label>
<SfAutoComplete TValue="string" TItem="DynamicDictionary" PopupHeight="200px" Placeholder="Select a name" DataSource="@Orders" @bind-Value="@AutoValue">
<AutoCompleteFieldSettings Value="CustomerName"/>
</SfAutoComplete>
</div>
</div>
</div>
</div>
@code{
public string CountryValue { get; set; } = "France";
public string EmployeeValue { get; set; } = "Andrew";
public string ColorValue { get; set; } = "Chocolate";
public string VehicleValue { get; set; } = "Ferrari LaFerrari";
public string AutoValue { get; set; } = "Michael";
public class Countries
{
public string? Name { get; set; }
public string? Code { get; set; }
}
public List<Countries> Country = new List<Countries>
{
new Countries() { Name = "Australia", Code = "AU" },
new Countries() { Name = "Bermuda", Code = "BM" },
new Countries() { Name = "Canada", Code = "CA" },
new Countries() { Name = "Cameroon", Code = "CM" },
new Countries() { Name = "Denmark", Code = "DK" },
new Countries() { Name = "France", Code = "FR" },
new Countries() { Name = "Finland", Code = "FI" },
new Countries() { Name = "Germany", Code = "DE" },
new Countries() { Name = "Greenland", Code = "GL" },
new Countries() { Name = "Hong Kong", Code = "HK" },
new Countries() { Name = "India", Code = "IN" },
new Countries() { Name = "Italy", Code = "IT" },
new Countries() { Name = "Japan", Code = "JP" },
new Countries() { Name = "Mexico", Code = "MX" },
new Countries() { Name = "Norway", Code = "NO" },
new Countries() { Name = "Poland", Code = "PL" },
new Countries() { Name = "Switzerland", Code = "CH" },
new Countries() { Name = "United Kingdom", Code = "GB" },
new Countries() { Name = "United States", Code = "US" }
};
public Query RemoteQuery = new Query();
public class EmployeeData
{
public int EmployeeID { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Country { get; set; }
}
public class Colors
{
public string? Code { get; set; }
public string? Color { get; set; }
}
private ObservableCollection<Colors> ColorsData = new ObservableCollection<Colors>()
{
new Colors() { Color = "Chocolate", Code = "#75523C" },
new Colors() { Color = "Cadet Blue", Code = "#3B8289" },
new Colors() { Color = "Dark Orange", Code = "#FF843D" },
new Colors() { Color = "Dark Red", Code = "#CA3832"},
new Colors() { Color = "Fuchsia", Code = "#D44FA3" },
new Colors() { Color = "Hot Pink", Code = "#F23F82" },
new Colors() { Color = "Indigo", Code = "#2F5D81" },
new Colors() { Color = "Lime Green", Code = "#4CD242" },
new Colors() { Color = "Orange Red", Code = "#FE2A00" },
new Colors() { Color = "Tomato", Code = "#FF745C" },
new Colors() { Color = "Brown", Code = "#A52A2A" },
new Colors() { Color = "Maroon", Code = "#800000" },
new Colors() { Color = "Green", Code = "#008000" },
new Colors() { Color = "Pink", Code = "#FFC0CB" },
new Colors() { Color = "Purple", Code = "#800080" }
};
public List<ExpandoObject> VehicleData { get; set; } = new List<ExpandoObject>();
public List<DynamicDictionary> Orders = new List<DynamicDictionary>() { };
protected override void OnInitialized()
{
VehicleData = Enumerable.Range(1, 15).Select((x) =>
{
dynamic d = new ExpandoObject();
d.ID = (1000 + x).ToString();
d.Text = (new string[] { "Hennessey Venom", "Bugatti Chiron", "Bugatti Veyron Super Sport", "SSC Ultimate Aero", "Koenigsegg CCR", "McLaren F1", "Aston Martin One- 77", "Jaguar XJ220", "McLaren P1", "Ferrari LaFerrari", "Mahindra Jaguar", "Hyundai Toyota", "Jeep Volkswagen", "Tata Maruti Suzuki", "Audi Mercedes Benz" }[x - 1]);
return d;
}).Cast<ExpandoObject>().ToList<ExpandoObject>();
Orders = Enumerable.Range(1, 15).Select((x) =>
{
dynamic d = new DynamicDictionary();
d.OrderID = 1000 + x;
d.CustomerName = (new string[] { "Nancy", "Andrew", "Janet", "Margaret", "Steven", "Michael", "Robert", "Anne", "Nige", "Fuller", "Dodsworth", "Leverling", "Callahan", "Suyama", "Davolio" }[x - 1]);
return d;
}).Cast<DynamicDictionary>().ToList<DynamicDictionary>();
}
public class DynamicDictionary : System.Dynamic.DynamicObject
{
Dictionary<string, object> dictionary = new Dictionary<string, object>();
public override bool TryGetMember(GetMemberBinder binder, out object? result)
{
string name = binder.Name;
return dictionary.TryGetValue(name, out result);
}
public override bool TrySetMember(SetMemberBinder binder, object? value)
{
if (value != null)
{
dictionary[binder.Name] = value;
}
return true;
}
//The GetDynamicMemberNames method of DynamicObject class must be overridden and return the property names to perform data operation and editing while using DynamicObject.
public override System.Collections.Generic.IEnumerable<string> GetDynamicMemberNames()
{
return this.dictionary.Keys;
}
}
}
<style>
.control-wrapper {
margin: 30px 40px;
}
.example-label {
font-size: 14px;
margin-bottom: 6px;
}
</style>