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
221 lines (208 loc) · 11.9 KB
/
DataBinding.razor
File metadata and controls
221 lines (208 loc) · 11.9 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
@page "/MultiSelect-Dropdown/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 MultiSelect's data binding support. Select an item from the suggestion list by clicking the MultiSelect component. When you first click on the remote data MultiSelect, the loader icon will appear until the remote request retrieves the data from the server and displays it.</p>
</SampleDescription>
<ActionDescription>
<p>The MultiSelect loads the data either from local data sources or remote data services through the <a href="https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.DropDowns.MultiSelectModel-1.html#Syncfusion_Blazor_DropDowns_MultiSelectModel_1_DataSource" target="_blank" aria-label="Class reference of DataSource property in MultiSelect Dropdown"> 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 MultiSelect 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 reference 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/" aria-label="Blazor Data NuGet Package">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 MultiSelect is bound to a collection of sports data.</li>
<li>Remote Data MultiSelect is bound to a collection of customer data using <code>DataManager</code>.</li>
<li>Observable Collection MultiSelect is bound to a <code>ObservableCollection</code> of colors data.</li>
<li>ExpandObject MultiSelect is bound to the collection of vehicles data of type <code>ExpandObject</code>.</li>
<li>DynamicObject MultiSelect 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/multiselect-dropdown/data-source" target="_blank">Blazor MultiSelect - Data Binding</a></li>
<li><a href="https://blazor.syncfusion.com/documentation/data/getting-started" target="_blank">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>
<SfMultiSelect TValue="string[]" TItem="Countries" Placeholder="Select countries" DataSource="@country" @bind-Value="@CountryValue">
<MultiSelectFieldSettings Text="Name" Value="Code"></MultiSelectFieldSettings>
</SfMultiSelect>
</div>
</div>
<div class="col-lg-6">
<div class="control-wrapper">
<label class="example-label">Remote Data</label>
<SfMultiSelect TValue="int?[]" TItem="EmployeeData" Placeholder="Select employees" Query="@RemoteQuery" @bind-Value="@EmployeeValue">
<SfDataManager Url="https://blazor.syncfusion.com/services/production/api/Employees" Adaptor="@Syncfusion.Blazor.Adaptors.WebApiAdaptor" CrossDomain=true></SfDataManager>
<MultiSelectFieldSettings Text="FirstName" Value="EmployeeId"></MultiSelectFieldSettings>
</SfMultiSelect>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="control-wrapper">
<label class="example-label">Observable Collection</label>
<SfMultiSelect TValue="string[]" TItem="Colors" PopupHeight="230px" Placeholder="Select colors" DataSource="@ColorsData" @bind-Value="@ColorValue">
<MultiSelectFieldSettings Text="Color" Value="Code"></MultiSelectFieldSettings>
</SfMultiSelect>
</div>
</div>
<div class="col-lg-6">
<div class="control-wrapper">
<label class="example-label">ExpandoObject</label>
<SfMultiSelect TItem="ExpandoObject" TValue="string[]" PopupHeight="230px" Placeholder="Select vehicles" DataSource="@VehicleData" @bind-Value="@VehicleValue">
<MultiSelectFieldSettings Text="Text" Value="ID"></MultiSelectFieldSettings>
</SfMultiSelect>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="control-wrapper">
<label class="example-label">DynamicObject</label>
<SfMultiSelect TValue="string[]" TItem="DynamicDictionary" Placeholder="Select customers" DataSource="@Orders" @bind-Value="@MultiValue">
<MultiSelectFieldSettings Text="CustomerName" Value="CustomerName"></MultiSelectFieldSettings>
</SfMultiSelect>
</div>
</div>
</div>
</div>
@code{
public string[] CountryValue { get; set; } = { "FR", "DE" };
public int?[] EmployeeValue { get; set; } = { 1, 2 };
public string[] ColorValue { get; set; } = { "#75523C", "#FE2A00"};
public string[] VehicleValue { get; set; } = { "1002", "1010"};
public string[] MultiValue { get; set; } = { "Michael", "Robert"};
public class Countries
{
public string? Name { get; set; }
public string? Code { get; set; }
}
private 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? Designation { 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>