我通過創建自定義的接受Action對象列表的新的自定義單元格中實現類似的功能。
Action{
String id;
boolean clicked;
ClickHandler handler;
}
CustomCell extends AbstractCell<List<Action>>{
}
在render方法中,我已經爲動作列表中的每個動作對象創建了一個具有「id」值的按鈕元素。
@Template("<button id=\"{0}\"></button>")
SafeHtml buttonHtml(String id);
public void render(Context context, List<Action> rowActions, SafeHtmlBuilder sb) {
for(Action action: rowActions){
sb.append(buttonHtml(action.getId()));
}
}
在onBrowserEvent,而當點擊發生時,如果在span元素髮生,我們可以得到跨度元素的ID,並設置相應的操作對象按狀態。
@Override
public void onBrowserEvent(Context context, Element parent, List<Action> value, NativeEvent event,
ValueUpdater<List<Action>> valueUpdater) {
// Let AbstractCell handle the keydown event.
super.onBrowserEvent(context, parent, value, event, valueUpdater);
// Handle the click event.
if ("click".equals(event.getType())) {
// Ignore clicks that occur outside of the outermost element.
EventTarget eventTarget = event.getEventTarget();
if (parent.isOrHasChild(Element.as(eventTarget))) {
// if (parent.getFirstChildElement().isOrHasChild(
// Element.as(eventTarget))) {
// use this to get the selected element!!
Element el = Element.as(eventTarget);
// check if we really click on the image
if (el.getTagName().equalsIgnoreCase("button")) {
for(Action action:value){
if(el.getAttribute("id").equals(action.getId())){
action.setClicked(true);
}
}
doAction(value,
valueUpdater);
}
}
}
}
在值更新器內部,基於點擊狀態,調用動作對象的點擊處理程序方法。
這些按鈕怎麼樣。 http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellSampler –
我想添加自定義按鈕? – user3192996