Search code examples
smartgwt

select and deselect check on listgrid


I get the next code a listgrid with the firs column is a check:

private ListGrid listGrid = new ListGrid();
....
....
listGrid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
    listGrid .addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            //TODO
        }
    });

I would like select or deselect the check component on my listgrid on the fly when a row is clicked.

How can I do it?


Solution

  • I've tried to search if your requirement can be fulfilled by some API functionality but couldn't find so.

    So I've implemented this behavior programmatically. Please try the following code:

    grid.addRecordClickHandler(new RecordClickHandler() {
    
            @Override
            public void onRecordClick(RecordClickEvent event) {
                ListGridRecord record = (ListGridRecord) event.getRecord();
                ListGridRecord[] selectedRecords = grid.getSelectedRecords();
                if (selectedRecords != null) {
                    List<ListGridRecord> selectedRecordsList = Arrays.asList(selectedRecords);
                    if (selectedRecordsList.contains(record)) {
                        // the record is already selected, so deselect it.
                        grid.deselectRecord(record);
                    } else {
                        // the record is already deselected, so select it.
                        grid.selectRecord(record);
                    }
                } else {
                    // the record is already deselected, so select it.
                    grid.selectRecord(record);
                }
            }
    });
    

    Here grid is an instance of ListGrid.