Skip to content

Commit 83f7734

Browse files
committed
Dropdown default text (#213)
1 parent b1b011d commit 83f7734

File tree

9 files changed

+113
-68
lines changed

9 files changed

+113
-68
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#get-model-from-dropdowns
2+
>tip If you are looking for more fields from the view-model that describes the dropdown items, not just the `Value`, see the [Get model from dropodwn]({%slug dropdowns-get-model%}) KB article and the [OnChange](events#onchange) event.
3+
>
4+
> You may also want to review/join the discussion and Vote for this request: <a href="https://www.telerik.com/forums/binding-dropdownlist-value-to-complex-model" target="_blank">Binding DropDownList Value to complex model</a>
5+
#end

components/autocomplete/data-bind.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ There are two key ways to bind data:
2121

2222
There are also some [considerations](#considerations) to keep in mind.
2323

24+
@[template](/_contentTemplates/common/get-model-from-dropdowns.md#get-model-from-dropdowns)
25+
2426
## Primitive Types
2527

2628
You can data bind the AutoComplete to a simple collection of `string` 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.
@@ -131,6 +133,7 @@ The AutoComplete component is generic and its type depends on the type of the mo
131133
}
132134
````
133135

136+
134137
### Missing Data
135138

136139
The AutoComplete is, essentially, a textbox. This means that its `Value` is always a string and it is up to you to bind and/or use it. The `Data` parameter, however, is required for the functionality of the component, and it must never be `null`. If there are no suggestions that you wish to provide to the user, consider using a regular TextBox, or creating an empty collection.

components/autocomplete/overview.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ User input: @TheValue
2929
Placeholder="Enter your role (can be free text)" ClearButton="true" />
3030
3131
@code{
32+
//Current value is null (no item is sellected) which allows the Placeholder to be displayed.
3233
string TheValue { get; set; }
3334
3435
List<string> Suggestions { get; set; } = new List<string> {
@@ -69,7 +70,7 @@ The AutoComplete is a generic component and its type is determined by the type o
6970

7071
* `MinLength` - how many characters the text has to be before the suggestions list appears. Cannot be `0`. Often works together with [filtering]({%slug autocomplete-filter%}).
7172

72-
* `Placeholder` - the text the user sees as a hint when there is no text in the input.
73+
* `Placeholder` - the text the user sees as a hint when there is no text in the input. In order for it to be shown, the `Value` parameter should be set to the default value for string (`null`).
7374

7475
* `PopupHeight` - the height of the expanded dropdown list element.
7576

@@ -87,6 +88,7 @@ The AutoComplete is a generic component and its type is determined by the type o
8788

8889
* Validation - see the [Input Validation]({%slug common-features/input-validation%}) article for more details.
8990

91+
@[template](/_contentTemplates/common/get-model-from-dropdowns.md#get-model-from-dropdowns)
9092

9193
## See Also
9294

components/combobox/data-bind.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,13 @@ To bind the combobox to a primitive type (like `int`, `string`, `double`), you n
4646
@code {
4747
protected List<string> MyList = new List<string>() { "first", "second", "third" };
4848
49-
protected string MyItem { get; set; } = "second";
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+
}
5056
}
5157
````
5258

@@ -77,12 +83,20 @@ To bind the ComboBox to a model:
7783
public string MyTextField { get; set; }
7884
}
7985
80-
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
86+
int selectedValue { get; set; }
87+
88+
//Define a preselected value when the component initializes
89+
protected override void OnInitialized()
90+
{
91+
selectedValue = 3;
92+
}
8193
82-
int selectedValue { get; set; } = 3; //usually the current value should come from the view model data
94+
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
8395
}
8496
````
8597

98+
@[template](/_contentTemplates/common/get-model-from-dropdowns.md#get-model-from-dropdowns)
99+
86100
## Considerations
87101

88102
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.
@@ -115,7 +129,13 @@ The ComboBox is a generic component and its type comes from the model it is boun
115129
116130
protected List<string> MyList = new List<string>() { "first", "second", "third" };
117131
118-
string initialValue { get; set; } = "third";
132+
string initialValue { get; set; }
133+
134+
//Define a preselected value when the component initializes
135+
protected override void OnInitialized()
136+
{
137+
initialValue = "third";
138+
}
119139
}
120140
````
121141
````Model

components/combobox/overview.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ Selected value: @selectedValue
3333
@code {
3434
IEnumerable<MyDdlModel> myComboData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
3535
36-
int selectedValue { get; set; } = 3; //usually the current value should come from the model data
36+
int selectedValue { get; set; }
37+
38+
//Define a preselected value when the component initializes. Placeholder will not be shown as the selected value is defined.
39+
protected override void OnInitialized()
40+
{
41+
selectedValue = 3;
42+
}
3743
3844
//in a real case, the model is usually in a separate file
3945
//the model type and value field type must be provided to the dropdpownlist
@@ -75,7 +81,7 @@ The ComboBox is a generic component and its type is determined by the type of th
7581

7682
* `Id` - renders as the `id` attribute on the `<input />` element, so you can attach a `<label for="">` to the input.
7783

78-
* `Placeholder` - the text the user sees as a hint when no item is selected (the `Value` is `null` or an empty string).
84+
* `Placeholder` - the text the user sees as a hint when no item is selected. In order for it to be shown, the `Value` parameter should be set to a default value depending on the type defined in the `ValueField` parameter. For example, `0` for an `int`, and `null` for an `int?` or `string`. You need to make sure that it does not match the value of an existing item in the data source.
7985

8086
* `PopupHeight` - the height of the expanded dropdown list element.
8187

@@ -129,10 +135,7 @@ Missing selection is most common when the initial value is `null` as data source
129135
| No match | No item is selected. `Value` is updated to the custom one. | No item is selected. `Value` is updated to `default(typeof(Value))`. The `OnChange` event does not fire for the value clearing. |
130136

131137

132-
>tip If you are looking for more fields from the view-model that describes the dropdown items, not just the `Value`, see the [Get model from dropodwn]({%slug dropdowns-get-model%}) KB article and the [OnChange](events#onchange) event.
133-
>
134-
> You may also want to review/join the discussion and Vote for this request: <a href="https://www.telerik.com/forums/binding-dropdownlist-value-to-complex-model" target="_blank">Binding DropDownList Value to complex model</a>
135-
138+
@[template](/_contentTemplates/common/get-model-from-dropdowns.md#get-model-from-dropdowns)
136139

137140
## See Also
138141

components/dropdownlist/data-bind.md

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ There are two key ways to bind data:
2121

2222
and some considerations you may find useful, such as showing the `DefaultText` when the value is out of the data source range:
2323

24-
* [Considerations](#considerations)
25-
* [Value Out of Range](#value-out-of-range)
26-
* [Component Reference](#component-reference)
27-
* [Missing Value or Data](#missing-value-or-data)
24+
* [Considerations](#considerations)
25+
* [Value Out of Range](#value-out-of-range)
26+
* [Component Reference](#component-reference)
27+
* [Missing Value or Data](#missing-value-or-data)
2828

2929
## Primitive Types
3030

@@ -44,9 +44,15 @@ Bind to a List of a primitive type (stirng, int,...)
4444
</TelerikDropDownList>
4545
4646
@code {
47-
protected List<string> MyList = new List<string>() { "first", "second", "third" };
47+
protected List<string> MyList = new List<string>() { "first", "second", "third" };
4848
49-
protected string MyItem { get; set; } = "second";
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+
}
5056
}
5157
````
5258

@@ -69,20 +75,28 @@ Bind to a collection of models
6975
</TelerikDropDownList>
7076
7177
@code {
72-
//in a real case, the model is usually in a separate file
73-
//the model type and value field type must be provided to the dropdpownlist
74-
public class MyDdlModel
75-
{
76-
public int MyValueField { get; set; }
77-
public string MyTextField { get; set; }
78-
}
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+
}
7985
80-
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
86+
int selectedValue { get; set; }
87+
88+
//Define a preselected value when the component initializes
89+
protected override void OnInitialized()
90+
{
91+
selectedValue = 3;
92+
}
8193
82-
int selectedValue { get; set; } = 3; //usually the current value should come from the view model data
94+
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
8395
}
8496
````
8597

98+
@[template](/_contentTemplates/common/get-model-from-dropdowns.md#get-model-from-dropdowns)
99+
86100
## Considerations
87101

88102
The DropDownList 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.
@@ -109,9 +123,15 @@ Reference type when binding to primitive values
109123
//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
110124
Telerik.Blazor.Components.TelerikDropDownList<string, string> myDdlRef;
111125
112-
protected List<string> MyList = new List<string>() { "first", "second", "third" };
126+
protected List<string> MyList = new List<string>() { "first", "second", "third" };
127+
128+
string initialValue { get; set; }
113129
114-
string initialValue {get;set;} = "third";
130+
//Define a preselected value when the component initializes
131+
protected override void OnInitialized()
132+
{
133+
initialValue = "third";
134+
}
115135
}
116136
````
117137
````Model

components/dropdownlist/overview.md

Lines changed: 28 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ Selected value: @selectedValue
2929
</TelerikDropDownList>
3030
3131
@code {
32-
//in a real case, the model is usually in a separate file
33-
//the model type and value field type must be provided to the dropdpownlist
34-
public class MyDdlModel
35-
{
36-
public int MyValueField { get; set; }
37-
public string MyTextField { get; set; }
38-
}
32+
//in a real case, the model is usually in a separate file
33+
//the model type and value field type must be provided to the dropdpownlist
34+
public class MyDdlModel
35+
{
36+
public int MyValueField { get; set; }
37+
public string MyTextField { get; set; }
38+
}
3939
40-
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
40+
int selectedValue { get; set; }
4141
42-
int selectedValue { get; set; } = 3; //usually the current value should come from the model data
42+
//Define a preselected value when the component initializes
43+
protected override void OnInitialized()
44+
{
45+
selectedValue = 3;
46+
}
47+
48+
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
4349
}
4450
````
4551

@@ -61,7 +67,7 @@ The DropDownList provides the following features:
6167

6268
* `Data` - allows you to provide the data source. Required.
6369

64-
* `DefaultText` - sets the hint that is shown if the `Value` has the `default` value for the type of the `ValueField`. For example, `0` for an `int`, and `null` for an `int?` or `string`. You need to make sure that it does not match the value of an existing item in the data source. You can find examples in the [Examples section](#examples) in this article and in the [Input Validation]({%slug common-features/input-validation%}#dropdownlist) article.
70+
* `DefaultText` - simple hint to be displayed when no item is selected yet. In order for it to be shown, the `Value` parameter should be set to a default value depending on the type defined in the `ValueField` parameter. For example, `0` for an `int`, and `null` for an `int?` or `string`. You need to make sure that it does not match the value of an existing item in the data source. See the first example in the [Examples section](#examples) in this article and in the [Input Validation]({%slug common-features/input-validation%}#dropdownlist) article.
6571

6672
* `Enabled` - whether the component is enabled.
6773

@@ -97,6 +103,15 @@ The DropDownList provides the following features:
97103
* Validation - see the [Input Validation]({%slug common-features/input-validation%}) article for more details.
98104

99105

106+
## Selected Item and DefaultText
107+
108+
By default, if no `Value` is provided and no `DefaultText` is defined, the DropDownList will appear empty.
109+
110+
* To display `DefaultText` - `Value` should be `0` or `null` depending on the data type you are using in the `ValueField` and the `DefaultText` should be defined.
111+
112+
* To display a selected item when the component renders - provide the `Value` of the desired element. Note that it must match an item of the component's data source.
113+
114+
100115
## Examples
101116

102117
>caption Default text (hint) to show when no actual item is selected
@@ -116,41 +131,17 @@ The DropDownList provides the following features:
116131
@code {
117132
protected List<string> MyStringList = new List<string>() { "first", "second", "third" };
118133
134+
//Current value is null (no item is sellected) which allows the DefaultText to be displayed.
119135
protected string MyStringItem { get; set; }
120136
121137
protected List<int> MyIntList = new List<int>() { 1, 2, 3 };
122138
139+
//Current value is 0 (no item is sellected) which allows the DefaultText to be displayed.
123140
protected int MyIntItem { get; set; }
124141
}
125142
````
126143

127144

128-
129-
>caption Show default item only when there is no selection by toggling the DefaultText parameter value depending on your business logic
130-
131-
````CSHTML
132-
Selected value: @selectedValue
133-
<br />
134-
135-
<TelerikDropDownList Data="@myDdlData" TextField="MyTextField" ValueField="MyValueField" @bind-Value="selectedValue"
136-
DefaultText="@( selectedValue == 0 ? "Please Select" : null )">
137-
</TelerikDropDownList>
138-
139-
@code {
140-
int selectedValue { get; set; }
141-
142-
IEnumerable<MyDdlModel> myDdlData = Enumerable.Range(1, 20).Select(x => new MyDdlModel { MyTextField = "item " + x, MyValueField = x });
143-
144-
public class MyDdlModel
145-
{
146-
public int MyValueField { get; set; }
147-
public string MyTextField { get; set; }
148-
}
149-
}
150-
````
151-
152-
153-
154145
>caption Get selected item from external code
155146
156147
````CSHTML
@@ -193,10 +184,7 @@ Selected value: @selectedValue
193184
````
194185

195186

196-
>tip If you are looking for more fields from the view-model that describes the dropdown items, not just the `Value`, see the [Get model from dropodwn]({%slug dropdowns-get-model%}) KB article and the [OnChange](events#onchange) event.
197-
>
198-
> You may also want to review/join the discussion and Vote for this request: <a href="https://www.telerik.com/forums/binding-dropdownlist-value-to-complex-model" target="_blank">Binding DropDownList Value to complex model</a>
199-
187+
@[template](/_contentTemplates/common/get-model-from-dropdowns.md#get-model-from-dropdowns)
200188

201189

202190
## See Also

components/multiselect/data-bind.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ To bind the MultiSelect to a model:
124124
}
125125
````
126126

127+
@[template](/_contentTemplates/common/get-model-from-dropdowns.md#get-model-from-dropdowns)
128+
127129
## Considerations
128130

129131
The MultiSelect 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](#reference) and what happens [if you can't provide data or a value](#missing-value-or-data).

components/multiselect/overview.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ The MultiSelect is a generic component and its type is determined by the type of
190190
}
191191
````
192192

193+
@[template](/_contentTemplates/common/get-model-from-dropdowns.md#get-model-from-dropdowns)
194+
193195
## See Also
194196

195197
* [Data Binding]({%slug multiselect-databind%})

0 commit comments

Comments
 (0)