2011-11-14 12 views
6

In un progetto gwt ho un CellTree con celle personalizzate. Per facilitare i test vorrei aggiungere ID per ciascuna cella. So che posso farlo in questo modo:Come posso aggiungere un ID univoco a una cella personalizzata?

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

ma vorrei usare qualcosa di simile a EnsureDebugID() in modo da non devo bruciare gli ID nel codice. C'è un modo per farlo?

risposta

2

vorrei fare qualcosa tra i due degli approcci di cui sopra. Dovresti sicuramente aggiungere un prefisso per essere sicuro di poter identificare facilmente le celle durante il test, e dovresti anche prendere l'approccio createUniqueId() piuttosto che generare i tuoi UUID che possono essere più problematici.

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

Generalmente quando faccio questo genere di cose aggiungo un prefisso. quindi ID = "sec_22" dove sec_ è il prefisso. Allora so che quella sezione ha qualcosa di unico.

1

È possibile utilizzare

Document.get().createUniqueId(); 

Ecco la descrizione:

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

ho voluto impostare un id ad un TextCell e mi è piaciuto questo

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())); 
     } 
    } 

} 

ho impostato l'id uguale al valore di testo.

Problemi correlati