GWT CellTable-Need to have two buttons in last single cell of each row

前端 未结 2 1654
面向向阳花
面向向阳花 2021-01-12 10:40

I am using CellTable to add columns to it. It works fine when I add rows and single data on each cell.

It has header like Name ,Age, Address with rows below it which

2条回答
  •  醉梦人生
    2021-01-12 11:32

    There are two ways to achieve that:

    1. Subclass AbstractCell and implement the render method to create two buttons and handle its events (see here for more details).
    2. Use a CompositeCell to add two ActionCells

    Second approach is easier and cleaner. Here is the code for that:

    public void onModuleLoad() {
        CellTable table = new CellTable();
    
        List> cells = new LinkedList>();
        cells.add(new ActionHasCell("Edit", new Delegate() {
    
            @Override
            public void execute(Person object) {
               // EDIT CODE
            }
        }));
        cells.add(new ActionHasCell("Delete", new Delegate() {
    
            @Override
            public void execute(Person object) {
                // DELETE CODE
            }
        }));
    
        CompositeCell cell = new CompositeCell(cells);
        table.addColumn(new TextColumn() {
    
            @Override
            public String getValue(Person object) {
                return object.getName()
            }
        }, "Name");
    
        // ADD Cells for Age and Address
    
        table.addColumn(new Column(cell) {
    
            @Override
            public Person getValue(Person object) {
                return object;
            }
        }, "Actions");
    
    
    }
    
    private class ActionHasCell implements HasCell {
        private ActionCell cell;
    
        public ActionHasCell(String text, Delegate delegate) {
            cell = new ActionCell(text, delegate);
        }
    
        @Override
        public Cell getCell() {
            return cell;
        }
    
        @Override
        public FieldUpdater getFieldUpdater() {
            return null;
        }
    
        @Override
        public Person getValue(Person object) {
            return object;
        }
    }
    

提交回复
热议问题