Remove cells from header on Grid

Hi all! Thanks for reading in advance.

I just wanted to know if there’s a way to remove cells from the HeaderRow. I’ve got this Grid where I can add columns dinamically, and I wanted to implement a way to also remove these columns.

I’m merging the cells on the HeaderRow all the time to make a superior header (something like the first row in this example http://demo.vaadin.com/sampler/#ui/grids-and-trees/grid/features)

When I remove the columns it works ok, they are removed from the Grid, but when I try to add new columns afterwards, it breaks when it tries to merge the next cells (HeaderRow.join()), because on the header these removed columns are still present and it’s causing the breakdown of the app because columns and cells on the header don’t match anymore.

Is there a way to remove cells from the HeaderRow?

Greetings!

Hi,

From a quick look there seems to be some code for removing the columns from the merged cells that is executed when the column is removed. If it’s failing in your case, it’s most likely a bug. Can you post a simple UI where you see this happening? It would speed up the bug verification and fixing process. You could also open a bug in our
Trac
.

//Teemu

It’s not a 100% working example, but with this you can see already what I’m doing and understand the issue

@SpringUI
@Theme(Whatever.DEFAULT_THEME)
@Widgetset(Whatever.DEFAULT_WIDGETSET)
public class HomeUI extends UI {

    private static final long serialVersionUID = 6392271856720525876L;
    
    static Navigator navigator;

    
    @Autowired
    SpringViewProvider viewProvider;
    
    @Override
    protected void init(VaadinRequest request) {
        getPage().setTitle("UI Test");
        
        setTheme("test");
        
        // Create a navigator to control the views
        navigator = new Navigator(this, this);
        navigator.addProvider(viewProvider);        
                        
        navigator.navigateTo(GridTestView.VIEW_NAME);
    }

}

@SpringView(name = GridTestView.VIEW_NAME)
public class GridTestView extends VerticalLayout implements View {
    
    public static final String VIEW_NAME = "grid-test";
    
    public static final  String[] MAIN_FIELDS = new String[]
 { "materialGroup", "materialName", "solidity", "type"};
    public static final  String[] SECONDARY_FIELDS = new String[]
 { "secondary1", "secondary2"};

    Grid grid;
    HeaderRow filteringHeader;
    Button addColumns;
    Button deleteColumns;
    
    @Override
    public void enter(ViewChangeEvent event) {
        
        grid = new Grid();
                
        grid.setCaption("Double click to edit");
        grid.setSizeFull();
        grid.setEditorEnabled(true);
        grid.setSelectionMode(SelectionMode.NONE);
        grid.setResponsive(true);

        for(String field : MAIN_FIELDS){
            grid.addColumn(field);
        }
        for (int i = 0; i < 100; ++i) {
            String material_group = "Material Group " + i;
            String material_name = "Material Name " + i;
            String solidity = "Solidity " + i;
            String type = "Type " + i;
            grid.addRow(material_group, material_name, solidity, type);
        }
        
        addColumns = new Button("Add more columns");
        
        addComponent(addColumns);
        addComponent(grid);
        
        HeaderRow header = grid.prependHeaderRow();
        header.join((Object[]) MAIN_FIELDS);
        header.getCell(MAIN_FIELDS[0]
).setText("Whatever");

        
        addColumns.addClickListener(new ClickListener() {

            List<String> group_fields = new ArrayList<String>();
            @Override
            public void buttonClick(ClickEvent event) {
                Random ran = new Random();
                int x = ran.nextInt(6) + 5;
                for(String field : SECONDARY_FIELDS){
                    
                    grid.addColumn(field + "_" + x);
                    group_fields.add(field + "_" + x);                    
                    
                }
                String[] groupFieldsArray = group_fields.toArray(new String[0]
);
                HeaderRow header = grid.getHeaderRow(0);
                
                deleteColumns = new Button("Remove Column");
//the header keeps containing the deleted columns so this fails when some columns have been removed
                header.join((Object[]) groupFieldsArray); 
                header.getCell(groupFieldsArray[0]
).setComponent(deleteColumns);                

                deleteColumns.addClickListener(new ClickListener() {
                    @Override
                    public void buttonClick(ClickEvent event) {
                        
                        for(String field : groupFieldsArray){
                            grid.removeColumn(field); //this is working OK, it deletes the columns

                        }               
                    }
                    
                });
            }
            
        });

    }
}

I opened also a bug ticket on the vaadin trac, but I didn’t get an answer. Not here and no there :frowning:


https://dev.vaadin.com/ticket/19810

Sorry,

I haven’t had the time to look at this. Thanks for pinging. I’ll check this later today, and hopefully I can provide some more information to this.

//Teemu

After taking a quick look it seems that joining a column that has been removed seems to cause some issues in there. There’s a missing check in the join method that should verify that the columns exist in Grid and skip them when needed.

//Teemu

Ok then I guess I would need need to keep track of the issue in the trac site to know when the bug is solved right?

Thank you!