简体   繁体   English

如何检测双击 MudBlazor 中的表格行?

[英]How to detect a double click on a table row in MudBlazor?

I am using MudBlazor and I would like to detect a double click on a single table row and react to the event.我正在使用 MudBlazor,我想检测对单个表格行的双击并对事件做出反应。 However, there is no double click listed in the API of the table.但是表中的API并没有列出双击。 The single click is easy to do with the OnRowClick callback (see also here on SO ).使用OnRowClick回调很容易实现单击(另请参见此处的 SO )。 There is no equivalent for double clicks.没有等效的双击。

I tried using the Blazor event ondblclick with我尝试使用 Blazor 事件ondblclick

<MudTd @ondblclick="OnOrderDbClicked">
    // ...
<MudTd/>

I have two issues with this:我有两个问题:

  1. I can catch the double click.我可以抓住双击。 But I cannot make it aware of the row that was clicked (for which I'd have a workaround).但是我无法让它知道被单击的行(对此我有一个解决方法)。
  2. Also I'd have to add to to each cell because the <RowTemplate/> element just won't accept it.此外,我还必须添加到每个单元格,因为<RowTemplate/>元素不会接受它。

I only have a crutch for issue 1: Bind a variable MyOrderVM MyItem to the SelectedItem item parameter of the table and access the object in the method that is called by the double click (because a double click also causes two single clicks which select the item).我只有问题1的拐杖:将变量MyOrderVM MyItem绑定到表的SelectedItem项目参数并在双击调用的方法中访问object(因为双击也会导致两次单击select项目).

I haven't done a lot of testing but it seems this could work but isn't there a better solution?我没有做过很多测试,但似乎这可行,但没有更好的解决方案吗? Or do I worry to much about side effects?还是我太担心副作用了?

As you have said, currently there is no out-of-the-box way like with OnRowClick for double click.正如您所说,目前没有像 OnRowClick 这样的开箱即用方式进行双击。 However, feel free to create an issue in the repo .但是,请随时在 repo 中创建问题

As a workaround, you could use your method 1 with a little adaption to stop the propagation of the click event to the MudTable.作为一种解决方法,您可以使用您的方法 1 稍作修改,以停止将点击事件传播到 MudTable。 I think this way is easier to understand than the detour over the SelectedItem我认为这种方式比绕过SelectedItem更容易理解

<MudTd>
  // the click is handled by the div and not bubbling up to the MudTd
  <div @onclick="@EmptyCallback" @onclick:stopPropagation="true" 
              @ondblclick="@( (x) => DoSomething(context))" >
     // ...
  </div>
<MudTd/>

Here is a MudBlazor Playground to show it.这是一个MudBlazor Playground来展示它。

Disclaimer:免责声明:

I'm a contributor to MudBlazor我是 MudBlazor 的贡献者

将 T="YourTable" 属性添加到 MudTable 至关重要

There is an EventCallback ondblclick in MudTd . MudTd中有一个EventCallback ondblclick You can call that and pass a method with your desired parameter.您可以调用它并使用所需参数传递一个方法。 See the sample code below.请参阅下面的示例代码。

<RowTemplate>
   <MudTd @ondblclick="@( (x) => DoubleClickEvent(context))">
      @context.SomeItem
   </MudTd>
</RowTemplate>

Create a method in the @code section, and name it as you named it in the Callback.@code部分中创建一个方法,并按照您在回调中的名称命名它。 See the sample code below.请参阅下面的示例代码。

private void DoubleClickEvent(ContextModel model)
{
   // Do something with the model.
}

I hope it helps someone.我希望它可以帮助某人。

UPDATE:更新:
For people finding this on their search, MudBlazor does have a indicator for double clicks now. 对于在搜索中找到它的人,MudBlazor 现在确实有一个双击指示器。

It is not an explixit parameter, but rather part of the OnRowClick parameter. 它不是显式参数,而是OnRowClick参数的一部分。 In the TableRowClickEventArgs it providers, there is a property MouseEventArgs that has a property Detail . 在它提供的TableRowClickEventArgs中,有一个属性MouseEventArgs具有属性Detail The documentation to this property state: 此属性的文档 state:

A count of consecutive clicks that happened in a short amount of time, incremented by one.短时间内发生的连续点击次数,递增 1。

So by checking this against 2 you can react to double clicks.因此,通过对照 2 检查它,您可以对双击做出反应。 I´ve used this in my own application and can confirm this works.我在我自己的应用程序中使用了它并且可以确认它有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM