使用内联编辑模式时,GXT3 Grid无法为复选框发送事件

时间:2014-04-19 06:09:52

标签: java gwt grid gxt

我正在使用GXT 3 Grid和InlineEdit模式跟随(或多或少)他们网站上的示例代码。我认为有一种方法可以让复选框单元格触发'EditComplete'事件,如果是这样,我不确定在收到它后如何禁用同一行上的日期单元格。只需在下面的代码中查找评论:“//不要触发复选框:”。

以下代码适用于Eclipse Web应用程序项目 - 您只需在“onModuleLoad”方法中使用它,如下所示:

public void onModuleLoad() {
  GridInlineEditingTest j = new GridInlineEditingTest();
}

以下是代码:

    import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor.Path;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.cell.core.client.form.CheckBoxCell;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.PropertyAccess;
import com.sencha.gxt.data.shared.Store;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.BoxLayoutContainer.BoxLayoutPack;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.container.Viewport;
import com.sencha.gxt.widget.core.client.event.CompleteEditEvent;
import com.sencha.gxt.widget.core.client.event.CompleteEditEvent.CompleteEditHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.CheckBox;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.DateTimePropertyEditor;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.grid.Grid;
import com.sencha.gxt.widget.core.client.grid.Grid.GridCell;
import com.sencha.gxt.widget.core.client.grid.GridView;
import com.sencha.gxt.widget.core.client.grid.editing.GridEditing;
import com.sencha.gxt.widget.core.client.grid.editing.GridInlineEditing;

public class GridInlineEditingTest {
  public GridInlineEditingTest() {
        VerticalLayoutContainer vlc = new VerticalLayoutContainer();
        vlc.add(createGrid(), new VerticalLayoutData(1, 1));

        Viewport vp = new Viewport();
        vp.add(vlc);
        RootPanel.get().add(vp);
      }

      interface PlaceProperties extends PropertyAccess<Plant> {
        ValueProvider<Plant, Date> available();

        @Path("id")
        ModelKeyProvider<Plant> key();
        ValueProvider<Plant, String> name();
        ValueProvider<Plant, Boolean> indoor();
      }

      private static final PlaceProperties properties = GWT.create(PlaceProperties.class);

      protected Grid<Plant> grid;
      private FramedPanel panel;
      private ListStore<Plant> store;
      private DateField dateField;

      public Widget createGrid() {
        if (panel == null) {
          ColumnConfig<Plant, String>   nameCol     = new ColumnConfig<Plant, String>(  properties.name(),      220,    "Name"  );
          ColumnConfig<Plant, Date>     dateCol     = new ColumnConfig<Plant, Date>(    properties.available(), 95,     "Date"  );
          ColumnConfig<Plant, Boolean>  indorCol    = new ColumnConfig<Plant, Boolean>( properties.indoor(),    55,     "Indoor");

          // display formatting
          DateCell dateCell = new DateCell(DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT));
          dateCol.setCell(dateCell);

          // display a checkbox in the gridview
          indorCol.setCell(new CheckBoxCell());


          List<ColumnConfig<Plant, ?>> l = new ArrayList<ColumnConfig<Plant, ?>>();
          l.add(nameCol);
          l.add(dateCol);
          l.add(indorCol);
          ColumnModel<Plant> columns = new ColumnModel<Plant>(l);

          store = new ListStore<Plant>(properties.key());
          store.setAutoCommit(false);
          store.addAll(getPlants());

          GridView<Plant> gridView = new GridView<Plant>();
          grid = new Grid<Plant>(store, columns, gridView);
          grid.getView().setAutoExpandColumn(nameCol);

          // EDITING//
          final GridEditing<Plant> editing = new GridInlineEditing<Plant>(grid); 
          dateField = new DateField(new DateTimePropertyEditor(DateTimeFormat.getFormat(PredefinedFormat.DATE_SHORT)));
          dateField.setClearValueOnParseError(false);
          editing.addEditor(dateCol, dateField);
          CheckBox checkField = new CheckBox();
          editing.addEditor(indorCol, checkField);

          editing.addCompleteEditHandler( new CompleteEditHandler<Plant>(){

              // not firing for checkbox:
              @Override
              public void onCompleteEdit(CompleteEditEvent<Plant> event) {
                  GridCell cell = event.getEditCell();

                  int row = cell.getRow();
                  int col = cell.getCol();
                  System.out.println("got here. row "+row+", col "+col);

              }
          });

          panel = new FramedPanel();
          panel.setHeadingText("Editable Grid Example");
          panel.setPixelSize(600, 400);
          panel.addStyleName("margin-10");

          VerticalLayoutContainer con = new VerticalLayoutContainer();
          con.setBorders(true);
          con.add(grid, new VerticalLayoutData(1, 1));

          panel.setWidget(con);
          panel.setButtonAlign(BoxLayoutPack.CENTER);
          panel.addButton(new TextButton("Reset", new SelectHandler() {
            @Override
            public void onSelect(SelectEvent event) {
              store.rejectChanges();
            }
          }));

          panel.addButton(new TextButton("Save", new SelectHandler() {
            @Override
            public void onSelect(SelectEvent event) {
              store.commitChanges();
            }
          }));
        }
        return panel;
      }

      private static int AUTO_ID = 0;

      public class Plant {
        private DateTimeFormat df = DateTimeFormat.getFormat("MM/dd/y");
        private int id;
        private String name;
        private String light;
        private double price;
        private Date available;
        private boolean indoor;
        private String color;
        private int difficulty;
        private double progress;

        public Plant() {
          id = AUTO_ID++;
          difficulty = (int) (Math.random() * 100);
          progress = Math.random();
        }

        public Plant(String name, String light, double price, String available, boolean indoor) {
          this();
          setName(name);
          setLight(light);
          setPrice(price);
          setAvailable(df.parse(available));
          setIndoor(indoor);
        }

        public int getId() {                            return id;                      }
        public double getProgress() {                   return progress;                }
        public String getColor() {                      return color;                   }
        public int getDifficulty() {                    return difficulty;              }
        public Date getAvailable() {                    return available;               }
        public String getLight() {                      return light;                   }
        public String getName() {                       return name;                    }
        public double getPrice() {                      return price;                   }
        public boolean isIndoor() {                     return indoor;                  }

        public void setId(int id) {                     this.id = id;                   }
        public void setProgress(double progress) {      this.progress = progress;       }
        public void setAvailable(Date available) {      this.available = available;     }
        public void setDifficulty(int difficulty) {     this.difficulty = difficulty;   }
        public void setColor(String color) {            this.color = color;             }
        public void setIndoor(boolean indoor) {         this.indoor = indoor;           }
        public void setLight(String light) {            this.light = light;             }
        public void setName(String name) {              this.name = name;               }
        public void setPrice(double price) {            this.price = price;             }

        @Override
        public String toString() {
          return name != null ? name : super.toString();
        }
      }

      public List<Plant> getPlants() {
        List<Plant> plants = new ArrayList<Plant>();
        plants.add(new Plant("Bloodroot", "Mostly Shady", 2.44, "03/15/2006", true));
        plants.add(new Plant("Columbine", "Shade", 9.37, "03/15/2006", true));
        plants.add(new Plant("Marsh Marigold", "Mostly Sunny", 6.81, "05/17/2006", false));
        plants.add(new Plant("Cowslip", "Mostly Shady", 9.90, "03/06/2006", true));
        plants.add(new Plant("Dutchman's-Breeches", "Mostly Shady", 6.44, "01/20/2006", true));
        plants.add(new Plant("Ginger, Wild", "Mostly Shady", 9.03, "04/18/2006", true));
        return plants;
      }

}

感谢。祝你有美好的一天!

2 个答案:

答案 0 :(得分:1)

您正在列中设置复选框单元格,然后还将字段附加为列的内联编辑器。因此,如果用户单击复选框(单元格),您希望忽略该单击,而是显示一个复选框(字段),用户可以单击该复选框(字段)?

相反,正在发生的事情是复选框(单元格)报告它正在使用该click事件做一些有用的事情 - 它正在改变它的值。因此,网格编辑机制忽略了点击,因此复选框(字段)永远不会进入编辑模式,因此它当然不会完成编辑模式。

你想通过将两个不同的复选框绘制在同一个地方的目的来实现什么,并且功能不同?如果您尝试使用CheckBoxCell实例作为始终在网格单元格中绘制复选框符号的方法,则有两个主要选择:

  • 在内联编辑中跳过CheckBox字段,然后让单元格处理它。它不会触发编辑事件,但它仍然会直接与商店交互。如果需要,您可以监听单元格的事件,或者只是记录来自商店的记录更改事件,或者您可以将单元格子类化以修改行为。
  • 删除事件处理CheckBoxCell的内容以防止它处理事件 - 这可能就像覆盖onBrowserEvent一样简单,但我怀疑你实际上会想要完全阻止其检查更改行为以便Inline编辑版本负责处理

最后,请记住,内联编辑的目的是使网格不会成为大量字段,并使其仅在用户实际与其交互时绘制这些字段。这意味着用户必须首先单击某个字段以显示类似于复选框的内容,然后与该字段进行交互以进行更改。再次在http://www.sencha.com/examples/#ExamplePlace:inlineeditablegrid的内嵌可编辑网格中的CheckBox字段中查看(虽然这次使用自定义单元格),您将看到这意味着两次点击即可更改值并获取CompleteEditing事件(如以及你所追求的各种其他领域变革事件 - 这真的是你的想法吗?

答案 1 :(得分:0)

根据CheckBoxCell#isEditing()的源代码说明:

  

复选框永远不会处于“编辑模式”。在已选中和未选中之间没有中间状态。

在此处找到备用解决方案How to get the row index of selected checkbox on grid GXT

请查看GXT checkbox in grid