2014-02-16 44 views
0

我想要一列完整的按鈕[我自己定製的按鈕],並希望在我的應用程序中有按鈕的按鈕?如何創建代碼?如何在GWT dataGrid中添加自定義按鈕?

感謝


細胞細胞=新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()是真實的,只有我要創建的按鈕列,但現在它爲所有列創建?請提出一些建議?,

+0

這些按鈕怎麼樣。 http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellSampler –

+0

我想添加自定義按鈕? – user3192996

回答

0

您可以使用ActionCell,然後將CSS應用到它以使其看起來像你喜歡的任何方式。

看看GWT展示瞭如何做到這一點的例子:http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellSampler

+0

我想添加到我的專欄只爲某些條件。所以如何在添加單元格到列之前檢查條件。更多我想使用圓形按鈕,所以我有一個定製的按鈕,我如何使用我的定製按鈕,併爲它寫點擊處理程序? – user3192996

+0

1.在GWT中,你永遠不會把處理程序添加到列中的按鈕 - 你只需要在ActionCell中覆蓋execute()。看看我提供的Showcase示例中的代碼。 2.您可以添加或隱藏列,並且可以根據您喜歡的任何條件顯示或隱藏每個單元格內的按鈕。 3.正如我所說的,使用CSS讓你的按鈕看起來無論如何都是你喜歡的 - 無論是漸變還是圓角。 –

+0

請提供一個詳細的one.especially如何獲取對象和檢查條件,然後再添加到列 – user3192996

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元素髮生,我們可以得到跨度元素的ID,並設置相應的操作對象按狀態。

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

在值更新器內部,基於點擊狀態,調用動作對象的點擊處理程序方法。

+0

我創建了actioncell,它工作正常。但是,如果我應用CSS它應用到整個單元格,而不是button.pls幫助 – user3192996

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; 

} 
+0

CSS是應用於整個單元格而不是按鈕? – user3192996

+0

.myButtonStyle; .myButtonStyle background-color:red; } .myButtonStyle TD button { background:#002A50; 顏色:白色; font-size:5pt; !重要; text-align:center; 光標:指針; font-weight:normal; width:15px; !重要; height:15px; !重要; } @external .btn_disable; .btn_disable { background:#002A50; 顏色:灰色; font-size:5pt;!important; text-align:center;光標:默認; 光標:默認; font-weight:normal; width:15px;!important; height:15px;!important; } – user3192996

相關問題