非常有用的。下面是我对一个有条件地呈现CheckboxCell
import com.google.gwt.cell.client.AbstractCell; import com.google.gwt.cell.client.CheckboxCell; import com.google.gwt.cell.client.ValueUpdater; import com.google.gwt.dom.client.Element; import com.google.gwt.dom.client.EventTarget; import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.safehtml.shared.SafeHtmlBuilder; /** * CheckboxCell
that is conditionally rendered if the enclosing column's * Boolean com.google.gwt.user.cellview.client.Column.getValue(T object)
method returns true
.
*/
public class ConditionallyRenderedCheckboxCell extends AbstractCell {
public ConditionallyRenderedCheckboxCell() {
//We handle the same events as CheckboxCell
super("change", "keydown");
}
private CheckboxCell cell = null;
@Override
public void render(Context context, Boolean renderCheckboxCell, SafeHtmlBuilder sb) {
if (renderCheckboxCell) {
this.cell = new CheckboxCell(false,true);
//Render the checkbox cell unchecked
this.cell.render(context, false, sb);
}
}
@Override
public void onBrowserEvent(com.google.gwt.cell.client.Cell.Context context,
Element parent, Boolean value, NativeEvent event,
ValueUpdater valueUpdater) {
//If we have created a checkbox cell, do event handling, otherwise, ignore it.
if(this.cell != null){
super.onBrowserEvent(context, parent, value, event, valueUpdater);
// Handle the change event.
if ("change".equals(event.getType())) {
// Ignore events that occur outside of the outermost element.
EventTarget eventTarget = event.getEventTarget();
if (parent.isOrHasChild(Element.as(eventTarget))) {
// Use this to get the selected element!!
Element el = Element.as(eventTarget);
//Check if we really clicked on the checkbox
if (el.getNodeName().equalsIgnoreCase("input") && el.getPropertyString("type").equalsIgnoreCase("checkbox")) {
//If an value updater was defined, invoke it
if(valueUpdater != null)
valueUpdater.update(el.getPropertyBoolean("checked"));
}
}
}
}
}
}
有趣,我会试试这个,然后报告! – theboot 2011-05-13 19:29:16