从Nattable中删除行 [英] Delete rows from Nattable
问题描述
我想在星云Nattable中实现行删除逻辑. 我打算这样做:
I want to implement a row deletion logic in a Nebula Nattable. This is what I plan to do:
- 在我的问题是,这是完成此任务的最佳方法:
my question is, which is the best way to accomplish this:
- 是否应该从数据模型中删除相应的值,并在执行
this.natview.refresh();
时刷新表视图? OR - 是否应该从
SelectionLayer
中获取行并将其删除(如果是,该怎么办?)? OR - 通过
IConfiguration
对此功能是否有默认支持?
- Should I delete the corresponding value from my data model and the table view is refreshed when I execute
this.natview.refresh();
? OR - Should I get the rows from
SelectionLayer
and delete them (if so how do I do ?)? OR - is there any default support for this function through
IConfiguration
?
推荐答案
在NatTable中,您通常会执行以下操作:
In NatTable you would typically do the following:
-
创建用于删除行的命令
Create a command for deleting a row
public class DeleteRowCommand extends AbstractRowCommand { public DeleteRowCommand(ILayer layer, int rowPosition) { super(layer, rowPosition); } protected DeleteRowCommand(DeleteRowCommand command) { super(command); } @Override public ILayerCommand cloneCommand() { return new DeleteRowCommand(this); } }
- 是否应该从数据模型中删除相应的值,并在执行
-
为该命令创建命令处理程序
Create a command handler for that command
public class DeleteRowCommandHandler<T> implements ILayerCommandHandler<DeleteRowCommand> { private List<T> bodyData; public DeleteRowCommandHandler(List<T> bodyData) { this.bodyData = bodyData; } @Override public Class<DeleteRowCommand> getCommandClass() { return DeleteRowCommand.class; } @Override public boolean doCommand(ILayer targetLayer, DeleteRowCommand command) { //convert the transported position to the target layer if (command.convertToTargetLayer(targetLayer)) { //remove the element this.bodyData.remove(command.getRowPosition()); //fire the event to refresh targetLayer.fireLayerEvent(new RowDeleteEvent(targetLayer, command.getRowPosition())); return true; } return false; } }
-
将命令处理程序注册到主体DataLayer
Register the command handler to the body DataLayer
bodyDataLayer.registerCommandHandler( new DeleteRowCommandHandler<your type>(bodyDataProvider.getList()));
-
将菜单项添加到您的菜单配置中,以触发命令
Add a menu item to your menu configuration that fires the command
new PopupMenuBuilder(natTable) .withMenuItemProvider(new IMenuItemProvider() { @Override public void addMenuItem(NatTable natTable, Menu popupMenu) { MenuItem deleteRow = new MenuItem(popupMenu, SWT.PUSH); deleteRow.setText("Delete"); deleteRow.setEnabled(true); deleteRow.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent event) { int rowPosition = MenuItemProviders.getNatEventData(event).getRowPosition(); natTable.doCommand(new DeleteRowCommand(natTable, rowPosition)); } }); } }) .build();
使用此命令不需要调用NatTable#refresh()
,因为命令处理程序将触发RowDeleteEvent
.我也不建议在这种情况下调用NatTable#refresh()
,因为它可能会更改和刷新超过其应有的值,并且不会正确地更新其他状态,这可以通过触发RowDeleteEvent
来正确完成.
Using this you don't need to call NatTable#refresh()
because the command handler fires a RowDeleteEvent
. I also don't suggest to call NatTable#refresh()
in such a case, as it might change and refresh more than it should and would not update other states correctly, which is done correctly by firing the RowDeleteEvent
.
请注意,所示示例删除了为其打开上下文菜单的行.如果应该删除所有 selected 行,则应创建一个知道SelectionLayer
的命令处理程序,并检索所选行,如其他答案所示.
Note that the shown example deletes the row for which the context menu is opened. If all selected rows should be deleted, you should create a command handler that knows the SelectionLayer
and retrieve the selected rows as shown in the other answer.
这篇关于从Nattable中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!