如何在右键单击表格行时显示电子上下文菜单

时间:2022-08-15 17:34:50

I am trying to learn Electron and building a simple application. In the HTML part, I have table with some rows.
I have created the context menu using "electron-context-menu" NPM package. Now I want that when I right click on any row of the table then some menu will popup, like remove row.
For example, I have a row like below:

我正在尝试学习Electron并构建一个简单的应用程序。在HTML部分中,我有一些行的表。我使用“electron-context-menu”NPM包创建了上下文菜单。现在我希望当我右键单击表格的任何一行时,会弹出一些菜单,比如删除行。例如,我有一行如下:

<tr>
  <td id="name-1"></td>
  <td id="prog-1"><progress id='progress-1' max='100' value='0'> </progress></td>
  <td id="size-1"></td>
  <td id="status-1"></td>
  <td style="display:none;" id="path-1"></td>
  <td style="display:none;" id="link-1"></td>
  <td style="display:none;" id="formatid-1"></td>
</tr>

Now when I right click on this row, then only the remove row menu should appear and upon click of that menu, I want to call a function deleteRow(this) which will remove the row.
For dynamically adding and deleting rows, I am using code from:

现在,当我右键单击此行时,只显示删除行菜单,单击该菜单后,我想调用一个函数deleteRow(this),它将删除该行。为了动态添加和删除行,我使用的代码来自:

here is JSFiddle

这是JSFiddle

Any help? Also, how can I get the id of element in row?

有帮助吗?另外,我怎样才能获得行中元素的id?

2 个解决方案

#1


1  

Add false parameter after callback.

回调后添加false参数。

new_row.addEventListener('contextmenu', function(e){
    var t = e.srcElement.id.split('-');
    id = t[1];
    menu.popup(remote.getCurrentWindow());
}, false);

#2


0  

I found answer myself:

我找到了答案:

const menu = new Menu();
menu.append(new MenuItem({
  label: 'Resume', click(){
    console.log('resume clicked');
  }
}));
menu.append(new MenuItem({type: 'separator'}));
menu.append(new MenuItem({
  label: 'Pause', click(){
    console.log('item 2 clicked');
  }
}));

And where I am adding dynamic rows:

我在哪里添加动态行:

new_row.addEventListener('contextmenu', function(e){
      var t = e.srcElement.id.split('-');
      id = t[1];
      menu.popup(remote.getCurrentWindow());
    });

#1


1  

Add false parameter after callback.

回调后添加false参数。

new_row.addEventListener('contextmenu', function(e){
    var t = e.srcElement.id.split('-');
    id = t[1];
    menu.popup(remote.getCurrentWindow());
}, false);

#2


0  

I found answer myself:

我找到了答案:

const menu = new Menu();
menu.append(new MenuItem({
  label: 'Resume', click(){
    console.log('resume clicked');
  }
}));
menu.append(new MenuItem({type: 'separator'}));
menu.append(new MenuItem({
  label: 'Pause', click(){
    console.log('item 2 clicked');
  }
}));

And where I am adding dynamic rows:

我在哪里添加动态行:

new_row.addEventListener('contextmenu', function(e){
      var t = e.srcElement.id.split('-');
      id = t[1];
      menu.popup(remote.getCurrentWindow());
    });