2012-09-02 23 views
2

有沒有方法讓Wicket中的DropDownChoice將工具提示(例如標題屬性)分配給各個選項元素?Wicket - DropDownChoice - 選項的標題(工具提示)

我有以下形式選擇框項目:

public class SelectBoxItem 
{ 
    private Long id; 
    private String label; 
    private String description; 
} 

所有項目都從數據庫加載。

我使用ChoiceRenderer配置DropDownChoice組件來使用ID作爲鍵和標籤作爲值。

現在我需要將它配置爲使用描述作爲工具提示消息。


我只在網上找到this related thread。瀏覽相關的Wicket類讓我得出與作者相同的結論,例如,這對於DropDownChoice/ChoiceRenderer類的當前版本來說可能是不可能的。是對的嗎?在那種情況下,是否有類似的組件會允許這樣做?


(對於我的代碼基礎的更全面的描述,請參閱my other question,我問了一下在同一範圍內不同的問題。)

回答

1

你試過重寫方法appendOptionHtml?你可以用它來追加所需的html(即title =「toolTipText」)。

4

這是我對這個問題的解決方案。非常感謝Andrea Del Bene的建議。

public class TitledDropDownChoice<T> extends DropDownChoice<T> { 

// ... constructors from superclass ... 

@Override 
protected void appendOptionHtml(AppendingStringBuffer buffer, 
     T choice, int index, String selected) { 

    super.appendOptionHtml(buffer, choice, index, selected); 

    // converts <option value="foo">bar</option> to 
    // <option value="foo" title="bar">bar</option> 
    String replString = "value=\"" + getChoiceRenderer() 
     .getIdValue(choice, index) + "\""; 
    int pos = buffer.indexOf(replString); 
    buffer.insert(pos + replString.length(), 
     " title=\"" + getChoiceRenderer().getDisplayValue(choice) + "\""); 

} 

}