简体   繁体   English

GWT Button下拉列表在子表的单元格事件处理中

[英]Gwt Button dropdown in celltable event handling on children

I have too many buttons in a table and I'd like to replace them by a button that open a dropdown list of actions. 我的表格中有太多按钮,我想用一个可以打开下拉列表的按钮来代替它们。 However I don't really know how to handle the events from the dropdown items. 但是我真的不知道如何处理下拉菜单中的事件。 I manage to do it using a javascript function but it's not very practical because I can only pass primitive values. 我设法使用javascript函数来做到这一点,但这不是很实用,因为我只能传递原始值。

I also want to make it as a custom cell in the future to use it in different pages in my project so returning some html isn't very practical. 我还希望将来将其作为自定义单元,以在项目的不同页面中使用它,因此返回一些html不太实用。

Here's my code : 这是我的代码:

     final ButtonCell buttonInfoCell = new ButtonCell();
         Column<GwtStockProduct, String> buttonCell = new Column<GwtStockProduct, String>(buttonInfoCell) {

     @Override
     public void render(final Context context, final GwtStockProduct value, final SafeHtmlBuilder sb) {
     Div div = new Div();
     Div bG = new Div();
     div.add(bG);
     bG.addStyleName("btn-group");

     Button button = new Button();
     DropDownMenu dropDown = new DropDownMenu();

     Span span = new Span();
     span.addStyleName("caret");
     span.setVisible(false);
     button.add(span);

     button.getElement().setAttribute("style", "background-image: none !important; background-color: #234C78 !important;");
     // button.removeStyleName("");
     button.addStyleName("btn-hide-icon btn-blue");
     button.setDataToggle(Toggle.DROPDOWN);
     button.setText("Change stock");

     for (int i = 1; i < 5; ++i) {
     AnchorListItem item = new AnchorListItem();
     item.getElement().getFirstChildElement().removeAttribute("href");
     item.getElement().getFirstChildElement().setAttribute("onclick", "triggerClick('" + i + "')");
     item.setText("Item " + i);
     dropDown.add(item);
     }

     // dropDown.getElement().setAttribute("style", "position: relative !important;");
     bG.add(button);
     bG.add(dropDown);

     // sb.append(SafeHtmlUtils.fromTrustedString(buttonGroup));
     sb.appendHtmlConstant(div.getElement().getInnerHTML());
     }

     @Override
     public String getValue(final GwtStockProduct object) {
     // TODO Auto-generated method stub
     return null;
     }
     };

     stockTable.addColumn(buttonCell, "Actions");
     stockTable.setColumnWidth(buttonCell, 5, Unit.PCT);

I use SelectionCell to render a drop-down list of options. 我使用SelectionCell渲染选项的下拉列表。 Maybe that will help you: 也许会帮助您:

ArrayList<String> options = new ArrayList<String>();
options.add("choose an option..."); // the prompt text
options.add("option 1");
options.add("option 2");
// ...

final SelectionCell optionsCell = new SelectionCell(options);

Column<TableType, String> optionsColumn = new Column<TableType, String>(optionsCell) {
    @Override
    public String getValue(TableType object) {
        return null;
    }
};
optionsColumn.setFieldUpdater(new FieldUpdater<TableType, String>() {
    @Override
    public void update(int index, TableType object, String value) {
        if(value == "option 1")
            // process option 1
        else if(value == "option 2")
            // process option 2
        // ...

        // reset drop-down to show the prompt text
        optionsCell.clearViewData(object);
        redrawRow(index);
    }
});

table.addColumn(optionsColumn, "options");

The first option is just a prompt text and after each selection change the drop-down list is reset to show the prompt. 第一个选项只是提示文本,在每次选择更改后,都会重置下拉列表以显示提示。

The disadvantage is that you can not have different sets of options for different rows as the list is generated once for the whole column. 缺点是您不能为不同的行使用不同的选项集,因为整个列只生成一次列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM