如何在GWT dataGrid中添加自定义按钮?

时间:2014-02-16 10:29:29

标签: gwt

我想要一列满按钮[我自己的自定义按钮],并希望在我的应用程序中为这些按钮设置点击处理程序?如何创建代码?

由于


Cell cell = new AbstractCell()     {

    @Override
    public void render( com.google.gwt.cell.client.Cell.Context context, String value, SafeHtmlBuilder sb )
    {
        FlowPanel fp = new FlowPanel();
        custombutton tb = new custombutton ("click");
        tb.setText( value );
        fp.add( tb );
        sb.appendHtmlConstant( fp.getElement().getString() );
    }
};
// Address.
Column<ContactInfo, String> addressColumn = new Column<ContactInfo, String>( cell )
{
    @Override
    public String getValue( ContactInfo object )
    {
        return object.getAddress();
   }

这是我的代码,如果ContactInfo.getid()为true,我应该为该列创建按钮,但现在是为所有列创建的?请提出一些想法?,

3 个答案:

答案 0 :(得分:0)

您可以使用ActionCell,然后对其应用CSS,使其看起来像您喜欢的任何方式。

请参阅GWT Showcase,了解如何执行此操作:http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellSampler

答案 1 :(得分:0)

我通过创建一个接受自定义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元素中,我们可以获取该span元素的id,并将相应的action对象设置为clicked状态。

@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);
        }
    }
  }
}

在值更新程序内部,根据单击的状态,调用操作对象单击处理程序方法。

答案 2 :(得分:0)

public static Column<VolunteerTO, VolunteerTO> createReissueButtonColumn(String columnName) {

    ActionCell<VolunteerTO> reListCell = new ActionCell<VolunteerTO>("Reissue", new ActionCell.Delegate<VolunteerTO>() {
                @Override
                public void execute(VolunteerTO object) {
                   // code to be executed 
                }
             }) 
         {
             @Override
             public void render(Cell.Context context,VolunteerTO value,SafeHtmlBuilder sb) {
                 if(null != value.getVolunteerStatus()  && !"".equalsIgnoreCase(value.getVolunteerStatus())) {
                     super.render(context,value,sb);
                 }

             }


         };

         Column<VolunteerTO, VolunteerTO> reListColumn = new Column<VolunteerTO, VolunteerTO>(reListCell) {
              @Override
              public VolunteerTO getValue(VolunteerTO object) {
                return object;
              }
                public String getCellStyleNames(Cell.Context context, VolunteerTO object) {

                if(object.getVolunteerStatus().equalsIgnoreCase(GatesClientConstants.ISSUED)|| object.getVolunteerStatus().equalsIgnoreCase(GatesClientConstants.PROTECTED)){
                    return "myButtonStyle";
                }else if(object.getVolunteerStatus().equalsIgnoreCase(GatesClientConstants.ACCEPTED)|| object.getVolunteerStatus().equalsIgnoreCase(GatesClientConstants.RELEASED)){
                    return "btn_disable";
                }
                return "leftalign";

            }

         };
         reListColumn.setDataStoreName(columnName);
         reListColumn.setSortable(false);
    return reListColumn;

}
相关问题