Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    CellSelectionChangedEvent.cancel does change the cell selected style

    Hi,

    Javadocs for com.smartgwt.client.widgets.grid.events.CellSelectionChangedEvent.cancel() say
    Calling this method will prevent the GridRenderer styling from being updated to reflect the selection change.
    but the selected style *is* applied on the cell after cancel.


    repro on SNAPSHOT_v8.3d_2012-09-17/Pro and SNAPSHOT_v8.3d_2012-10-11/Pro.
    This code will enable a button when cells in row 1 are selected, not when cells in row 0 are selected. When you click a cell in row 0, the button stays disabled (so the cancel() succeeded), but the cell's selected style itself is applied (NOK).

    Code:
    final ListGrid grid = new ListGrid();
    grid.setWidth(200);
    grid.setHeight(100);
    String fieldName1 = "a";
    String fieldName2 = "b";
    ListGridField field1 = new ListGridField(fieldName1);
    ListGridField field2 = new ListGridField(fieldName2);
    grid.setFields(field1, field2);
    
    ListGridRecord [] records = new ListGridRecord[100];
    for (int i=0; i<records.length; i++) {
    	records[i] = new ListGridRecord();
    	records[i].setAttribute(fieldName1, "aha" + i);
    	records[i].setAttribute(fieldName2, "oho" + i);
    }
    
    grid.setRecords(records);
    
    
    HLayout buttons = new HLayout();
    IButton select = new IButton("select row 40");
    select.addClickHandler(new ClickHandler() {
    
    	public void onClick(ClickEvent event) {
    		grid.selectSingleRecord(40);
    		grid.scrollToRow(40);
    	}
    });
    final IButton doSomething = new IButton("do something");
    doSomething.setDisabled(true);
    buttons.setMembers(select, doSomething);
    
    
    grid.setCanEdit(false);
    grid.setShowRollOver(false);
    grid.setShowSelectedStyle(true);
    grid.setCanSelectCells(true);
    grid.setCanSelectAll(true);
    grid.addCellSelectionChangedHandler(new CellSelectionChangedHandler() {
    	
    	public void onCellSelectionChanged(CellSelectionChangedEvent event) {
    		SC.logInfo("onCellSelectionChanged started");
    		
    		int[][] ee = event.getCellList();
    		for (int i=0; i<ee.length; i++) {
    			SC.logInfo("event for cell (" + ee[i][0]+","+ee[i][1]+")");
    		}
    		
    		
    		for (int i=0; i<ee.length; i++) {
    			if (ee[i][1] == 0) {
    				// don't allow selections in the first column
    				// CTRL+A sends 1 event
    				event.cancel();
    				return;
    			}
    		}
    		
    		// enable/disable action button when there are valid cells selected
    		CellSelection cellSelection = grid.getCellSelection();
    		doSomething.setDisabled(!cellSelection.anySelected());
    	}
    });
    
    
    
    VLayout layout = new VLayout();
    layout.setWidth100();
    layout.setHeight(600);
    layout.setMembers(grid, buttons);
    * I also notice some redraw problems when I click the "select row 40 button". When I hover over the row with the mouse, the selected style is applied.
    * What I wanted to investigate is what happens when I hit CTRL+A. Then I guess only 1 event is thrown instead of one event for each cell. So I guess I can't cancel the selections for row 0 that way and must manually deselect them. Or is there a better way?


    thanks,

    #2
    It looks like there are some problems with the "cancel()" method on this event. We'll look into it. If you get rid of this, your problem with the "Select Row 40" button goes away, though of course the first column is selectable.

    It's probably easiest to address this by intercepting the actual events that cause selection.

    You can add a cell mouseDown handler to intercept selection via mouse clicks and cancel that event if the event occurred over the first column.

    You can also disable the automated behavior of selecting all on Ctrl+A via the canSelectAll attribute, and then implement your own keyPress handler on the grid which reacts to ctrl+a and selects the cells you're interested in.

    Comment


      #3
      I actually could solve the case by not calling cancel() at all, but within the onCellSelectionChanged do
      Code:
      int[][] e = event.getCellList();
      CellSelection cellSelection = grid.getCellSelection();
      if (e[i][1] == 0) cellSelection.deselectCell(e[i][0], e[i][1])
      This never shows the first row with selected style either, so it's a good workaround, and even works for the built in CTRL+A scenario.

      Comment

      Working...
      X