需要在GWT CellList中进行多选

时间:2016-06-30 05:17:50

标签: gwt celllist

我想在CellList组件中选择多个单元格; 我是GWT的新手,请有人帮忙。 为了获得多选,我该如何修改以下代码?

public class HelloWorld implements EntryPoint {
private static final List<String> DAYS = Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday");

public void onModuleLoad() {
    // Create a cell to render each value.
    TextCell textCell = new TextCell();

    // Create a CellList that uses the cell.
    CellList<String> cellList = new CellList<String>(textCell);
    cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

    // Add a selection model to handle user selection.
    final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
    cellList.setSelectionModel(selectionModel);
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
        public void onSelectionChange(SelectionChangeEvent event) {
            String selected = selectionModel.getSelectedObject();
            if (selected != null) {
                // Window.alert("You selected: " + selected);
            }
        }
    });

    cellList.setRowCount(DAYS.size(), true);

    // Push the data into the widget.
    cellList.setRowData(0, DAYS);

    // Add it to the root panel.
    RootPanel.get("gwtCellListBox").add(cellList);
}
}

2 个答案:

答案 0 :(得分:1)

首先,您需要MultiSelectionModel。然后,如果您想要按住Ctrl键,请使用DefaultSelectionEventManager作为CellPreviewEvent.Handler,并使用始终返回EventTranslatorTOGGLE的自定义false。< / p>

答案 1 :(得分:0)

试试这个

public class HelloWorld implements EntryPoint {
private static final List<String> DAYS = Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday");

public void onModuleLoad() {
    // Create a cell to render each value.
    TextCell textCell = new TextCell();

    // Create a CellList that uses the cell.
    CellList<String> cellList = new CellList<String>(textCell);
    cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

    // Add a selection model to handle user selection.
     final MultiSelectionModel<String> selectionModel = new MultiSelectionModel<String>();
     cellList.setSelectionModel(selectionModel);
     selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
     public void onSelectionChange(SelectionChangeEvent event) {

        Set<String> selectedItems = selectionModel.getSelectedSet();

        for (String s : selectedItems) {
            System.out.println(s);
            Window.alert("You selected: " + s);
        }
    }
});

    cellList.setRowCount(DAYS.size(), true);

    // Push the data into the widget.
    cellList.setRowData(0, DAYS);

    // Add it to the root panel.
    RootPanel.get("gwtCellListBox").add(cellList);
}
}