كيفية التعامل مع النقر فوق حدث أو أي حدث آخر في GWT Cloud؟
سؤال
أنا أستخدم tagcloud_0.4.jar لاستخدام Tag Cloud في GWT. الآن ، عندما أقوم بالنقر فوق أي علامة داخل سحابة العلامات ، فكيف يمكنني التعامل مع هذا الحدث أو كيف يمكنني إجراء مكالمة RPC عندما يحدد المستخدم أي علامة من Cloud؟
شكرًا.
المحلول
فيما يلي مثال للقبض على clickevent على السحابة. ولكن هذا حل بديل حيث قد تتغير المكتبة. نظرًا لأن التطوير لا يعرض إدارة الأحداث على السحابة لا على العلامات ، فربما يكون هذا لسبب وجيه ...
لذا استخدم هذا الرمز على مسؤوليتك الخاصة. نصلي من أجل أي تغيير في هيكل القطعة دوم. أقترح عليك أن تطلب من التطوير بعض التحسينات على عناصر واجهة المستخدم ، مثل إدارة مثل هذه الأحداث.
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());
لا تنتمي إلى StackOverflow