0
我正在使用tagcloud_0.4.jar在GWT中使用標籤雲。現在,當我點擊標籤雲中的任何標籤時,如何處理該事件,或者當用戶從雲中選擇任何標籤時,如何進行RPC調用?如何處理GWT雲中的Click事件或任何其他事件?
謝謝。
我正在使用tagcloud_0.4.jar在GWT中使用標籤雲。現在,當我點擊標籤雲中的任何標籤時,如何處理該事件,或者當用戶從雲中選擇任何標籤時,如何進行RPC調用?如何處理GWT雲中的Click事件或任何其他事件?
謝謝。
以下是捕捉雲端ClickEvent的示例。但是,這是一個解決方法,因爲圖書館可能會改變。由於開發者不提供在雲上管理事件,所以這也許是一個很好的理由...
因此,使用此代碼需要您自擔風險。祈求沒有改變小部件的結構DOM。我建議你向開發者詢問其小部件的一些增強功能,例如管理這些事件。
final TagCloud cloud = new TagCloud();
cloud.addWord(new WordTag("AAA"));
cloud.addWord(new WordTag("AAA"));
cloud.addWord(new WordTag("BBB"));
cloud.addWord(new WordTag("CCC"));
cloud.addWord(new WordTag("CCC"));
cloud.addWord(new WordTag("CCC"));
cloud.addDomHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
// Prevent the click from the tag to be executed
event.preventDefault();
Element e;
// For each tag present in the cloud, look for the one that is triggered
for (int i = 0; i < cloud.getTags().size(); ++i) {
// Gets the element in the cloud, this really is the unsafe part of the code
// if the developer change the dom of its widget
e = DOM.getChild(DOM.getChild(cloud.getElement(), 0), i);
// Is the current element targeted by the event?
if (event.getClientX() >= e.getOffsetLeft() && event.getClientY() >= e.getOffsetTop()
&& event.getClientX() <= e.getOffsetLeft() + e.getOffsetWidth()
&& event.getClientY() <= e.getOffsetTop() + e.getOffsetHeight()) {
// Gets the abstract tag
Tag t = cloud.getTags().get(i);
// Gets tag descendant
if (t instanceof WordTag) {
// Do what you want with your WordTag, maybe an RPC call
} else if (t instanceof ImageTag) {
// Do what you want with your ImageTag, maybe an RPC call
}
break;
}
}
}
}, ClickEvent.getType());