Skip to content

Commit 9d781ba

Browse files
Merge dimodi-patch-1-jan13-2698 into production (#2702)
* docs(common): Enhance common Rebind() example * Update onread.md --------- Co-authored-by: Dimo Dimov <961014+dimodi@users.noreply.github.com>
1 parent 7d51770 commit 9d781ba

File tree

2 files changed

+51
-46
lines changed

2 files changed

+51
-46
lines changed

common-features/data-binding/onread.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,13 @@ Also check [how to rebind and refresh a component with a `Timer`](slug://common-
220220
````RAZOR
221221
@using Telerik.DataSource.Extensions
222222
223-
<TelerikDropDownList @ref="@TheDropDown"
223+
<TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary"
224+
OnClick="@RebindComponents">Rebind Components</TelerikButton>
225+
226+
<br />
227+
<br />
228+
229+
<TelerikDropDownList @ref="@DropDownListRef"
224230
TItem="@SampleModel"
225231
TValue="@int"
226232
OnRead="@OnDropDownRead"
@@ -236,13 +242,10 @@ Also check [how to rebind and refresh a component with a `Timer`](slug://common-
236242
</ItemTemplate>
237243
</TelerikDropDownList>
238244
239-
<TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary"
240-
OnClick="@RebindComponents">Rebind Components</TelerikButton>
241-
242245
<br />
243246
<br />
244247
245-
<TelerikGrid @ref="@TheGrid"
248+
<TelerikGrid @ref="@GridRef"
246249
TItem="@SampleModel"
247250
OnRead="@OnGridRead"
248251
AutoGenerateColumns="true"
@@ -251,34 +254,34 @@ Also check [how to rebind and refresh a component with a `Timer`](slug://common-
251254
PageSize="5" />
252255
253256
@code {
254-
TelerikGrid<SampleModel> TheGrid { get; set; }
255-
TelerikDropDownList<SampleModel, int> TheDropDown { get; set; }
257+
private TelerikGrid<SampleModel>? GridRef { get; set; }
258+
private TelerikDropDownList<SampleModel, int>? DropDownListRef { get; set; }
256259
257-
List<SampleModel> GridData { get; set; }
258-
List<SampleModel> DropDownData { get; set; }
260+
private List<SampleModel> GridData { get; set; } = new();
261+
private List<SampleModel> DropDownData { get; set; } = new();
259262
260-
int DropDownValue { get; set; } = 1;
263+
private int DropDownValue { get; set; } = 1;
261264
262-
int ItemCounter { get; set; } = 3;
265+
private int ItemCounter { get; set; } = 3;
263266
264-
void RebindComponents()
267+
private void RebindComponents()
265268
{
266-
GenerateData(); // simulate change in the data
269+
GenerateData(); // simulate data change
267270
268-
TheGrid.Rebind();
269-
TheDropDown.Rebind();
271+
GridRef?.Rebind();
272+
DropDownListRef?.Rebind();
270273
}
271274
272-
async Task OnGridRead(GridReadEventArgs args)
275+
private async Task OnGridRead(GridReadEventArgs args)
273276
{
274-
var result = GridData.ToDataSourceResult(args.Request);
277+
var result = await GridData.ToDataSourceResultAsync(args.Request);
275278
args.Data = result.Data;
276279
args.Total = result.Total;
277280
}
278281
279-
async Task OnDropDownRead(DropDownListReadEventArgs args)
282+
private async Task OnDropDownRead(DropDownListReadEventArgs args)
280283
{
281-
var result = DropDownData.ToDataSourceResult(args.Request);
284+
var result = await DropDownData.ToDataSourceResultAsync(args.Request);
282285
args.Data = result.Data;
283286
args.Total = result.Total;
284287
}
@@ -290,17 +293,15 @@ Also check [how to rebind and refresh a component with a `Timer`](slug://common-
290293
base.OnInitialized();
291294
}
292295
293-
void GenerateData()
296+
private void GenerateData()
294297
{
295298
GridData = new List<SampleModel>();
296299
DropDownData = new List<SampleModel>();
297300
298-
var rnd = new Random();
299-
300301
for (int i = 1; i <= ItemCounter; i++)
301302
{
302-
GridData.Add(new SampleModel() { Id = i, Text = $"Text {rnd.Next(1, 100)}" });
303-
DropDownData.Add(new SampleModel() { Id = i, Text = $"Text {rnd.Next(1, 100)}" });
303+
GridData.Add(new SampleModel() { Id = i, Text = $"Text {(char)Random.Shared.Next(65, 91)}{(char)Random.Shared.Next(65, 91)}" });
304+
DropDownData.Add(new SampleModel() { Id = i, Text = $"Text {(char)Random.Shared.Next(65, 91)}{(char)Random.Shared.Next(65, 91)}" });
304305
}
305306
306307
ItemCounter++;
@@ -309,7 +310,7 @@ Also check [how to rebind and refresh a component with a `Timer`](slug://common-
309310
public class SampleModel
310311
{
311312
public int Id { get; set; }
312-
public string Text { get; set; }
313+
public string Text { get; set; } = string.Empty;
313314
}
314315
}
315316
````

common-features/data-binding/overview.md

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,31 +115,36 @@ Thus, you will usually need to create a new reference for `Data` value in order
115115
>caption Call `Rebind()` or create new Data reference
116116
117117
````RAZOR
118-
<p>
119-
<TelerikButton OnClick="@RefreshGridData">Refresh Grid Data</TelerikButton>
120-
</p>
121-
122118
<TelerikGrid @ref="@GridRef"
123119
Data="@GridData"
124-
AutoGenerateColumns="true" />
120+
AutoGenerateColumns="true">
121+
<GridToolBarTemplate>
122+
<TelerikButton ThemeColor="@ThemeConstants.Button.ThemeColor.Primary"
123+
OnClick="@RefreshGridData">Modify Grid Data And Refresh</TelerikButton>
124+
</GridToolBarTemplate>
125+
</TelerikGrid>
125126
126127
@code {
127-
TelerikGrid<SampleModel> GridRef { get; set; }
128-
List<SampleModel> GridData { get; set; }
128+
private TelerikGrid<SampleModel>? GridRef { get; set; }
129+
130+
private List<SampleModel> GridData { get; set; } = new();
131+
132+
private int LastId { get; set; }
129133
130-
void RefreshGridData()
134+
private void RefreshGridData()
131135
{
132-
var newId = GridData.Count + 1;
136+
GridData.RemoveAt(0);
133137
134-
GridData.FirstOrDefault().Text = DateTime.Now.Ticks.ToString();
138+
GridData.ElementAt(Random.Shared.Next(0, GridData.Count)).Text = DateTime.Now.Ticks.ToString();
135139
136-
GridData.Add(new SampleModel() {
137-
Id = newId,
138-
Text = "Text " + newId
140+
GridData.Add(new SampleModel()
141+
{
142+
Id = ++LastId,
143+
Text = "Text " + LastId
139144
});
140145
141146
// Call Rebind...
142-
GridRef.Rebind();
147+
GridRef?.Rebind();
143148
144149
// ...OR...
145150
@@ -152,13 +157,12 @@ Thus, you will usually need to create a new reference for `Data` value in order
152157
153158
protected override void OnInitialized()
154159
{
155-
GridData = new List<SampleModel>();
156-
157-
for (int i = 1; i <= 3; i++)
160+
for (int i = 1; i <= 5; i++)
158161
{
159-
GridData.Add(new SampleModel() {
160-
Id = i,
161-
Text = "Text " + i
162+
GridData.Add(new SampleModel()
163+
{
164+
Id = ++LastId,
165+
Text = $"Text {LastId}"
162166
});
163167
}
164168
@@ -168,7 +172,7 @@ Thus, you will usually need to create a new reference for `Data` value in order
168172
public class SampleModel
169173
{
170174
public int Id { get; set; }
171-
public string Text { get; set; }
175+
public string Text { get; set; } = string.Empty;
172176
}
173177
}
174178
````

0 commit comments

Comments
 (0)