【问题标题】:How do I allow a single-item/sub-editor GWT Editor to remove itself from a ListEditor that contains it? e.g. on remove button clicked如何允许单项/子编辑器 GWT 编辑器从包含它的 ListEditor 中删除自己?例如点击删除按钮
【发布时间】:2015-06-25 13:42:44
【问题描述】:

在尝试使用 GWT 的 ListEditor 系统时,我无法找到一个工作示例,其中列表中每个项目的 UI 都有一个删除/删除按钮。

我发现的例子都像 this one[1] 并且有一个 EditorSource.create() 实现创建每个项目 Editor 并且似乎连接了一个处理程序以通过 listEditor.getList().remove(index) 从基础列表中删除项目.

但是,删除处理程序的匿名实现在子编辑器创建时围绕 index 的值关闭,这会导致 IndexOutOfBoundExceptions 或错误的项目被删除,因为每次删除都会更改所有项目的索引来吧。

我拉了一会儿头发,试图看看我在示例中缺少什么以防止这种情况发生,但据我所知,他们确实都有这个问题,所以虽然修复相当简单,但我仍会在此处发布,因此人们至少可以找到一个正确移除项目的示例。

[1] 我认为我找到的所有示例都源自我链接的示例,尽管该示例在 remove() 中具有更多逻辑,并且可能一直在做一些事情来避免问题,例如更正列表以某种方式订购,我还没有深入研究该项目中的其他代码。

【问题讨论】:

    标签: gwt indexoutofboundsexception gwt-editors


    【解决方案1】:

    以下是一个最小的ListEditor 示例,它纠正了其他示例中发现的问题。

    public abstract class FooEditor extends Composite implements Editor<Foo> {
    
        Widget root; // Instantiated explicitly or through uibinder
    
        // Implemented as one of uibinder+fields, fields, methods, or LeafValueEditor.set/getValue()
    
        public FooEditor() { 
            initWidget(root); 
        }
    
        // Used for brevity, could be any triggering mechanism, click handler, event handler, etc.
        abstract void onDeleteClicked(); 
    }
    
    public class FooListEditor extends Composite implements IsEditor<ListEditor<Foo, FooEditor>> {
    
        private class FooEditorSource extends EditorSource<FooEditor> {
    
            @Override 
            public FooEditor create(int index) {
    
                FooEditor subEditor = new FooEditor()
                {
                    @Override
                    public void onDeleteClicked()
                    {
                        // =======================================================
                        //
                        // This fixes the problem present in other examples
                        // by determining the current index at the time of removal
                        //
                        // =======================================================
                        int currentIndex = listEditor.getEditors().indexOf(this);
                        listEditor.getList().remove(currentIndex);    
                    }
                };
    
                setIndex(subEditor, index);
    
                return subEditor;
            }
    
            @Override 
            public void dispose(FooEditor subEditor) { 
                subEditor.removeFromParent(); 
            }
    
            @Override 
            public void setIndex(FooEditor subEditor, int index) {
                listPanel.insert(subEditor, index);
            }
        }
    
        FlowPanel listPanel; // Instantiated explicitly or through uibinder
    
        ListEditor<Foo, FooEditor> listEditor = ListEditor.of(new FooEditorSource());
    
        public FooListEditor() {
            initWidget(listPanel);
        }
    
        @Override 
        public ListEditor<Foo, FooEditor> asEditor() { 
            return listEditor; 
        }
    }
    

    【讨论】:

    • 非常感谢您添加此内容。对社区绝对有用:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多