简体   繁体   English

选中/取消选中 CheckboxCell,单击一行 - GWT

[英]Check/Uncheck CheckboxCell, onclick of a row - GWT

I have a cell table with the first column as checkboxes.我有一个单元格表,第一列是复选框。 My checkboxes have to be checked or unchecked when there is any single click event on the entire row.当整行上有任何单击事件时,必须选中或取消选中我的复选框。 This is the following code for creating a MultiSelectionModel , creating CheckboxCell and creating a column for cell table.这是用于创建MultiSelectionModel 、创建CheckboxCell和为单元格表创建列的以下代码。

MultiSelectionModel<Object> selectionModel = new MultiSelectionModel<>();
    table.setSelectionModel(selectionModel);

    CheckboxCell selectedCell = new CheckboxCell();
    Column<Object,Boolean> selectedCol = new Column<Object, Boolean>(selectedCell){
        @Override
        public Boolean getValue(Object object) {
            return object.isSelected();
        }
    };
    table.addColumn(selectedCol);


    //Single Click Event to Enable/Disable checkbox.
    table.addDomHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            Set<Object> selectedItems = selectionModel.getSelectedSet();

            for (Object s : selectedItems) {
                Window.alert(String.valueOf(s));
                selectionModel.setSelected(s, true);
            }
        }
    }, ClickEvent.getType());

I tried to mark a row as checked using "selectionModel.setSelected(s, true)".我尝试使用“selectionModel.setSelected(s, true)”将一行标记为已检查。 But it isn't working, when I clicked on row, the corresponding checkbox is not being checked.但它不起作用,当我单击行时,未选中相应的复选框。

My question is how do I enable/disable checkboxes onclick of an entire row.我的问题是如何启用/禁用单击整行的复选框。 Is my approach correct.我的方法是否正确。 Or Is there any other way to perform this action in GWT.或者有没有其他方法可以在 GWT 中执行此操作。

You are very close to the working solution.您非常接近工作解决方案。

In the selectedCell you should return the value depending on selectionModel :selectedCell您应该根据selectionModel返回值:

return selectionModel.isSelected(object);

This way you are using default multi selection model that selects rows by clicking on them.这样您就可以使用默认的多选模型,通过单击它们来选择行。 And the checkbox value comes from the selection model.复选框值来自选择模型。 That's it.就是这样。

See the working example below:请参阅下面的工作示例:

CellTable<String> table = new CellTable<String>();

final MultiSelectionModel<String> selectionModel = new MultiSelectionModel<>();
table.setSelectionModel(selectionModel);

CheckboxCell selectedCell = new CheckboxCell();
Column<String, Boolean> selectedCol = new Column<String, Boolean>(selectedCell) {
    @Override
    public Boolean getValue(String object) {
        // return object.isSelected();
        return selectionModel.isSelected(object);
    }
};
table.addColumn(selectedCol);

table.addColumn(new TextColumn<String>() {
    @Override
    public String getValue(String object) {
        return object;
    }
});

List<String> values = new ArrayList<>();
for(int i = 0; i < 10; i++)
    values.add("Line " + (i + 1));
table.setRowData(values);

You can use standard Ctrl and Shift keys to control selection.您可以使用标准的CtrlShift键来控制选择。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM