在我的CellTable中,一列有ButtonCells。所以我需要通過單擊位於CellTable外部的按鈕來禁用這些ButtonCells。在celltable中禁用ButtonCell
回答
早上好,
這是一個不錯的小例子,在清晨做。在這裏,你有我的解決方案,我不是說,它是最好的,但它的工作!
public class _57_DisableAllButtons implements EntryPoint {
boolean enable = false;
@Override
public void onModuleLoad() {
Button b1 = new Button("Button1");
Button b2 = new Button("Button2");
Button b3 = new Button("Button3");
RootPanel.get("nameFieldContainer").add(b1);
RootPanel.get("nameFieldContainer").add(b2);
RootPanel.get("nameFieldContainer").add(b3);
Button disableAll = new Button("Disabale all");
RootPanel.get("sendButtonContainer").add(disableAll);
disableAll.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
final RootPanel rootpannel = RootPanel.get("nameFieldContainer");
int widgetcount = rootpannel.getWidgetCount();
for (int i = 0; i < widgetcount; i++) {
Widget w = rootpannel.getWidget(i);
if(w instanceof Button){
((Button) w).setEnabled(enable);
}
}
enable = !enable;
}
});
}
}
你可以創建自己的鈕釦電池類。例如:
import com.google.gwt.cell.client.AbstractSafeHtmlCell;
import com.google.gwt.cell.client.Cell;
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.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.text.shared.SafeHtmlRenderer;
import com.google.gwt.text.shared.SimpleSafeHtmlRenderer;
/**
* A {@link Cell} used to render a button.
*/
public class StyledButtonCell extends AbstractSafeHtmlCell<String> {
private String disabledString = "";
private boolean disabled = false;
/**
* Construct a new ButtonCell that will use a {@link SimpleSafeHtmlRenderer}.
*/
public StyledButtonCell() {
this(SimpleSafeHtmlRenderer.getInstance());
}
/**
* Construct a new ButtonCell that will use a given {@link SafeHtmlRenderer}.
*
* @param renderer a {@link SafeHtmlRenderer SafeHtmlRenderer<String>} instance
*/
public StyledButtonCell(SafeHtmlRenderer<String> renderer) {
super(renderer, "click", "keydown");
}
@Override
public void onBrowserEvent(Context context, Element parent, String value,
NativeEvent event, ValueUpdater<String> valueUpdater) {
super.onBrowserEvent(context, parent, value, event, valueUpdater);
if ("click".equals(event.getType())) {
EventTarget eventTarget = event.getEventTarget();
if (!Element.is(eventTarget)) {
return;
}
if (parent.getFirstChildElement().isOrHasChild(Element.as(eventTarget))) {
// Ignore clicks that occur outside of the main element.
onEnterKeyDown(context, parent, value, event, valueUpdater);
}
}
}
@Override
public void render(Context context, SafeHtml data, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<button type=\"button\" tabindex=\"-1\"" + disabledString + ">");
if (data != null) {
sb.append(data);
}
sb.appendHtmlConstant("</button>");
}
@Override
protected void onEnterKeyDown(Context context, Element parent, String value,
NativeEvent event, ValueUpdater<String> valueUpdater) {
if (valueUpdater != null) {
valueUpdater.update(value);
}
}
public boolean isDisabled() {
return disabled;
}
public void setDisabled(boolean disabled) {
this.disabled = disabled;
if (disabled) {
disabledString = "disabled=\"disabled\"";
} else {
disabledString = "";
}
}
}
然後用你的表使用:
final StyledButtonCell buttonCell = new StyledButtonCell();
buttonColumn = new Column<SomeItem, String>(buttonCell) {
public String getValue(SomeItem object) {
// The value to display in the button.
return "Go";
}
};
要禁用按鈕,只需撥打:
buttonCell.setDisabled(true);
table.redraw();
謝謝,很棒的工作。很高興在一年後找到你回答的問題,看到答案不好,並採取更好的答案。 只做了一個小小的改變:juse「HasEnabled」接口並將您的方法命名爲「isEnabled」和「setEnabled」,而不是「isDisabled」和「setDisabled」。 – Stefan
https://github.com/pressgang-ccms/PressGangCCMSUI/blob/master/src/main/java/com/google/gwt/user/client/ui/DisableableButtonCell.java對我來說是一個小小的變化在我自己的項目中。 – Phyxx
如果您不需要 「重命名」您的按鈕,您還可以基於布爾抽象單元創建自己的:
public class CustomButtonCell extends AbstractEditableCell<Boolean, Boolean> {
private static final SafeHtml ENABLED = SafeHtmlUtils.fromSafeConstant("<button type=\"button\" tabindex=\"-1\">");
private static final SafeHtml DISABLED = SafeHtmlUtils.fromSafeConstant("<button type=\"button\" tabindex=\"-1\" disabled=\"disabled\">");
private static final SafeHtml CLOSE_BRACKET = SafeHtmlUtils.fromSafeConstant("</button>");
private final boolean dependsOnSelection;
private final boolean handlesSelection;
private final String text;
public CustomButtonCell(final String text) {
this(false, false, text);
}
public CustomButtonCell(boolean dependsOnSelection, boolean handlesSelection, String text) {
super(BrowserEvents.CHANGE, BrowserEvents.CLICK, BrowserEvents.KEYDOWN);
this.dependsOnSelection = dependsOnSelection;
this.handlesSelection = handlesSelection;
this.text = text;
}
@Override
public boolean dependsOnSelection() {
return dependsOnSelection;
}
@Override
public boolean handlesSelection() {
return handlesSelection;
}
@Override
public boolean isEditing(Context context, Element parent, Boolean value) {
return false;
}
/**
* Based on CheckboxCell
*/
@Override
public void onBrowserEvent(Context context, Element parent, Boolean value,
NativeEvent event, ValueUpdater<Boolean> valueUpdater) {
String type = event.getType();
boolean enterPressed = BrowserEvents.KEYDOWN.equals(type)
&& event.getKeyCode() == KeyCodes.KEY_ENTER;
if (BrowserEvents.CHANGE.equals(type) || BrowserEvents.CLICK.equals(type) || enterPressed) {
InputElement input = parent.getFirstChild().cast();
Boolean isChecked = input.isChecked();
if (enterPressed && (handlesSelection() || !dependsOnSelection())) {
isChecked = !isChecked;
input.setChecked(isChecked);
}
if (value != isChecked && !dependsOnSelection()) {
setViewData(context.getKey(), isChecked);
} else {
clearViewData(context.getKey());
}
if (valueUpdater != null) {
valueUpdater.update(isChecked);
}
}
}
@Override
public void render(Context context, Boolean value, SafeHtmlBuilder sb) {
// Get the view data.
Object key = context.getKey();
Boolean viewData = getViewData(key);
if (viewData != null && viewData.equals(value)) {
clearViewData(key);
viewData = null;
}
if (value != null && ((viewData != null) ? viewData : value)) {
sb.append(ENABLED);
} else {
sb.append(DISABLED);
}
sb.append(SafeHtmlUtils.fromString(text));
sb.append(CLOSE_BRACKET);
}
}
,並通過其「獲取價值」 mechanisme編輯
Column<TestJSO, Boolean> revokeColumn = CellTableUtils.createColumn(new CustomButtonCell("Test"),
new GetValue<TestJSO, Boolean>() {
@Override
public Boolean getValue(TestJSO value) {
return value.isEnabled();
}
});
- 1. GWT CellTable,點擊時禁用ButtonCell
- 2. 如何在CellTable Gwt中設置ButtonCell樣式?
- 3. 禁用CellTable中的CheckboxCell
- 4. GWT Java - CellTable - ButtonCell - 如何讓它響應點擊
- 5. 如何在ComboBox javafx中設置ButtonCell?
- 6. 如何完全禁用CellTable中的行選擇?
- 7. GWT ButtonCell更改文本
- 8. 將樣式添加到ButtonCell中
- 9. GWT 2 ButtonCell在一行添加事件
- 10. GWT:GWT中CellTable
- 11. CELLTABLE在GWT
- 12. CellTable在TabLayoutPanel中不可見
- 13. 在Celltable上過濾
- 14. GWT - celltable
- 15. GWT - celltable
- 16. celltable - 如何使用圖像作爲celltable背景(或行背景)
- 17. GWT CellTable:添加/刪除CellTable中的一行
- 18. Gwt celltable在列上調用排序
- 19. GWT:在TabPanel裏面的FlowPanel中的CellTable
- 20. GWT CellTable CheckboxCell在IE8中不工作
- 21. GWT celltable如何在edittextcell中創建selectcell?
- 22. DataProvider在GWT中CellTable究竟做什麼
- 23. 如何在cellTable中每行有一行?
- 24. 在CellTable列中創建自定義ActionCell
- 25. GWT - 使CellTable Cell使用HTML?
- 26. GWT - CellTable/DataGrid使用ImageCell
- 27. GWT Celltable:如何避免在Celltable的特定單元格中輸入特定值?
- 28. CellTable中的GWT填充行
- 29. CellTable中的Colspan和Rowspan
- 30. 將ScrollPanel添加到CellTable中
我不知道這是去工作,因爲CellWidgets通常不會呈現經典的GWT部件(即TextField或Button) –