Hi team
Is there is a way to change focus from toolbar button to External button when "TAB" or "F2" was pressed.
For example, focus from "Add" button in toolbar going to External button "Update" .
Hoping for your immediate response.
Best Regards,
Tyrone
Hi Tyrone,
Based on your requirement, we suggest using the following approach with a JavaScript solution to achieve your goal. Please check the attached sample and code snippet for your reference.
|
@inject IJSRuntime RunTime
<SfGrid DataSource="@Orders" ID="Grid" AllowPaging="true" Height="200" @ref="Grid" Toolbar=@ToolbarItems> </SfGrid>
<SfButton @ref="sfButton" OnClick="Update">Update</SfButton> @code { private SfGrid<OrderData> Grid;
SfButton sfButton { get; set; } public string[] ToolbarItems = new string[] { "Add", "Delete","Update" }; public List<OrderData> Orders { get; set; }
protected override void OnInitialized() { Orders = OrderData.GetAllRecords(); }
[JSInvokable("ChangeFocusUpdate")] // Make this method callable from JavaScript public async Task FocusUpdateButton() { await sfButton.FocusAsync(); }
public async Task Update() {
}
protected override Task OnAfterRenderAsync(bool firstRender) { if (firstRender) // Only set this on the first render { var dotNetReference = DotNetObjectReference.Create(this); RunTime.InvokeAsync<string>("UpdateFocus", dotNetReference); // Pass reference to JS } return base.OnAfterRenderAsync(firstRender); } }
function filter(dotnet) { dotnetInstance = dotnet; // dotnet instance to invoke C# method from JS }
// Attach the keydown event listener document.addEventListener('keydown', function (event) { // Check if the Tab key is pressed if (event.key === 'Tab') {
const focusedElement = document.activeElement; if ( focusedElement && focusedElement.parentElement.id === "Grid_add") { // Prevent the default tab behavior event.preventDefault();
// Call the C# method to handle focus change dotnetInstance.invokeMethodAsync('ChangeFocusUpdate'); // call C# method from javascript function } } }); |
Regards,
Prathap Senthil
Hi Prathap,
At first it did not work in google chrome however it works in Mozilla. When I apply ccleaner it worked like a charm. what could be the possible cause of this?
Thanks a lot,
Tyrone
We are happy to hear that the provided solution was helpful on your end. However, the issue you encountered, where the DataGrid didn’t initially work in Google Chrome but worked in Mozilla Firefox and then worked after running CCleaner, could be related to browser-specific caching, cookies, or temporary files. The possible cause might be browser cache - browsers store cached versions of websites and resources (like CSS, JavaScript) to load them faster. If Chrome had an outdated or corrupted cache, the grid might not have loaded properly. Therefore, when applying code changes such as JS or CSS, please ensure you clear the cache. Thank you for your understanding.