我们可以动态更改 GWT Bootstrap 3 ButtonCell 图标吗?

Can we change GWT Bootstrap 3 ButtonCell Icon Dynamically?

我有一个 GWT bootstrap 3 按钮作为 ButtonCell 使用 IconType 和 ButtonType 创建:

public abstract class ButtonColumn<T> extends Column<T, String> {
    public ButtonColumn(IconType iconType, ButtonType buttonType) {
        this(new ButtonCell(buttonType, iconType));
    }
}

所以当我创建按钮时,我会

new ButtonColumn<Object>(IconType.PLAY, ButtonType.SUCCESS) {
  @Override
  public void onClick(Object obj) {
    doStuff(obj);
  }
};

我想更改我的按钮 IconType onClick。有可能实现吗? 我可以创建一个扩展 GWT 的自定义 IconType IconType Enum? I wanted to put an animated icon (like a loading icon).

嗯,您不能连续更改按钮的图标,尤其是当您创建已指定图标的整个列时。但是你可以 redraw() 连续,这可能是实现你想要的结果的一种方式。

我用AbstractCell to render a button and onBrowserEvent:

  • 首先在 consumedEvents 参数 ClickEvent 中创建一个 AbstractCell
  • render() 方法中根据 clicked 状态呈现按钮
  • onBrowserEvent() 方法中更改 clicked 状态并重新呈现行

clicked 状态最好保留在 table 的基础数据类型中,以便每一行都可用。

这是一个完整的工作示例代码:

final CellTable<TableType> table = new CellTable<TableType>();

AbstractCell<TableType> buttonCell = new AbstractCell<ButtonCellTest.TableType>(ClickEvent.getType().getName()) {
    @Override
    public void render(Context context, TableType value, SafeHtmlBuilder sb) {
        Button button = new Button();
        button.setType(ButtonType.SUCCESS);
        button.setSize(ButtonSize.SMALL);
        button.add(new Icon(value.isClicked() ? IconType.CHECK : IconType.TIMES));
        sb.append(SafeHtmlUtils.fromTrustedString(button.toString()));
    }

    @Override
    public void onBrowserEvent(Context context, Element parent, TableType value, NativeEvent event, ValueUpdater<TableType> valueUpdater) {
        value.setClicked(!value.isClicked());
        // ... do stuff...
        table.redrawRow(context.getIndex());
    }
};
table.addColumn(new Column<TableType, TableType>(buttonCell) {
    @Override
    public TableType getValue(TableType object) {
        return object;
    }
});

ArrayList<TableType> rowData = new ArrayList<TableType>();
rowData.add(new TableType("row 1"));
rowData.add(new TableType("row 2"));
...
table.setRowData(rowData);

示例 table 的数据类型保持 clicked 状态:

public class TableType {
    String text;
    boolean clicked = false;

    public TableType(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public boolean isClicked() {
        return clicked;
    }

    public void setClicked(boolean clicked) {
        this.clicked = clicked;
    }
}

至于扩展 IconType 枚举 - 不,您不能扩展 Java 中的枚举。例如,请参阅此问题:Can enums be subclassed to add new elements?.

您可以尝试添加自己的 CSS class 但这应该作为另一个问题提出以获得准确的答案。