I want to add a button to the HeaderSpan of ListGrid in SmartGWT. I tried using the \'HeaderSpan.setAttribute((String property, Object value)) method but it did not work. Below
Finally I found the way to add button to a HeaderSpan. Below is the code (Have omitted import statements for brevity):-
public class AdvancedListGrid extends ListGrid {
public void setHeaderSpanButtons() {
this.getAttribute("headerSpans");
NodeList list = this.getElement().getElementsByTagName("td");
int length = list.getLength();
for(int index = 0; index < length; index++) {
Element element = list.getItem(index);
if(element.getInnerHTML().toString().equals("Identification")) {
element.setInnerHTML(null);
element.insertFirst(createHeaderSpanContents());
}
}
}
private Element createHeaderSpanContents() {
HLayout hLayout = new HLayout();
Button button = new Button("+");
button.setHeight(20);
button.setWidth(20);
button.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
SC.say("Why did you click me?");
}});
Label title = new Label("Test Title");
title.setWidth(70);
title.setHeight(20);
hLayout.addMember(button);
hLayout.addMember(new LayoutSpacer());
hLayout.addMember(title);
return hLayout.getElement();
}
}
The client class will call the AdvancedListGrid.setHeaderSpanButtons() method to add buttons to the header span. Of course you can customize the method to meet your needs.