How to add a unique identifier to a custom cell?

In a gwt project, I have a CellTree with custom cells. For easier testing, I would like to add identifiers for each of the cells. I know I can do 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 do not need to write identifiers in the code. Is there any way to do this?

+6
source share
4 answers

I would do something between the two above approaches. You should definitely add a prefix to make sure that you can easily identify cells during testing, and you should also use createUniqueId() rather than generating your own UUIDs, which can be more annoying.

 @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>"; } 
+2
source

you can use

 Document.get().createUniqueId(); 

Here is the description:

 /** * Creates an identifier guaranteed to be unique within this document. * * This is useful for allocating element id's. * * @return a unique identifier */ public final native String createUniqueId() /*-{ // In order to force uid to be document-unique across multiple modules, // we hang a counter from the document. if (!this.gwt_uid) { this.gwt_uid = 1; } return "gwt-uid-" + this.gwt_uid++; }-*/; 
+1
source

Usually, when I do such things, I add a prefix to it. therefore, ID = "sec_22", where sec_ is the prefix. Then I know that the section has something unique.

0
source

I wanted to set id in TextCell and I liked it

 import com.google.gwt.cell.client.TextCell; import com.google.gwt.core.client.GWT; import com.google.gwt.safehtml.client.SafeHtmlTemplates; import com.google.gwt.safehtml.shared.SafeHtml; import com.google.gwt.safehtml.shared.SafeHtmlBuilder; public class EnsuredDbgIdTextCell extends TextCell { private static EnsuredDbgIdTextCellTemplate template = null; public EnsuredDbgIdTextCell() { super(); if (template == null) { template = GWT.create(EnsuredDbgIdTextCellTemplate.class); } } public interface EnsuredDbgIdTextCellTemplate extends SafeHtmlTemplates { @Template("<div id=\"{0}\" style=\"outline:none;\" tabindex=\"0\">{0}</div>") SafeHtml withValueAsDebugId(String value); } @Override public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) { if (value != null) { sb.append(template.withValueAsDebugId(value.asString())); } } } 

I set id equal to text value.

0
source

Source: https://habr.com/ru/post/901442/


All Articles