How can I add unique id to a custom cell?

只愿长相守 提交于 2019-12-08 20:54:36

问题


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?


回答1:


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>"; 
}



回答2:


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++;
  }-*/;



回答3:


Generally when I do this sort of thing I add a prefix to it. so ID="sec_22" where sec_ is the prefix. Then I know that section has something unique.




回答4:


I wanted to set an id to a TextCell and I did like this

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 the id equals to the text value.



来源:https://stackoverflow.com/questions/8123354/how-can-i-add-unique-id-to-a-custom-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!