选择并取消选中listgrid上的检查

时间:2013-09-20 10:47:16

标签: smartgwt

我得到下一个代码,带有第一列的listgrid是一个检查:

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

我希望在点击一行时动态选择或取消选中listgrid上的check组件。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

我试图搜索您的要求是否可以通过某些API功能来实现,但却找不到。

所以我以编程方式实现了这种行为。请尝试以下代码:

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);
            }
        }
});

此处网格ListGrid的实例。