How to add button in HeaderSpan of SmartGWT

前端 未结 5 1349
轮回少年
轮回少年 2021-01-28 13:39

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

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-28 14:12

    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.

提交回复
热议问题