我想在星云Nattable中实现行删除逻辑。 这就是我打算做的事情:
我的问题是,这是实现这一目标的最佳方式:
this.natview.refresh();
时刷新表格视图吗?
或 SelectionLayer
获取行并删除它们(如果是这样,我该怎么办?)?
或 IConfiguration
?答案 0 :(得分:3)
在NatTable中,您通常会执行以下操作:
创建删除行的命令
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);
}
}
为该命令创建命令处理程序
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
bodyDataLayer.registerCommandHandler(
new DeleteRowCommandHandler<your type>(bodyDataProvider.getList()));
将菜单项添加到触发命令的菜单配置
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
来正确完成。 / p>
请注意,显示的示例将删除打开上下文菜单的行。如果应删除所有选定的行,则应创建一个知道SelectionLayer
的命令处理程序,并检索所选行,如另一个答案中所示。
答案 1 :(得分:0)
在我们的应用程序中,我们执行以下操作:
获取选定的行对象:
SelectionLayer selectionLayer = body.getSelectionLayer();
int[] selectedRowPositions = selectionLayer.getFullySelectedRowPositions();
Vector<Your Model Objects> rowObjectsToRemove = new Vector<Your Model Objects>();
for (int rowPosition : selectedRowPositions) {
int rowIndex = selectionLayer.getRowIndexByPosition(rowPosition);
rowObjectsToRemove .add(listDataProvider.getRowObject(rowIndex));
}
从数据提供者
natTable.refresh()