【问题标题】:Editor not getting ListBox changes编辑器未获得 ListBox 更改
【发布时间】:2012-01-14 22:15:13
【问题描述】:

问题:正确加载表单、ParentEditor、子编辑器 ThisEditor 填充其所有字段,包括列表框(下拉)小部件 MyWidget。但是,如果我在列表框中选择一个新选项并保存,它不会保存新选择的选项;尽管对其他小部件的编辑可以很好地保存。似乎在驱动程序刷新时,编辑器没有获得我的列表框中的值。在调试模式下,在 driver.edit 上,我可以在包括列表框在内的所有表单小部件上看到 TakesValueEditor 调用 setValue(value)。但是在刷新时,我可以看到 TakesValueEditor 在其他表单小部件上调用它的 getValue() 但是 不在我的列表框中。

编辑器层次结构:ParentEditor > ThisEditor > MyWidget。 ParentEditor 是整个表单。 ThisEditor 是表单的一个子部分。 MyWidget 是 ThisEditor 部分中的自定义列表框。

我正在使用 MVP 模式。下面是 View 和 Presenter 的示例代码 sn-ps:

VIEW:

    /** ThisEditor is a sub-section of ParentEditor (the Form) and contains a 
    MyWidget (custom listbox). */
    public class ThisEditor extends Composite implements Editor<ThisProxy>, ThisView {
   ... //rfeDriver interface defined and created here

   @UiField
        MyWidget my;  //getMy and setMy methods in ThisProxy

   ... //other field declarations

        public ThisEditor() {
            initWidget(binder.createAndBindUi(this));
        }

  @Override
       public MyView getMy() {
           return my;
       }
  ... //other methods
    }

    /** This is the View interface that MyWidget implements */
    public interface MyView extends HasOptions, HasValue<MyProxy>, Focusable {
        interface Presenter {
       ...
   }
   ...
    }

    public class MyWidget extends Composite implements MyView,    
            IsEditor<LeafValueEditor<MyProxy>> {
   ...
   @UiField
        ListBox listBox; //single-select dropdown 
   ...

   public MyWidget() {
            initWidget(binder.createAndBindUi(this));
       addChangeHandler(); //listen to changes to listBox and setSelectedIndex (?)
   }
   ...
   @Override
   public int getSelectedIndex() {
            return listBox.getSelectedIndex();
        }

        @Override
        public void setSelectedIndex(int index) {
            listBox.setSelectedIndex(index);
        }
   ...

        /**
         * Called by the TakesValueEditor on rfeDriver.edit.
         */
   @Override
        public MyProxy getValue() {
            //ask presenter for the MyProxy object -- presenter calls
            //getSelectedIndex() on this widget and returns the object associated 
            //with the index 
            return presenter.getValue();
        }

    /**
     * Called by the TakesValueEditor on rfeDriver.flush.
     */
     @Override
     public void setValue(MyProxy value) {
     //pass the value to the presenter to parse and set the index that corresponds  
     //to this object
         presenter.setValue(value);
     }

PRESENTER

    public class MyPresenter implements MyView.Presenter,     
            ValueLookupCompleteEventHandler {
        ...
   protected HasOptions view;
   private List<MyProxy> myList;

   public MyPresenter(ParentPresenter parent) {
       //setParent for this child presenter
   }

   ... //methods to set view and create association between view and presenter

   @Override
   public MyProxy getValue() {
       //identify the current selection
       String selectedId = view.getValue(view.getSelectedIndex());

       if (selectedId != null) {
           //iterate myList to find the MyProxy object whose id.equals(selectedId)
           for (Iterator<MyProxy> i = myList.iterator(); i.hasNext();) {
          MyProxy value = i.next();
          if (selectedId.equals(value.getCode().toString())) {
              return value;
          }
      } 
            }
       return null;
        }

   @Override
   public void setValue(MyProxy value) {
       //handle null value
        String selectedId = value.getCode().toString();
   ... //verify the value is in myList

   //traverse dropdown list and set selected index corresponding to value object
   for (int i = 0; i < view.getItemCount(); ++i) {
       if (selectedId.equals(view.getValue(i))) {
           view.setSelectedIndex(i);
       }
   }
    }
}

【问题讨论】:

    标签: gwt gwt-editors


    【解决方案1】:

    我刚刚遇到了同样的问题。如果您查看 GWT ListBox 的源代码,您会发现它没有实现 isEditor。我实际上不认为它是一个 GWT 编辑器组件。很难理解。我不得不围绕它编写代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      • 2017-01-11
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多