【问题标题】:Using RowEditing and CheckBoxSelectionModel in a Grid Fails在网格中使用 RowEditing 和 CheckBoxSelectionModel 失败
【发布时间】:2014-07-04 07:31:18
【问题描述】:

在 GXT Grid 中,我尝试使用 RowEditing 和 CheckBoxSelectionModel。 Sencha Explorer Demo 在Row Editable Grid 和CheckBox Grid 示例中有这些示例,但它们没有显示包含这些功能组合的示例。当我在同一个网格上使用这两个功能时,我没有得到我预期的行为。如果我单击“选择”复选框,则该行将进入编辑模式,我预计该复选框只会从选中变为未选中,反之亦然。此外,当该行进入编辑模式时,该行存在损坏。这是在单击该行中的任何值之前网格中的一行的示例:

这是单击其中一个值后的那一行:

有人有这方面的经验吗?

更新

这是一个演示问题的示例类:

package org.greatlogic.gxtgrid.client;

import java.util.ArrayList;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.sencha.gxt.core.client.IdentityValueProvider;
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.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.grid.CheckBoxSelectionModel;
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.GridView;
import com.sencha.gxt.widget.core.client.grid.editing.GridRowEditing;

public class GXTGrid implements EntryPoint {
//-------------------------------------------------------------------------
@Override
public void onModuleLoad() {
  ListStore<Pet> listStore = new ListStore<>(new ModelKeyProvider<Pet>() {
    @Override
    public String getKey(Pet pet) {
      return Integer.toString(pet.getPetId());
    }
  });
  IdentityValueProvider<Pet> ivp = new IdentityValueProvider<>();
  CheckBoxSelectionModel<Pet> sm = new CheckBoxSelectionModel<>(ivp);
  ArrayList<ColumnConfig<Pet, ?>> ccList = new ArrayList<>();
  ccList.add(sm.getColumn());
  ColumnConfig<Pet, String> cc1;
  cc1 = new ColumnConfig<>(Pet.getPetNameValueProvider(), 100, "Name");
  ccList.add(cc1);
  ColumnModel<Pet> columnModel = new ColumnModel<>(ccList);
  Grid<Pet> grid = new Grid<>(listStore, columnModel);
  grid.setSelectionModel(sm);
  grid.setView(new GridView<Pet>());
  GridRowEditing<Pet> gre = new GridRowEditing<>(grid);
  gre.addEditor(cc1, new TextField());
  listStore.add(new Pet(1, "Lassie"));
  listStore.add(new Pet(2, "Scooby"));
  listStore.add(new Pet(3, "Snoopy"));
  ContentPanel contentPanel = new ContentPanel();
  contentPanel.add(grid);
  RootLayoutPanel.get().add(contentPanel);
}
//-------------------------------------------------------------------------
private static class Pet {
private int _petId;
private String    _petName;
public static ValueProvider<Pet, String> getPetNameValueProvider() {
  return new ValueProvider<Pet, String>() {
    @Override
    public String getPath() {
      return "Pet.PetName";
    }
    @Override
    public String getValue(Pet pet) {
      return pet._petName;
    }
    @Override
    public void setValue(Pet pet, final String value) {
      pet._petName = value;
    }
  };
}
public Pet(int petId, final String petName) {
  _petId = petId;
  _petName = petName;
}
public int getPetId() {
  return _petId;
}
}
//-------------------------------------------------------------------------
}

【问题讨论】:

  • 你能提供你的代码吗?你会得到更多更好的答案。
  • @RadASM 代码相当广泛,但您可以使用Sencha GXT Explorer Demo 中的Sencha Row Editable Grid 示例并将CheckBoxSelectionModel 添加到其中以查看问题。您是否在使用 RowEditing 和 CheckBoxSelectModel 选项的网格上取得了成功?
  • 我习惯了 inlineRowEditing。所以基本上我已经使用 gxt 一段时间了,我可能会提供帮助。一种可能对您有所帮助(不是解决问题而是避免问题)的想法不是单击单元格而是双击开始编辑。 gridRowEditing.setClicksToEdit(ClicksToEdit.TWO);很抱歉,没有看到源代码,我想我不能给你更多的帮助。
  • @RadASM 谢谢...我将创建一个(相对)小的测试用例并用代码更新我的问题。

标签: gxt


【解决方案1】:

这种带有 CheckBoxSelectionModel 的 GridRowEditing 行为是完全正常的。

我用你的代码来尝试一些事情。我认为使用GridRowEditing 和CheckBoxSelectionModel 的最佳方式是,正如我猜想的那样,在双击时开始编辑,因为目前还没有提供任何东西来实现一键编辑。为此,只需添加

gre.setClicksToEdit(ClicksToEdit.TWO);

否则,如果你实在不想用两次点击开始编辑,你也可以使用InlineRowEditing,这样你就可以随心所欲地使用CheckBoxSelectionModel了。

最终,您也许可以覆盖GridRowEditing 的整个行为,只需单击一下即可正确处理 CheckBoxSelectionModel,但这会更复杂,并且需要更具体的 GXT 框架知识,而我没有。

【讨论】:

  • 我尝试添加 setClicksToEdit(ClicksToEdit.TWO) 并看到相同的行为(当然,我现在必须单击两次才能进入编辑模式)。行的开头是“损坏的”......它显示记录的toString,在我的例子中是默认的Object#toString。无论我是单击一次还是两次单击编辑,此行为都是一致的。我可能会忍受这种行为,除了用于编辑的字段不与列对齐。无论如何,再次感谢您的帮助!
【解决方案2】:

我还没有使用CheckBoxSelectionModel 找到解决此问题的方法,因此我决定尝试另一种方法,即在包含复选框的网格中添加一列,并手动处理选择的状态。为此,我发现我需要响应一些事件,这还不错。这是示例代码的新版本,它应该为实际实现提供一个起点:

import java.util.ArrayList;
import java.util.TreeSet;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.sencha.gxt.cell.core.client.form.CheckBoxCell;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.core.client.dom.XElement;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.form.CheckBox;
import com.sencha.gxt.widget.core.client.form.Field;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.grid.CellSelectionModel;
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.GridView;
import com.sencha.gxt.widget.core.client.grid.editing.GridRowEditing;

public class GXTGrid implements EntryPoint {
//-------------------------------------------------------------------------
private ListStore<Pet>   _listStore;
private TreeSet<Integer> _selectedPetIdSet;
//-------------------------------------------------------------------------
@Override
public void onModuleLoad() {
  _selectedPetIdSet = new TreeSet<>();
  _listStore = new ListStore<>(new ModelKeyProvider<Pet>() {
    @Override
    public String getKey(Pet pet) {
      return Integer.toString(pet.getPetId());
    }
  });
  final CellSelectionModel<Pet> sm = new CellSelectionModel<>();
  ArrayList<ColumnConfig<Pet, ?>> ccList = new ArrayList<>();
  ValueProvider<Pet, Boolean> selectValueProvider;
  selectValueProvider = new ValueProvider<GXTGrid.Pet, Boolean>() {
    @Override
    public String getPath() {
      return "SelectCheckBox";
    }
    @Override
    public Boolean getValue(Pet pet) {
      return _selectedPetIdSet.contains(pet.getPetId());
    }
    @Override
    public void setValue(Pet pet, final Boolean selected) { //
    }
  };
  ColumnConfig<Pet, Boolean> cc0 = new ColumnConfig<>(selectValueProvider, 23, "");
  CheckBoxCell checkBoxCell = new CheckBoxCell() {
    @Override
    protected void onClick(XElement parent, final NativeEvent event) {
      super.onClick(parent, event);
      Pet pet = sm.getSelectedItem();
      if (!_selectedPetIdSet.remove(pet.getPetId())) {
        _selectedPetIdSet.add(pet.getPetId());
      }
    }
  };
  cc0.setCell(checkBoxCell);
  cc0.setFixed(true);
  cc0.setHideable(false);
  cc0.setMenuDisabled(true);
  cc0.setResizable(false);
  cc0.setSortable(false);
  ccList.add(cc0);
  ColumnConfig<Pet, String> cc1;
  cc1 = new ColumnConfig<>(Pet.getPetNameValueProvider(), 100, "Name");
  ccList.add(cc1);
  ColumnModel<Pet> columnModel = new ColumnModel<>(ccList);
  Grid<Pet> grid = new Grid<>(_listStore, columnModel);
  grid.setSelectionModel(sm);
  grid.setView(new GridView<Pet>());
  GridRowEditing<Pet> gre = new GridRowEditing<>(grid);
  Field<Boolean> checkBox = new CheckBox();
  checkBox.setEnabled(false);
  gre.addEditor(cc0, checkBox);
  gre.addEditor(cc1, new TextField());
  _listStore.add(new Pet(1, "Lassie"));
  _listStore.add(new Pet(2, "Scooby"));
  _listStore.add(new Pet(3, "Snoopy"));
  ContentPanel contentPanel = new ContentPanel();
  contentPanel.add(grid);
  RootLayoutPanel.get().add(contentPanel);
}
//-------------------------------------------------------------------------
private static class Pet {
private int _petId;
private String    _petName;
public static ValueProvider<Pet, String> getPetNameValueProvider() {
  return new ValueProvider<Pet, String>() {
    @Override
    public String getPath() {
      return "Pet.PetName";
    }
    @Override
    public String getValue(Pet pet) {
      return pet._petName;
    }
    @Override
    public void setValue(Pet pet, final String value) {
      pet._petName = value;
    }
  };
}
public Pet(int petId, final String petName) {
  _petId = petId;
  _petName = petName;
}
public int getPetId() {
  return _petId;
}
}
//-------------------------------------------------------------------------
}

【讨论】:

  • 是否也解决了行首显示toString的问题?
  • 是的,因为现在在编辑模式下,行首有一个CheckBox 字段。请注意,我刚刚更改了代码,只是在编辑模式下禁用了此复选框,这大大简化了解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 2016-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多