Search code examples
gwtgridviewcheckboxgxtsetvalue

How to automatically check the checkboxes in the gridview when displaying?


I'm using GXT 3 to build a GridView that will display "incidents".

What I want to do is that when it renders it, I want some checkboxes to be checked, others to empty, according to the boolean in the database.

Below you have my code:

        CheckBoxSelectionModel<IncidentDto> isIncidentCM = new CheckBoxSelectionModel<IncidentDto>(incidentProperties.incident());
        allColumns.add(isIncidentCM.getColumn());
        ColumnModel<IncidentDto> columnModel = new ColumnModel<IncidentDto>(allColumns);

        final Grid<IncidentDto> grid = new Grid<IncidentDto>(store, columnModel);
        grid.setSelectionModel(isIncidentCM);
        add(grid);

And the IncidentProperties value provider:

IdentityValueProvider<IncidentDto> incident();

Solution

  • I'm not sure if you can bind the selection value to a boolean property, but you could add a listener to the Grid to update the checkboxes based on the boolean condition.

    grid.addBeforeShowHandler(BeforeShowEvent event) {
      @Override
      public void onBeforeShow(BeforeShowEvent event) {
        List<IncidentDto> itemsToSelect = new ArrayList<IncidentDto>();
        for (IncidentDto incident : store.getAll()) {
          if (incident.getBooleanProperty()) { //whatever your property is called
            itemsToSelect.add(incident);
          }
        }
        isIncidentCM.setSelection(itemsToSelect);
      }
    }
    

    There may be other implications in using a BeforeShowEvent depending on how/when you populate your store, render the grid, etc. but assuming your store is fully loaded and the property available from your store objects I believe this should accomplish your goal.