2016-06-30 52 views
0

我想在CellList組件中選擇多個單元; 我是GWT新手,有人請幫忙。 爲了獲得多選,我如何修改下面的代碼?需要GWT中的多選CellList

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

嗨,我試着用下面的代碼進行多重選擇,它可以很好地用_ ** Ctrl ** _鍵按住。但是我想使用_ **進行這種多重選擇,而不需要按住_ ** Ctrl ** _ –

+0

final MultiSelectionModel selectModel = new MultiSelectionModel (); \t \t cellList.setSelectionModel(selectModel); \t \t selectModel.addSelectionChangeHandler(新SelectionChangeEvent.Handler(){ \t \t \t公共無效onSelectionChange(SelectionChangeEvent事件){ \t \t \t \t設置選擇= selectModel.getSelectedSet(); \t \t \t \t如果(選擇! = NULL){ \t \t \t \t \t //Window.alert("You選擇:「+選擇); \t \t \t \t} \t \t \t} \t \t}); –

+0

我建議你爲多個選擇添加一個複選框列,如果你需要幫助,請告訴我。 –

回答

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

首先,你需要一個MultiSelectionModel。然後,如果您想要按住Ctrl鍵,請使用DefaultSelectionEventManager作爲CellPreviewEvent.Handler,並使用自定義EventTranslator,並始終返回TOGGLEfalse