How to disable certain rows selection using GWT Datagrid 2.4

749 Views Asked by At

I am using GWT 2.4 version. How to disable row selection in GWT Datagrid.

In my Grid i have mixed records, But based on the condition i need to disable the row selection in DataGrid.

For Example : I have 2 rows with below values

EMP Name| EMP ID | Status

ABC | 1001 | Active

ABC | 1001 | In Active

When user click on 2nd row it should not be selected.

How to achieve this. Please let me know.

1

There are 1 best solutions below

0
Adam On

I was able to achieve this with default checkbox selection event manager.

By default you would have a CheckboxCell that would handle row selection. The trick is that you can override the render() method of the CheckboxCell and do not render the checkbox if corresponding row is not allowed to be selected by simply not calling super.render() method.

Below is a full working code for your example. You can not select rows that are not active. Please, notice the parameters of the constructor new CheckboxCell(true, false) - this is important.

public Widget test() {
    DataGrid<DatagridType> grid = new DataGrid<DatagridType>();

    final MultiSelectionModel<DatagridType> selectionModel = new MultiSelectionModel<DatagridType>(new ProvidesKey<DatagridType>() {
        @Override
        public Integer getKey(DatagridType item) {
            return item.getRowId();
        }
    });
    grid.setSelectionModel(selectionModel, DefaultSelectionEventManager.<DatagridType> createCheckboxManager());

    selectionModel.addSelectionChangeHandler(new Handler() {
        @Override
        public void onSelectionChange(SelectionChangeEvent event) {
            Window.alert(selectionModel.getSelectedSet().toString());
        }
    });

    grid.addColumn(new Column<DatagridType, Boolean>(new CheckboxCell(true, false)) {
        @Override
        public Boolean getValue(DatagridType object) {
            return selectionModel.isSelected(object);
        }

        @Override
        public void render(Context context, DatagridType object, SafeHtmlBuilder sb) {
            if(object.isActive())
                super.render(context, object, sb);
        }
    }, "Select");

    TextColumn<DatagridType> nameColumn = new TextColumn<DatagridType>() {
        @Override
        public String getValue(DatagridType object) {
            return object.getEmpName();
        }
    };

    TextColumn<DatagridType> idColumn = new TextColumn<DatagridType>() {
        @Override
        public String getValue(DatagridType object) {
            return Integer.toString(object.getEmpId());
        }
    };

    TextColumn<DatagridType> statusColumn = new TextColumn<DatagridType>() {
        @Override
        public String getValue(DatagridType object) {
            return object.isActive() ? "Active" : "In Acticve";
        }
    };

    grid.addColumn(nameColumn, "EMP Name");
    grid.addColumn(idColumn, "EMP ID");
    grid.addColumn(statusColumn, "Status");

    List<DatagridType> values = new ArrayList<DatagridType>();
    values.add(new DatagridType(0, "ABC", 1001, true));
    values.add(new DatagridType(1, "ABC", 1001, false));
    values.add(new DatagridType(2, "AAA", 1002, true));
    values.add(new DatagridType(3, "AAA", 1002, false));
    values.add(new DatagridType(4, "BBB", 1003, true));
    values.add(new DatagridType(5, "BBB", 1003, false));

    grid.setRowData(values);

    grid.setHeight("500px");

    Window.alert("Done");

    return grid;
}

private class DatagridType {
    private int rowId;
    private String empName;
    private int empId;
    private boolean active;

    public DatagridType(int rowId, String empName, int empId, boolean active) {
        this.rowId = rowId;
        this.empName = empName;
        this.empId = empId;
        this.active = active;
    }

    public int getRowId() {
        return rowId;
    }

    public String getEmpName() {
        return empName;
    }

    public int getEmpId() {
        return empId;
    }

    public boolean isActive() {
        return active;
    }

    @Override
    public String toString() {
        return Integer.toString(rowId);
    }
}

I just had to add an unique row identifier as a selection id.

After each selection change you will see an alert with a list of selected row ids.

enter image description here