Search code examples
checkboxgridonchangegxt

How to get the row index of selected checkbox on grid GXT


I am working with GXT (2.2.5) and need to get the row index of chenged checkbox on the grid. What i am doing is to create a grid and apply a GridCellRender to show a checkBox in first column, then when any checkBox change their value (listener at the OnChange event), the row index of changed checkbox must be taken. This is a part of my code by the moment:

    checkColumn.setRenderer(new GridCellRenderer() {
        @Override
        public Object render(ModelData model, String property, ColumnData config, int rowIndex, int colIndex, ListStore store, Grid gri) {
            final CheckBox check = new CheckBox();
            check.addListener(Events.OnChange, new Listener<BaseEvent>() {
                @Override
                public void handleEvent(BaseEvent be) {
                    //Here we get the row index
                }
            });
            return check;
        }
    });

Thanks.


Solution

  • You can get it like this:

     checkColumn.setRenderer(new GridCellRenderer() {
            @Override
            public Object render(final ModelData model, String property, ColumnData config, int rowIndex, int colIndex, final ListStore store, Grid gri) {
                final CheckBox check = new CheckBox();
                check.addListener(Events.OnChange, new Listener<BaseEvent>() {
                    @Override
                    public void handleEvent(BaseEvent be) {
                        //////////
                        int indx = store.indexOf(model);
                        //////////
    
                    }
                });
                return check;
            }
        });
    

    (note that you must convert to final your store and model variables)

    Or maybe this:

    checkColumn.setRenderer(new GridCellRenderer() {
            @Override
            public Object render(final ModelData model, String property, ColumnData config, int rowIndex, int colIndex, final ListStore store, Grid gri) {
    
                final CheckBox check = new CheckBox();
                check.setData("indx", store.indexOf(model));
    
                check.addListener(Events.OnChange, new Listener<BaseEvent>() {
                    @Override
                    public void handleEvent(BaseEvent be) {
                        //////////
                        int indx = ((CheckBox) be.getSource()).getData("indx");
                        //////////
    
                    }
                });
                return check;
            }
        });
    

    I hope this helps