我如何添加唯一的ID到自定义单元格? [英] How can I add unique id to a custom cell?

查看:121
本文介绍了我如何添加唯一的ID到自定义单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个gwt项目中,我有一个带有自定义单元格的CellTree。为了便于测试,我想为每个单元添加ID。
我知道我可以这样做:

  @Override 
public void render(Context context, TreeElement value,SafeHtmlBuilder sb){
if(value == null){return;}
sb.appendHtmlConstant(< div id = \+ value.getID()+\ >+
value.getName())+< / div>;
}

但是我想用类似EnsureDebugID()的东西,不得不刻录代码中的ID。有没有办法做到这一点?

解决方案

我会在上述两种方法之间做些什么。您一定要添加一个前缀以确保您可以在测试期间轻松识别这些单元格,并且您还应该使用 createUniqueId() 方法而非

<@ p $ p> @Override
public void render(Context context,TreeElement value, SafeHtmlBuilder sb){
if(value == null){return;}
String id = Document.get()。createUniqueId();
sb.appendHtmlConstant(< div id = \cell _+ id +\>+
value.getName())+< / div>;
}


In a gwt project I have a CellTree with custom cells. For easier testing I would like to add IDs for each of the cells. I know I can make it like this :

@Override
public void render(Context context,TreeElement value, SafeHtmlBuilder sb) {             
            if (value == null) {return;}
            sb.appendHtmlConstant("<div id=\""+value.getID()+"\">" + 
                                       value.getName()) + "</div>"; 
}

But I would like to use something similar to EnsureDebugID() so I don't have to burn the IDs in the code. Is there a way to do that?

解决方案

I would do something between the two of the aforementioned approaches. You should definitely add a prefix to be sure you can easily identify the cells during testing, and you should also take the createUniqueId() approach rather than generating your own UUIDs which can be more troublesome.

@Override
public void render(Context context, TreeElement value, SafeHtmlBuilder sb) {             
    if (value == null) {return;}
    String id = Document.get().createUniqueId();
    sb.appendHtmlConstant("<div id=\"cell_"+id+"\">" + 
                           value.getName()) + "</div>"; 
}

这篇关于我如何添加唯一的ID到自定义单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆