Java GWT单元格表添加基于对象的selectionCell
我正在遵循单元格表的GWT showcase示例,但无法让它完全满足我的要求。我向单元格表传递一个成员列表,每个成员都有一个数字数组列表。我想把那个数字列表放在selectionCell中,但据我所知,这是不可能的。GWT示例单独查询类别,然后将其放入selectioncell,而我希望selectioncell由传递到表的对象填充。以下是相关的GWT代码Java GWT单元格表添加基于对象的selectionCell,java,gwt,Java,Gwt,我正在遵循单元格表的GWT showcase示例,但无法让它完全满足我的要求。我向单元格表传递一个成员列表,每个成员都有一个数字数组列表。我想把那个数字列表放在selectionCell中,但据我所知,这是不可能的。GWT示例单独查询类别,然后将其放入selectioncell,而我希望selectioncell由传递到表的对象填充。以下是相关的GWT代码 final Category[] categories = ContactDatabase.get().queryCategories();
final Category[] categories = ContactDatabase.get().queryCategories();
List<String> categoryNames = new ArrayList<String>();
for (Category category : categories) {
categoryNames.add(category.getDisplayName());
}
SelectionCell categoryCell = new SelectionCell(categoryNames);
Column<ContactInfo, String> categoryColumn = new Column<ContactInfo, String>(
categoryCell) {
@Override
public String getValue(ContactInfo object) {
return object.getCategory().getDisplayName();
}
};
final Category[]categories=ContactDatabase.get().queryCategories();
List categoryNames=new ArrayList();
用于(类别:类别){
添加(category.getDisplayName());
}
SelectionCell categoryCell=新的SelectionCell(categoryNames);
列类别Column=新列(
分类细胞){
@凌驾
公共字符串getValue(ContactInfo对象){
返回object.getCategory().getDisplayName();
}
};
这是我迄今为止的尝试
List<String> lotNumbers = new ArrayList<String>();
//this doesn't work, I can't call object.getLotNumbers();
SelectionCell lotNumberCell = new SelectionCell(lotNumbers);
Column<Member, String> lotNumberColumn = new Column<Member, String>(lotNumberCell) {
@Override
public String getValue(Member object) {
return object.getLotNumbers().get(0);
}
};
List lotNumbers=new ArrayList();
//这不起作用,我不能调用object.getLotNumbers();
SelectionCell lotNumberCell=新的SelectionCell(lotNumbers);
列lotNumberColumn=新列(lotNumberCell){
@凌驾
公共字符串getValue(成员对象){
返回object.getLotNumbers().get(0);
}
};
SelectionCell未设置为动态呈现内容,它使用静态字符串列表在每次显示下拉列表时呈现下拉列表。如果你想有一个动态下拉列表,你必须创建一个新的单元格类型来实现单元格
,或者其他一些类型,你可以迭代来生成下拉列表。这是我对DynamicSelectionCell的实现
使用单个DynamicSelectionCell对象并使用addOption方法为每行添加选项。选项存储在地图中,键为行号
对于表中的每一行$i,将呈现存储在映射中的键$i的选项
在DataGrid、CellTable上工作
代码
公共类DynamicSelectionCell扩展了AbstractInputCell{
公共树映射选项映射=新树映射();
接口模板扩展了安全HtmlTemplates{
@模板(“{0}”)
取消选择安全HTML(字符串选项);
@模板(“{0}”)
选择安全HTML(字符串选项);
}
私有静态模板;
private TreeMap indexForOption=new TreeMap();
/**
*使用指定的选项构造一个新的{@link SelectionCell}。
*
*@param options单元格中的选项
*/
公共动态选择单元(){
超级(“变更”);
如果(模板==null){
template=GWT.create(template.class);
}
}
public void addOption(List newOps,int key){
选项映射。放置(键,新操作);
HashMap localIndexForOption=新HashMap();
输入(ind,localIndexForOption);
刷新索引();
}
public void removeOption(int索引){
选项映射。删除(索引);
刷新索引();
}
私有索引(){
int ind=0;
对于(列表选项:optionMap.values()){
HashMap localIndexForOption=新HashMap();
输入(ind,localIndexForOption);
int指数=0;
用于(字符串选项:选项){
localIndexForOption.put(选项,索引++);
}
ind++;
}
}
@凌驾
public void onBrowserEvent(上下文上下文、元素父级、字符串值、,
NativeEvent事件,ValueUpdater(ValueUpdater){
onBrowserEvent(上下文、父级、值、事件、值更新程序);
字符串类型=event.getType();
如果(“变更”。等于(类型)){
Object key=context.getKey();
SelectElement select=parent.getFirstChild().cast();
String newValue=optionsMap.get(context.getIndex()).get(select.getSelectedIndex());
setViewData(键,新值);
完成编辑(父项、新值、键、值更新程序);
if(valueUpdater!=null){
valueUpdater.update(newValue);
}
}
}
@凌驾
公共void呈现(上下文上下文、字符串值、安全HtmlBuilder sb){
//获取视图数据。
Object key=context.getKey();
字符串viewData=getViewData(键);
if(viewData!=null&&viewData.equals(value)){
clearViewData(键);
viewData=null;
}
int-selectedIndex=getSelectedIndex(viewData==null?值:viewData,context.getIndex());
某人以““””号结尾;
int指数=0;
试一试{
for(字符串选项:optionsMap.get(context.getIndex())){
如果(索引+==selectedIndex){
sb追加(模板选择(选项));
}否则{
sb追加(模板取消选择(选项));
}
}
}捕获(例外e){
System.out.println(“错误”);
}
某人以““””号结尾;
}
私有int getSelectedIndex(字符串值,int ind){
整数索引=indexForOption.get(ind).get(value);
如果(索引==null){
返回-1;
}
返回index.intValue();
}
}
您好nwilde97您能详细介绍一下如何实现Cell吗
public class DynamicSelectionCell extends AbstractInputCell<String, String> {
public TreeMap<Integer, List<String>> optionsMap = new TreeMap<Integer, List<String>>();
interface Template extends SafeHtmlTemplates {
@Template("<option value=\"{0}\">{0}</option>")
SafeHtml deselected(String option);
@Template("<option value=\"{0}\" selected=\"selected\">{0}</option>")
SafeHtml selected(String option);
}
private static Template template;
private TreeMap<Integer, HashMap<String, Integer>> indexForOption = new TreeMap<Integer, HashMap<String, Integer>>();
/**
* Construct a new {@link SelectionCell} with the specified options.
*
* @param options the options in the cell
*/
public DynamicSelectionCell() {
super("change");
if (template == null) {
template = GWT.create(Template.class);
}
}
public void addOption(List<String> newOps, int key){
optionsMap.put(key, newOps);
HashMap<String, Integer> localIndexForOption = new HashMap<String, Integer>();
indexForOption.put(ind, localIndexForOption);
refreshIndexes();
}
public void removeOption(int index){
optionsMap.remove(index);
refreshIndexes();
}
private void refreshIndexes(){
int ind=0;
for (List<String> options : optionsMap.values()){
HashMap<String, Integer> localIndexForOption = new HashMap<String, Integer>();
indexForOption.put(ind, localIndexForOption);
int index = 0;
for (String option : options) {
localIndexForOption.put(option, index++);
}
ind++;
}
}
@Override
public void onBrowserEvent(Context context, Element parent, String value,
NativeEvent event, ValueUpdater<String> valueUpdater) {
super.onBrowserEvent(context, parent, value, event, valueUpdater);
String type = event.getType();
if ("change".equals(type)) {
Object key = context.getKey();
SelectElement select = parent.getFirstChild().cast();
String newValue = optionsMap.get(context.getIndex()).get(select.getSelectedIndex());
setViewData(key, newValue);
finishEditing(parent, newValue, key, valueUpdater);
if (valueUpdater != null) {
valueUpdater.update(newValue);
}
}
}
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
// Get the view data.
Object key = context.getKey();
String viewData = getViewData(key);
if (viewData != null && viewData.equals(value)) {
clearViewData(key);
viewData = null;
}
int selectedIndex = getSelectedIndex(viewData == null ? value : viewData, context.getIndex());
sb.appendHtmlConstant("<select tabindex=\"-1\">");
int index = 0;
try{
for (String option : optionsMap.get(context.getIndex())) {
if (index++ == selectedIndex) {
sb.append(template.selected(option));
} else {
sb.append(template.deselected(option));
}
}
}catch(Exception e){
System.out.println("error");
}
sb.appendHtmlConstant("</select>");
}
private int getSelectedIndex(String value, int ind) {
Integer index = indexForOption.get(ind).get(value);
if (index == null) {
return -1;
}
return index.intValue();
}
}