Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add unique id to a custom cell?

Tags:

testing

cell

gwt

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?

like image 694
Croo Avatar asked Nov 14 '11 14:11

Croo


People also ask

How do you assign a unique ID in Excel?

type 1 into the cell which is adjacent to the first data you want to add ID number. 2. Then in the cell below it, type this formula =IF(B1=B2,A1,A1+1), press Enter key to get the first result, drag fill handle down until last data showing up.

How do I get a unique identifier?

The simplest way to generate identifiers is by a serial number. A steadily increasing number that is assigned to whatever you need to identify next. This is the approached used in most internal databases as well as some commonly encountered public identifiers.


2 Answers

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>"; 
}
like image 69
Chris Cashwell Avatar answered Sep 22 '22 03:09

Chris Cashwell


You can use

Document.get().createUniqueId();

Here 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's 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++;
  }-*/;
like image 31
Stefan Avatar answered Sep 23 '22 03:09

Stefan