1
  • I created a group of InputNumber by foreach

    private InputNumber<double?> OnfocusRef; //This should be dynmic but I don't know how!!!
    @foreach(employee in Employees){
    {
     <EditForm Model="employee">
    
       <InputNumber @bind-Value="employee.Monday" @ref="OnfocusRef" 
        @onfocus=@(() => OnfocusHandler(employee))
    
       // other inputNumbers with rest days of the week (Tuesday, Wednesday ..) ....
     </EditForm>
    }
    
  • After rendering the code up, I get a rows of inputs days for each Employee like this:

enter image description here

  • By using a tab, user can move between input box, if user jump to the next row, I have to save the changes of the previous row because it is a different employee.

    private FocusedEmployee {get; set;}
    private async Task OnfocusHandler(Employee employee)
    {
     //first lading of the page
     if(FocusedEmployee is null)
     {
       FocusedEmployee  = employee;
     }
     //jump to next row
     else if(FocusedEmployee.Id != employee.Id)
     {
      await UpdateEmployee(FocusedEmployee)
      if(OnfocusRef.Element.HasValue)
       {
        // Problem here!
        await OnfocusRef.Element.Value.FocusAsync();
       }
       else { 
        FocusedEmployee= employee;
       }
     }
    }
    
  • My question: How can I set focus on the first input box of the current Employee after each saving of pre Employee? I have to loop and create a list of EditForm and each EditForm has a list of InputRadio, my problem with @ref is not any more unique !

Nb777
  • 1,658
  • 8
  • 27
  • Does this answer your question? [How to set the focus to an InputText element?](https://stackoverflow.com/questions/59137973/how-to-set-the-focus-to-an-inputtext-element) – Yogi May 09 '22 at 09:41
  • @Yogi, it doesn't anwer my question becuase I have to loop and create a list of `EditForm` and each `EditForm` has a list of `InputRadio`, my problem with `@ref` is not any more unique ! – Nb777 May 09 '22 at 09:47
  • 1
    I would use a "ViewModel" that included the Employee and an ElementReference - then in the foreach you iterate over the employeevm collection and set the `@ref` to the `employeevm.ref` - passing the employeevm to the OnfocusHandler - this way each employee has a reference to its own dom element – Mister Magoo May 09 '22 at 12:34
  • @MisterMagoo, thanks a lot, I still have one problem that after 2 sec of focusing I lost the focus again :). – Nb777 May 09 '22 at 13:10
  • @MisterMagoo, I saw your comment after post my answer. Great minds think alike ;) – dani herrera May 09 '22 at 14:38
  • 1
    @daniherrera I didn't have the energy to create a working example ¯\_(ツ)_/¯ – Mister Magoo May 09 '22 at 16:25
  • @MisterMagoo, I wrote it just as workout. Someday I would like to become a smart hacker like you, but I need gymnastics <3 – dani herrera May 09 '22 at 16:31
  • @MisterMagoo,@daniherrera , 3 upvote for solution and no vote for question :( – Nb777 May 10 '22 at 06:38

1 Answers1

3

You can deal with Refs dynamically:

@foreach (var t in PotatosAndReferences)
{
    <input type="text" @key="@t.Potato.Id" @bind="t.Potato.Name" @ref="@t.Ref" />
}
<br>
<button @onclick="_ => GoN(0)">Focus first</button>
<button @onclick="_ => GoN(15)">Focus 16</button>
@code {
    public class Potato
    {
        public int Id {get; set; }
        public string Name {get; set;} = default!;
    }  
    public class PotatoAndReference
    {
        public Potato Potato {get; set;} = default!;
        public ElementReference? Ref {get; set;} = null;
    }
    public List<Potato> Potatos
        = 
        Enumerable.Range(1, 50)
        .Select(i => new Potato() {Id = i, Name = $"Potato {i}"}).ToList();
    public List<PotatoAndReference> PotatosAndReferences = default!;
    protected override void OnInitialized()
        =>
        PotatosAndReferences =
            Potatos
            .Select(p => new PotatoAndReference(){Potato = p}).ToList();
    protected async Task GoN(int n)
        =>
        await PotatosAndReferences.ElementAt(n).Ref!.Value.FocusAsync();    
}

enter image description here

dani herrera
  • 48,760
  • 8
  • 117
  • 177
  • Thanks a lot, I am trying to apply your solution but I have a question about `int n` in `GoN(int n)`, What does mean? and from where does come?. Can I use `Potato.Id`? – Nb777 May 09 '22 at 13:48
  • 1
    Is just a sample to illustrate you can focus the item do you want. I accés by position but you can access by the condition you need. – dani herrera May 09 '22 at 14:35
  • 1
    I like potatoes +1 – Yogi May 09 '22 at 15:08