|
| 1 | +--- |
| 2 | +title: Overview |
| 3 | +page_title: Loader Overview |
| 4 | +description: Overview of the Loading indicator for Blazor. |
| 5 | +slug: loader-overview |
| 6 | +tags: telerik,blazor,loader,overview |
| 7 | +published: True |
| 8 | +position: 0 |
| 9 | +--- |
| 10 | + |
| 11 | +# Loader Overview |
| 12 | + |
| 13 | +This article provides information about the Blazor Loader component and its core features. |
| 14 | + |
| 15 | +The Loader component provides an animated indicator that you can use to show your users that the app is working on something and they should wait. |
| 16 | + |
| 17 | +## Basic Loading Indicator |
| 18 | + |
| 19 | +To add a Telerik Loader to your Blazor app, use the `<TelerikLoader>` tag and show it when needed by your app. You can also control its [visual appearance]({%slug loader-appearance%}) through parameters. |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +````CSHTML |
| 24 | +@if (IsLoading) |
| 25 | +{ |
| 26 | + <TelerikLoader /> |
| 27 | +} |
| 28 | +else |
| 29 | +{ |
| 30 | + @Data |
| 31 | +} |
| 32 | +
|
| 33 | +@code { |
| 34 | + public bool IsLoading { get; set; } |
| 35 | + public string Data { get; set; } |
| 36 | +
|
| 37 | + protected override async Task OnInitializedAsync() |
| 38 | + { |
| 39 | + await LoadData(); |
| 40 | + } |
| 41 | +
|
| 42 | + async Task LoadData() |
| 43 | + { |
| 44 | + IsLoading = true; |
| 45 | + await Task.Delay(2000); |
| 46 | + IsLoading = false; |
| 47 | + Data = "Your data goes here"; |
| 48 | + } |
| 49 | +} |
| 50 | +```` |
| 51 | + |
| 52 | +## Visible Parameter |
| 53 | + |
| 54 | +You can control whether the indicator is shown through its `Visible` parameter. This can be useful for integrating it into other components and/or for shortening the razor syntax. |
| 55 | + |
| 56 | +>caption Loading indicator in a single click button |
| 57 | +
|
| 58 | + |
| 59 | + |
| 60 | +````CSHTML |
| 61 | +@* Toggling the Loader and the Enabled state of the button through a single flag while working lets you implement a single-click button with a loading indicator *@ |
| 62 | +
|
| 63 | +<TelerikButton Primary="true" OnClick="@GenerateReport" Enabled="@(!IsGeneratingReport)"> |
| 64 | + <TelerikLoader Visible="@IsGeneratingReport" ThemeColor="light"></TelerikLoader> |
| 65 | + @( IsGeneratingReport ? "Generating Report" : "Generate Report" ) |
| 66 | +</TelerikButton> |
| 67 | +
|
| 68 | +@code { |
| 69 | + public bool IsGeneratingReport { get; set; } |
| 70 | +
|
| 71 | + public async Task GenerateReport() |
| 72 | + { |
| 73 | + IsGeneratingReport = true; |
| 74 | + await Task.Delay(2000); // do actual work here |
| 75 | + IsGeneratingReport = false; |
| 76 | + } |
| 77 | +} |
| 78 | +```` |
| 79 | + |
| 80 | +## Sample Loading Panel |
| 81 | + |
| 82 | +The `TelerikLoader` component is an animated icon that use can use in your apps. Sometimes, however, you may want to overlay a lot of content with a panel to show the user something is happening with it and to prevent an interaction with it. For example, while fetching data for a grid. |
| 83 | + |
| 84 | +You can devise such a loading panel as needed by your app, and place a Telerik Loading Indicator inside to show the user something is happening. An example follows. |
| 85 | + |
| 86 | +>caption Sample Loading Panel used twice in bootstrap columns |
| 87 | +
|
| 88 | + |
| 89 | + |
| 90 | +````CSHTML |
| 91 | +@* for a real case - adjust styling as needed, and extract into a separate component and add the CSS rules to your site stylesheet *@ |
| 92 | +
|
| 93 | +<div class="container"> |
| 94 | + <div class="row"> |
| 95 | + <div class="col"> |
| 96 | + @if (isLoadingData) |
| 97 | + { |
| 98 | + @* sample loading panel, styles are at the end *@ |
| 99 | + <div class="loading-panel-wrapper"> |
| 100 | + <div class="loading-panel-overlay"></div> |
| 101 | + <TelerikLoader Size="@LoaderSize.Large" Type="@LoaderType.ConvergingSpinner" ThemeColor="@ThemeColors.Tertiary" /> |
| 102 | + </div> |
| 103 | + } |
| 104 | + <TelerikGrid Data="@GridData" Height="500px" Pageable="true" AutoGenerateColumns="true"> |
| 105 | + </TelerikGrid> |
| 106 | + </div> |
| 107 | + <div class="col"> |
| 108 | + @if (isLoadingData) |
| 109 | + { |
| 110 | + @* In a real app, you should make this a component and put its styles in the app stylesheet |
| 111 | + here we just copy it to showcase the concept of showing a loading sign *@ |
| 112 | + <div class="loading-panel-wrapper"> |
| 113 | + <div class="loading-panel-overlay"></div> |
| 114 | + <TelerikLoader Size="@LoaderSize.Large" Type="@LoaderType.ConvergingSpinner" ThemeColor="@ThemeColors.Tertiary" /> |
| 115 | + </div> |
| 116 | + } |
| 117 | + <p>some other content</p> |
| 118 | + <TelerikButton OnClick="@FetchData">Reload data</TelerikButton> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | +</div> |
| 122 | +
|
| 123 | +@code { |
| 124 | + bool isLoadingData { get; set; } |
| 125 | +
|
| 126 | + protected override async Task OnInitializedAsync() |
| 127 | + { |
| 128 | + await FetchData(); |
| 129 | + } |
| 130 | +
|
| 131 | + async Task FetchData() |
| 132 | + { |
| 133 | + Console.WriteLine("called"); |
| 134 | + isLoadingData = true; |
| 135 | + await Task.Delay(2000); // simulate long operation |
| 136 | + GridData = Enumerable.Range(1, 30).Select(x => new SampleData |
| 137 | + { |
| 138 | + Id = x, |
| 139 | + Name = "name " + x, |
| 140 | + Team = "team " + x % 5, |
| 141 | + HireDate = DateTime.Now.AddDays(-x).Date |
| 142 | + }); |
| 143 | + isLoadingData = false; |
| 144 | + } |
| 145 | +
|
| 146 | + public IEnumerable<SampleData> GridData { get; set; } = Enumerable.Empty<SampleData>(); |
| 147 | + public class SampleData |
| 148 | + { |
| 149 | + public int Id { get; set; } |
| 150 | + public string Name { get; set; } |
| 151 | + public string Team { get; set; } |
| 152 | + public DateTime HireDate { get; set; } |
| 153 | + } |
| 154 | +} |
| 155 | +
|
| 156 | +<style> |
| 157 | + .loading-panel-wrapper { |
| 158 | + /* size and appearance that try to match its container with as little CSS as possible, |
| 159 | + you can alter them as necessary for your use case and as per your preferences |
| 160 | + */ |
| 161 | + width: 100%; |
| 162 | + height: 100%; |
| 163 | + min-height: 200px; |
| 164 | + position: absolute; |
| 165 | + z-index: 123456; |
| 166 | + /* one way center the loading indicator inside */ |
| 167 | + display: flex; |
| 168 | + justify-content: center; |
| 169 | + align-items: center; |
| 170 | + } |
| 171 | +
|
| 172 | + .loading-panel-wrapper .loading-panel-overlay { |
| 173 | + background: gray; |
| 174 | + position: absolute; |
| 175 | + width: 100%; |
| 176 | + height: 100%; |
| 177 | + opacity: 0.5; |
| 178 | + } |
| 179 | +</style> |
| 180 | +```` |
| 181 | + |
| 182 | +## See Also |
| 183 | + |
| 184 | + * [Live Demo: Loader](https://demos.telerik.com/blazor-ui/loader/overview) |
| 185 | + * [Appearance Settings]({%slug loader-appearance%}) |
| 186 | + * [API Reference](https://docs.telerik.com/blazor-ui/api/Telerik.Blazor.Components.TelerikLoader) |
| 187 | + |
0 commit comments