Skip to content

Commit 623a665

Browse files
svdimitrjivanova
authored andcommitted
docs(mc-combo): data binding article
1 parent 4495eee commit 623a665

File tree

1 file changed

+106
-99
lines changed

1 file changed

+106
-99
lines changed
Lines changed: 106 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,158 @@
11
---
22
title: Data Binding
3-
page_title: ComboBox - Data Binding
4-
description: Data Binding the ComboBox for Blazor.
5-
slug: components/combobox/databind
6-
tags: telerik,blazor,combobox,combo,data,bind,binding,databind
3+
page_title: MultiColumnComboBox - Data Binding
4+
description: Data Binding the MultiColumnComboBox for Blazor.
5+
slug: multicolumncombobox-data-binding
6+
tags: telerik,blazor,multicolumncombobox,combo,data,bind,binding,databind
77
published: True
88
position: 5
99
---
1010

1111
# ComboBox Data Binding
1212

13-
This article explains the different ways to provide data to a ComboBox component, the properties related to data binding and their results.
13+
This article explains how to provide data to the MultiColumnComboBox component, the properties related to data binding and their results.
1414

1515
@[template](/_contentTemplates/common/general-info.md#valuebind-vs-databind-link)
1616

17-
There are two key ways to bind data:
1817

19-
* [Primitive Types](#primitive-types)
20-
* [Model](#bind-to-a-model)
21-
22-
and some considerations you may find useful, such as showing the `Placeholder` when the value is out of the data source range:
18+
There are some considerations you may find useful, such as showing the `Placeholder` when the value is out of the data source range:
2319

2420
* [Considerations](#considerations)
2521
* [Value Out of Range](#value-out-of-range)
2622
* [Component Reference](#component-reference)
2723
* [Missing Value or Data](#missing-value-or-data)
2824

29-
## Primitive Types
30-
31-
You can data bind the ComboBox to a simple collection of data. When you have a concrete list of options for the user to choose from, their string representation is often suitable for display and you do not need special models.
32-
33-
To bind the combobox to a primitive type (like `int`, `string`, `double`), you need to
34-
35-
1. provide an `IEnumerable<TItem>` of the desired type to its `Data` property
36-
1. set a corresponding `Value`. If the `Value` is `null`, it will be populated with the first item from the data source.
37-
38-
>caption Data binding a ComboBox to a primitive type
39-
40-
````CSHTML
41-
@*Bind to a List of a primitive type (string, int,...)*@
42-
43-
<TelerikComboBox Data="@MyList" @bind-Value="MyItem">
44-
</TelerikComboBox>
45-
46-
@code {
47-
protected List<string> MyList = new List<string>() { "first", "second", "third" };
48-
49-
protected string MyItem { get; set; }
50-
51-
//Define a preselected value when the component initializes
52-
protected override void OnInitialized()
53-
{
54-
MyItem = "second";
55-
}
56-
}
57-
````
58-
5925
## Bind to a Model
6026

61-
You can bind the ComboBox to a model in your application. This is useful when you have a numerical representation of a finite list (for example, departments in a company), and you want the user to choose them based on a friendly text name.
27+
You can bind the MultiColumnComboBox to a model in your application. This is useful when you have a numerical representation of a finite list (for example, departments in a company), and you want the user to choose them based on a friendly text name.
6228

63-
To bind the ComboBox to a model:
29+
To bind the MultiColumnComboBox to a model:
6430

6531
1. populate its `Data` property with the collection of items you want in the dropdown
6632
1. set the `TextField` and `ValueField` properties to point to the corresponding names of the model
67-
1. set the `Value` property to the intial value of the model. If not set, it will be populated with the first item in the data source.
33+
1. set the `Value` property to the initial value of the model. If not set, it will be populated with the first item in the data source.
34+
1. define a list of [columns]({%slug multicolumncombobox-columns-overview%}) that will render in the dropdown
6835

69-
>caption Data binding a ComboBox to a model
36+
>note The MultiColumnComboBox must be bound to a collection of models. This is required because the columns cannot render properly if the component is bound to a collection of primitive types such as string and numbers.
7037
71-
````CSHTML
72-
@selectedValue
38+
>caption Data binding a MultiColumnComboBox to a model
7339
74-
<TelerikComboBox Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" @bind-Value="selectedValue">
75-
</TelerikComboBox>
40+
````CSHTML
41+
Selected value: @BoundValue
42+
<br />
43+
44+
<TelerikMultiColumnComboBox Data="@MultiComboData"
45+
@bind-Value="@BoundValue"
46+
ValueField="@nameof(SampleData.Id)"
47+
TextField="@nameof(SampleData.Name)">
48+
<MultiColumnComboBoxColumns>
49+
<MultiColumnComboBoxColumn Field="@nameof(SampleData.Id)" Title="The id" ></MultiColumnComboBoxColumn>
50+
<MultiColumnComboBoxColumn Field="@nameof(SampleData.Name)" Title="The name"></MultiColumnComboBoxColumn>
51+
</MultiColumnComboBoxColumns>
52+
</TelerikMultiColumnComboBox>
7653
7754
@code {
78-
//in a real case, the model is usually in a separate file
79-
//the model type and value field type must be provided to the dropdpownlist
80-
public class MyDdlModel
81-
{
82-
public int MyValueField { get; set; }
83-
public string MyTextField { get; set; }
84-
}
55+
public int BoundValue { get; set; }
8556
86-
int selectedValue { get; set; }
57+
public List<SampleData> MultiComboData { get; set; } = Enumerable.Range(0, 30).Select(x => new SampleData()
58+
{
59+
Id = x,
60+
Name = "Name " + x
61+
}).ToList();
8762
88-
//Define a preselected value when the component initializes
89-
protected override void OnInitialized()
63+
public class SampleData
9064
{
91-
selectedValue = 3;
65+
public int Id { get; set; }
66+
public string Name { get; set; }
9267
}
93-
94-
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
9568
}
9669
````
9770

9871
@[template](/_contentTemplates/common/get-model-from-dropdowns.md#get-model-from-dropdowns)
9972

10073
## Considerations
10174

102-
The ComboBox component attempts to infer the type of its model and value based on the provided `Data` and initial `Value`. This affects the way its [reference is obtained](#component-reference) and what happens [if you can't provide data or a value](#missing-value-or-data). Providing a [value that is not in the data source](#value-out-of-range) needs to be taken into account be the app, because the component will not change it.
75+
The MultiColumnCombobox component attempts to infer the type of its model and value based on the provided `Data` and initial `Value`. This affects the way its [reference is obtained](#component-reference) and what happens [if you can't provide data or a value](#missing-value-or-data). Providing a [value that is not in the data source](#value-out-of-range) needs to be taken into account be the application, because the component will not change it.
10376

10477
### Value Out of Range
10578

106-
This specific is applicable for the case when [custom value input]({%slug components/combobox/custom-value%}) is disabled (`AllowCustom="false"` which is its default value).
79+
This specific is applicable for the case when [custom value input]({%slug multicolumncombobox-custom-value%}) is disabled (`AllowCustom="false"` which is its default value).
10780

108-
When the `Value` the application provides does not match any of the values present in the `ValueField` of the `Data` collection, the ComboBox component will not change the `Value` or select a new item. In the common case, it will show up blank to indicate there is nothing selected from its data.
81+
When the `Value` the application provides does not match any of the values present in the `ValueField` of the `Data` collection, the MultiColumnCombobox component will not change the `Value` or select a new item. In the common case, it will show up blank to indicate there is nothing selected from its data.
10982

11083
If you have set the `Placeholder` and the `Value` matches the `default` value of the type (for example, `0` for an `int` or `null` for an `int?` or `string`), you will see the `Placeholder`. A `Value` that is non-`default` will not show the `Placeholder`.
11184

11285
Handling such "unexpected" values is up to the application - for example, through defensive checks, or through form validation, or by first checking what is present in the data source before setting a new `Value`.
11386

11487
When `AllowCustom="true"`, what the user types in the input will be set to the `Value` of the component regardless of the data source.
11588

116-
### Component Reference
89+
### Component Reference and Methods
11790

118-
The ComboBox is a generic component and its type comes from the model it is bound to and from the value field type. When bound to a primitive type, the reference is of that primitive type only.
91+
The TelerikMultiColumnComboBox is a generic component and its type comes from the model it is bound to and from the value field type.
11992

120-
<div class="skip-repl"></div>
121-
````Primitive
122-
@*Reference type when binding to primitive values*@
93+
#### Methods
12394

124-
<TelerikComboBox @ref="myComboRef" Data="@MyList" Value="@initialValue">
125-
</TelerikComboBox>
95+
The MultiColumnComboBox methods are accessible through it's reference.
12696

127-
@code {
128-
//the type of the generic component is determined by the type of the model you pass to it, and the type of its value field
129-
Telerik.Blazor.Components.TelerikComboBox<string, string> myComboRef;
97+
<style>
98+
article style + table {
99+
table-layout: auto;
100+
word-break: normal;
101+
}
102+
</style>
103+
| Method | Description |
104+
| --- | --- |
105+
| `Rebind` | Fires the [`OnRead`]({%slug multicolumncombobox-events%}#onread) event of the MultiColumnCombox. If you definded the event manually, calling the `Rebind` method will also execute the business logic in the OnRead event handler. |
106+
107+
108+
````CSHTML
109+
@* Get a reference to the MultiColumnComboBox and use the Rebind method *@
110+
111+
Selected value: @BoundValue
112+
<br />
113+
114+
<TelerikButton OnClick="@RebindMultiColumnComboBox">Rebind the Component</TelerikButton>
130115
131-
protected List<string> MyList = new List<string>() { "first", "second", "third" };
116+
<TelerikMultiColumnComboBox Data="@MultiComboData"
117+
@bind-Value="@BoundValue"
118+
ValueField="@nameof(SampleData.Id)"
119+
TextField="@nameof(SampleData.Name)" @ref="@MultiColumnComboReference">
120+
<MultiColumnComboBoxColumns>
121+
<MultiColumnComboBoxColumn Field="@nameof(SampleData.Id)" Title="The id"></MultiColumnComboBoxColumn>
122+
<MultiColumnComboBoxColumn Field="@nameof(SampleData.Name)" Title="The name"></MultiColumnComboBoxColumn>
123+
</MultiColumnComboBoxColumns>
124+
</TelerikMultiColumnComboBox>
132125
133-
string initialValue { get; set; }
126+
@code {
127+
private TelerikMultiColumnComboBox<SampleData, int> MultiColumnComboReference { get; set; }
134128
135-
//Define a preselected value when the component initializes
136-
protected override void OnInitialized()
129+
private void RebindMultiColumnComboBox()
137130
{
138-
initialValue = "third";
131+
var itemToBeAdded = new SampleData()
132+
{
133+
Id = 100,
134+
Name = "Added From Code"
135+
};
136+
137+
MultiComboData.Add(itemToBeAdded);
138+
139+
BoundValue = 100;
140+
141+
MultiColumnComboReference.Rebind();
139142
}
140-
}
141-
````
142-
````Model
143-
@*Reference when binding to model collections*@
144143
145-
<TelerikComboBox @ref="@myComboRef" Data="@myComboData" TextField="MyTextField" ValueField="MyValueField" Value="3">
146-
</TelerikComboBox>
147-
@code {
148-
//the type of the generic component is determined by the type of the model you pass to it, and the type of its value field
149-
Telerik.Blazor.Components.TelerikComboBox<MyDdlModel, int> myComboRef;
144+
public int BoundValue { get; set; }
150145
151-
IEnumerable<MyDdlModel> myComboData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
146+
public List<SampleData> MultiComboData { get; set; } = Enumerable.Range(0, 30).Select(x => new SampleData()
147+
{
148+
Id = x,
149+
Name = "Name " + x
150+
}).ToList();
152151
153-
public class MyDdlModel
152+
public class SampleData
154153
{
155-
public int MyValueField { get; set; }
156-
public string MyTextField { get; set; }
154+
public int Id { get; set; }
155+
public string Name { get; set; }
157156
}
158157
}
159158
````
@@ -162,13 +161,21 @@ The ComboBox is a generic component and its type comes from the model it is boun
162161

163162
In case you cannot provide either of a `Value`, or `Data`, or both when the component initializes, you need to set the corresponding type properties to the `TItem` and `TValue` properties as shown below.
164163

165-
>caption ComboBox configuration if you cannot provide Value or Data
164+
>caption MultiColumnComboBox configuration if you cannot provide Value or Data
166165
167166
````CSHTML
168-
@*How to declare the combobox if no Value or Data are provided*@
169-
170-
<TelerikComboBox Data="@myComboData" TextField="MyTextField" ValueField="MyValueField" TValue="int" TItem="MyDdlModel">
171-
</TelerikComboBox>
167+
@*How to declare the MultiColumnComboBox if no Value or Data are provided*@
168+
169+
<TelerikMultiColumnComboBox Data="@myComboData"
170+
TextField="MyTextField"
171+
ValueField="MyValueField"
172+
TValue="int"
173+
TItem="MyDdlModel">
174+
<MultiColumnComboBoxColumns>
175+
<MultiColumnComboBoxColumn Field="@nameof(MyDdlModel.MyValueField)"></MultiColumnComboBoxColumn>
176+
<MultiColumnComboBoxColumn Field="@nameof(MyDdlModel.MyTextField)"></MultiColumnComboBoxColumn>
177+
</MultiColumnComboBoxColumns>
178+
</TelerikMultiColumnComboBox>
172179
173180
@code {
174181
public class MyDdlModel //TItem matches the type of the model
@@ -186,5 +193,5 @@ The ComboBox is a generic component and its type comes from the model it is boun
186193

187194
## See Also
188195

189-
* [ComboBox Overview]({%slug components/combobox/overview%})
190-
* [Live Demo: ComboBox](https://demos.telerik.com/blazor-ui/combobox/overview)
196+
* [ComboBox Overview]({%slug multicolumncombobox-overview%})
197+
* [Live Demo: ComboBox](https://demos.telerik.com/blazor-ui/multicolumncombobox/overview)

0 commit comments

Comments
 (0)