【问题标题】:Setting element ID in GWT在 GWT 中设置元素 ID
【发布时间】:2013-01-10 10:08:57
【问题描述】:

我正在尝试将 ID 添加到 GWT 元素。我试过下面的代码。

在SubscribeView.java 文件中,定义了existingFundRadioButton 字段,我为它设置了一个ID,如下所示:

DOM.setElementAttribute( existingFundRadioButton.getElement(), 
                         "ID",
                         "existingFundRadioButton" )

我尝试使用existingFundRadioButton.getElement().setId("existingFundRadioButton"),但它不起作用。

这是我的代码。

<g:FlowPanel width="100%">
    <g:FlowPanel>

        <g:HTMLPanel ui:field='searchPanel' styleName="{res.fdcWidgets.box}">
            <table class="{res.fdcWidgets.inputTable}">
                <colgroup>
                    <col width="25%" />
                    <col width="75%" />
                </colgroup>
                <tbody>
                    <tr>
                        <td>
                            <fdc:I18NRadioButton name="fund"
                                ui:field="existingFundRadioButton" checked="true"
                                messageKey="buy.existingFundLabel" />
                        </td>
                        <td>
                            <hli:HoldingsListBoxWidget ui:field="holdingsListBox" />
                        </td>
                    </tr>

                </tbody>
            </table>

        </g:HTMLPanel>
    </g:FlowPanel>
    <g:FlowPanel styleName="{res.fdcWidgets.contentRightColumn}">
        <fund:FundInformationWidget ui:field="fundInformationWidget" />
    </g:FlowPanel>
</g:FlowPanel>

【问题讨论】:

  • 如果通过选择答案或发布您自己的答案得到解决,最好关闭问题。如果问题没有被关闭,Stackoverflow 将不会有这么大的帮助!!!

标签: java gwt qtp


【解决方案1】:

旧解决方案

如果您卡在旧版本的 GWT - 对于 RadioButton,我们使用特定于浏览器的方法。我们通过扩展 RadioButton 来实现这一点。

MyRadioButton extends RadioButton{
        public void setId( String id )
        {
            getElement().setId( id );
            Element child = DOM.getChild( getElement(), 0 );
            DOM.setElementAttribute( child, "id", id + CONST_INPUT_ELEMENT );
            final Element e_label = DOM.getChild( getElement(), 1 );
            // Handling IE
            DOM.setElementAttribute( e_label, "htmlFor", id + CONST_LABEL_ELEMENT );
            // Handling Other Browsers
            DOM.setElementAttribute( e_label, "for", id + CONST_LABEL_ELEMENT );
        }
}

新解决方案

对于那些有幸拥有在所有小部件中支持 ensureDebugId 的最新 GWT 的人,您可以在从 UIObject 继承的所有 ui 元素中使用 ensureDebugId("someid")!!!!

【讨论】:

  • 感谢您的回复。我尝试使用 elementname.ensureDebugId("abc") 并且成功了。
  • 谢谢你!!!我们从来没有更新过这种方法,因为之前的方法是有效的!是时候更新我们的包装器了:)。当然,在某些旧版本的 GWT 中可能需要上述答案。
  • 您可以在 java 代码中添加 ensureDebugId("abc") 方法,或者您可以在 ui.xml 文件中设置 debugId 属性...它对我有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多