找到你要的答案

Q:Composite cell event Handling

Q:复合信元事件处理

I'm using a composite cell in a grid which has two buttons.I want to handle the click events on both the buttons seperately.Is it possible? please explain in detail how to handle these events seperately.

I'm attaching my composite cell code with the buttons for the reference.

private CompositeCell<FilterInfo> createCompositeCell(){

HasCell<FilterInfo, String> button1= new HasCell<FilterInfo, String>() {

  public Cell<String> getCell() {
    return new TextCell();
  }

  public FieldUpdater<FilterInfo, String> getFieldUpdater() {
    // TODO Auto-generated method stub
    return null;
  }

  public String getValue(FilterInfo object) {
    return "button 1";
  }};

  HasCell<FilterInfo, String> button2= new HasCell<FilterInfo,String>(){

    public Cell<String> getCell() {
      return new ButtonCell();
    }

    public FieldUpdater<FilterInfo, String> getFieldUpdater() {
      // TODO Auto-generated method stub
      return null;
    }

    public String getValue(FilterInfo object) {
      // TODO Auto-generated method stub
      return "button 2";
    }
  };

  List<HasCell<FilterInfo, ?>> cells = new ArrayList<HasCell<FilterInfo, ?>>();
  cells.add(button1);
  cells.add(button2);

  CompositeCell<FilterInfo> compositeCell = new CompositeCell<FilterInfo>(cells);

  return compositeCell;

I'm using a composite cell in a grid which has two buttons.I want to handle the click events on both the buttons seperately.Is it possible? please explain in detail how to handle these events seperately.

我附上我的复合细胞代码与按钮的参考。

private CompositeCell<FilterInfo> createCompositeCell(){

HasCell<FilterInfo, String> button1= new HasCell<FilterInfo, String>() {

  public Cell<String> getCell() {
    return new TextCell();
  }

  public FieldUpdater<FilterInfo, String> getFieldUpdater() {
    // TODO Auto-generated method stub
    return null;
  }

  public String getValue(FilterInfo object) {
    return "button 1";
  }};

  HasCell<FilterInfo, String> button2= new HasCell<FilterInfo,String>(){

    public Cell<String> getCell() {
      return new ButtonCell();
    }

    public FieldUpdater<FilterInfo, String> getFieldUpdater() {
      // TODO Auto-generated method stub
      return null;
    }

    public String getValue(FilterInfo object) {
      // TODO Auto-generated method stub
      return "button 2";
    }
  };

  List<HasCell<FilterInfo, ?>> cells = new ArrayList<HasCell<FilterInfo, ?>>();
  cells.add(button1);
  cells.add(button2);

  CompositeCell<FilterInfo> compositeCell = new CompositeCell<FilterInfo>(cells);

  return compositeCell;
answer1: 回答1:

You handle click event in FieldUpdater of each button.

Here is minimal working example:

package com.adam.test.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.CompositeCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

public class Test implements EntryPoint {

    @Override
    public void onModuleLoad() {
        CompositeCell<FilterInfo> compositeCell = createCompositeCell();
        CellList<FilterInfo> list = new CellList<FilterInfo>(compositeCell);

        List<FilterInfo> values = new ArrayList<FilterInfo>();
        values.add(new FilterInfo());
        values.add(new FilterInfo());
        values.add(new FilterInfo());
        list.setRowData(values);

        RootPanel.get().add(list);
    }

    private CompositeCell<FilterInfo> createCompositeCell() {
        Column<FilterInfo, String> button1Column = new Column<FilterInfo, String>(new ButtonCell()) {
            @Override
            public String getValue(FilterInfo object) {
                return "Button 1";
            }
        };
        button1Column.setFieldUpdater(new FieldUpdater<FilterInfo, String>() {
            @Override
            public void update(int index, FilterInfo object, String value) {
                Window.alert("Button 1 clicked!");
            }
        });

        Column<FilterInfo, String> button2Column = new Column<FilterInfo, String>(new ButtonCell()) {
            @Override
            public String getValue(FilterInfo object) {
                return "Button 2";
            }
        };
        button2Column.setFieldUpdater(new FieldUpdater<FilterInfo, String>() {
            @Override
            public void update(int index, FilterInfo object, String value) {
                Window.alert("Button 2 clicked!");
            }
        });

        ArrayList<HasCell<FilterInfo, ?>> compositeColumns = new ArrayList<HasCell<FilterInfo, ?>>();
        compositeColumns.add(button1Column);
        compositeColumns.add(button2Column);

        return new CompositeCell<FilterInfo>(compositeColumns);
    }

    private class FilterInfo {}
}

你把每个按钮的点击事件,FieldUpdater。

Here is minimal working example:

package com.adam.test.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.CompositeCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

public class Test implements EntryPoint {

    @Override
    public void onModuleLoad() {
        CompositeCell<FilterInfo> compositeCell = createCompositeCell();
        CellList<FilterInfo> list = new CellList<FilterInfo>(compositeCell);

        List<FilterInfo> values = new ArrayList<FilterInfo>();
        values.add(new FilterInfo());
        values.add(new FilterInfo());
        values.add(new FilterInfo());
        list.setRowData(values);

        RootPanel.get().add(list);
    }

    private CompositeCell<FilterInfo> createCompositeCell() {
        Column<FilterInfo, String> button1Column = new Column<FilterInfo, String>(new ButtonCell()) {
            @Override
            public String getValue(FilterInfo object) {
                return "Button 1";
            }
        };
        button1Column.setFieldUpdater(new FieldUpdater<FilterInfo, String>() {
            @Override
            public void update(int index, FilterInfo object, String value) {
                Window.alert("Button 1 clicked!");
            }
        });

        Column<FilterInfo, String> button2Column = new Column<FilterInfo, String>(new ButtonCell()) {
            @Override
            public String getValue(FilterInfo object) {
                return "Button 2";
            }
        };
        button2Column.setFieldUpdater(new FieldUpdater<FilterInfo, String>() {
            @Override
            public void update(int index, FilterInfo object, String value) {
                Window.alert("Button 2 clicked!");
            }
        });

        ArrayList<HasCell<FilterInfo, ?>> compositeColumns = new ArrayList<HasCell<FilterInfo, ?>>();
        compositeColumns.add(button1Column);
        compositeColumns.add(button2Column);

        return new CompositeCell<FilterInfo>(compositeColumns);
    }

    private class FilterInfo {}
}
answer2: 回答2:

I would suggest you to create the custom cell and override the onBrowserEvent and render methods. Handle the click event in the onBrowserEvent method.

Refer http://www.gwtproject.org/doc/latest/DevGuideUiCustomCells.html

我建议你创建自定义细胞和覆盖onbrowserevent和渲染方法。在onbrowserevent方法处理单击事件。

参考http://www.gwtproject.org/doc/latest/devguideuicustomcells.html

gwt  grid  gxt  uibinder