使用ui:field声明将ID添加到字段 [英] Add id to field with ui:field declaration

查看:141
本文介绍了使用ui:field声明将ID添加到字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的UiBinder XML中声明这些元素:

 < label for =lastName>姓氏:< / label> 
< input type =textid =lastNameui:field =lastNameFieldmaxlength =150/>

简单地说,就是一个与文本输入相关联的标签。



然而,当我尝试编译时,我得到这个错误:

lockquote

元素(:23)

这似乎是一个愚蠢的限制,特别是因为 ui:field 不会生成一个ID。我迄今为止唯一找到的解决方案就是像这样在Java代码中分配ID:

  @UiElement InputElement lastNameField ; 
...
lastNameField.setId(lastName);

这给我的Java增加了不必要的混乱。它还增加了一些复杂性,如果这个ID被更新到某处,XML中的< label> 声明也需要更新(并且没有@UiElement对于标签,所以它在Java方面几乎完全不可见。)



是否有方法将ID添加到具有ui:field的元素声明来自UiBinder XML本身?

解决方案

UiBinder使用ID来实现它的 ui:field magic,所以你不能从XML中设置它。



实现它的方法是让Java常量使用ID并从双方使用它:

  @UiField(provided = true)
final String lastNameId = Document.get()。createUniqueId();

@UiField InputElement lastNameField;

...

lastNameField.setId(LAST_NAME_ID);

和XML:

 < ui:with field =lastNameIdtype =java.lang.String/> 

...

< label for ={lastNameId}>姓氏:< / label>
< input ui:field =lastNameFieldmaxlength =150/>

请注意,我没有用 type =java测试上面的代码.lang.String,我总是使用包含各种标识符的
a类(或者更确切地说,包含一个生成器的接口)。

<如果可以的话,可以使用< label>的替代语法

/ code>:

 < label>姓氏:< input ui:field = lastNameFieldmaxlength =150/>< / label> 


  • 阅读 for =值从Java使用它在 setId(),这样至少你可以消除重复,但你仍然有问题,你的ID可能不会是唯一的(只要您多次使用您的UiBinder-widget)

     < label ui:field = lastNameLabelfor =lastName>姓氏:< / label> 
    < input ui:field =lastNameFieldmaxlength =150/>



      @UiField LabelElement lastNameLabel; 
    @UiField InputElement lastNameField;

    ...

    lastNameField.setIf(lastNameLabel.getHtmlFor());



  • I'm trying to declare these elements in my UiBinder XML:

    <label for="lastName">Last Name:</label>
    <input type="text" id="lastName" ui:field="lastNameField" maxlength="150" />
    

    Simply put, a label that is associated with a text input.

    When I try to compile, however, I get this error:

    [ERROR] Cannot declare id="lastName" and ui:field="lastNameField" on the same element Element (:23)

    This seems like an idiotic restriction, especially since ui:field doesn't generate an ID. The only solution I've found so far is to assign the ID in the Java code itself like this:

    @UiElement InputElement lastNameField;
    ...
    lastNameField.setId("lastName");
    

    This adds needless clutter to my Java. It also adds the complication that if this ID gets updated somewhere down the line, the <label> declaration in the XML will also need to be updated (and there's no @UiElement for the label, so it's pretty much completely invisible from the Java side.)

    Is there a way to add an ID to an element with a ui:field declaration from within the UiBinder XML itself?

    解决方案

    UiBinder uses the ID to implement its ui:field magic, so no you can't set it from the XML.

    The way to do it is to have a Java constant with the ID and use it from both sides:

    @UiField(provided = true)
    final String lastNameId = Document.get().createUniqueId();
    
    @UiField InputElement lastNameField;
    
    …
    
    lastNameField.setId(LAST_NAME_ID);
    

    and in the XML:

    <ui:with field="lastNameId" type="java.lang.String"/>
    
    …
    
    <label for="{lastNameId}">Last Name:</label>
    <input ui:field="lastNameField" maxlength="150"/>
    

    Note that I haven't tested the above code with type="java.lang.String", I've always used a class containing various identifiers instead (or rather, an interface with a generator)

    Alternatives are:

    • if you can, use the alternate syntax for <label>:

      <label>Last Name: <input ui:field="lastNameField" maxlength="150"/></label>
      

    • read the for="" value from Java to use it in setId(), that way at least you remove duplication, but you'll still have the issue that your IDs possibly won't be unique (as soon as you use your UiBinder-widget more than once)

      <label ui:field="lastNameLabel" for="lastName">Last Name:</label>
      <input ui:field="lastNameField" maxlength="150" />
      

      @UiField LabelElement lastNameLabel;
      @UiField InputElement lastNameField;
      
      …
      
      lastNameField.setIf(lastNameLabel.getHtmlFor());
      

    这篇关于使用ui:field声明将ID添加到字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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