Announcement

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

    SelectItem.setValueFormatter not called in ListGrid?

    Hello, I'm using SmartClient Version: SC_SNAPSHOT-2011-11-08/PowerEdition Deployment (built 2011-11-08)


    In the following code, I have a Listgrid containing Lectures, with a foreign field to a Teacher table (who's teaching the lecture). This dropdown menu is written as follows:
    Code:
    final SelectItem TeacherSelectItem = new SelectItem("Teacher_id",
    				teacherMessages.name_single());
    		TeacherSelectItem.setOptionDataSource(DataSource.get("Teacher"));
    TeacherSelectItem.setValueField("Teacher_id");
    TeacherSelectItem.setAlign(Alignment.LEFT);
    TeacherSelectItem.setTextAlign(Alignment.LEFT);
    TeacherSelectItem.setTitleAlign(Alignment.LEFT);
    ListGrid TeacherListGrid = new ListGrid();
    TeacherListGrid.setShowFilterEditor(true);
    TeacherListGrid.setFilterOnKeypress(true);
    
    final SortSpecifier sort = new SortSpecifier("surname",SortDirection.ASCENDING);
    final		SortSpecifier[] s = new SortSpecifier[] {sort};
    
    TeacherListGrid.setInitialSort(s);
    
    TeacherSelectItem.setValueFormatter(new FormItemValueFormatter() {
    	public String formatValue(Object value, Record selectedRecord,
    			DynamicForm form, FormItem item) {
    		ListGridRecord selectedRecord = TeacherSelectItem.getSelectedRecord();
    
                    if(selectedRecord == null)
    			return "";
    
    		String t = selectedRecord.getAttribute("tussenvoegsel");
    		String tus = t == null || "".equals(t) ? "" : " " + t;
    		
    		if(selectedRecord.getAttribute("firstname") == null && selectedRecord.getAttribute("surname")==null)
    			return "";
    		
    		return selectedRecord.getAttribute("firstname")+tus+" "+selectedRecord.getAttribute("surname");		
    			}
    		});
    
    TeacherSelectItem.setAllowEmptyValue(true);
    
    TeacherSelectItem.setPickListWidth(500);
    TeacherSelectItem.setPickListFields(
                    TeacherModelMessages m = (TeacherModelMessages) GWT
    				.create(TeacherModelMessages.class);
    		
    		ListGridField[] ret = new ListGridField[3];
    		ret[0] = new ListGridField("firstname",m.firstname());
    		ret[1] = new ListGridField("surname",m.surname());
    		ret[2] = new ListGridField("companyName",m.companyName());
    		return ret;
    				);
    
    		TeacherSelectItem.setPickListProperties(TeacherListGrid);
    
    		TeacherField.setAlign(Alignment.LEFT);
    
    		TeacherField.setEditorType(TeacherSelectItem);
    		TeacherField.setOptionDataSource(DataSource.get("Teacher"));
    		TeacherField.setDisplayField("firstname");
    
    		TeacherField.setFilterEditorType(TeacherSelectItem); //reusing this is okay appearantly
    
    TeacherField.setCellFormatter(new CellFormatter(){
    			public String format(Object value, ListGridRecord record,
    					int rowNum, int colNum) {
    				String f = record.getAttribute("firstname");
    				String t = record.getAttribute("tussenvoegsel");
    				String s = record.getAttribute("surname");
    				String c = record.getAttribute("companyName");
    				
    				String ret  = ((f == null || "".equals(f)) ? "" : f) +
    							  ((t == null || "".equals(t)) ? "" : " " + t) +
    							  ((s == null || "".equals(s)) ? "" : " " + s) +
    							  ((c == null || "".equals(c)) ? "" : " (" + c + ")");
    				
    				return ret;
    			}
    		});
    		
    TeacherField.addCellSavedHandler(new CellSavedHandler(){
    	public void onCellSaved(CellSavedEvent event) {
    		final int rowNum = event.getRowNum();			    
    
                    DataSource ds= DataSource.get("Lecture");
    		DSRequest req= new DSRequest();
    			req.setOperationId("presenceList");
    			final Record r = event.getRecord();
    			Criteria c = new Criteria();
    			c.setAttribute("Lecture_id", r.getAttribute("Lecture_id"));
    	                ds.fetchData(c, new DSCallback(){
    			     public void execute(DSResponse response, Object rawData,DSRequest request) {
    						RecordList rl = response.getDataAsRecordList();
    						if(rl!=null){
    							Log.debug("fetched name " +  rl.get(0).getAttribute("surname") + ", " +rl.get(0).getAttribute("tussenvoegsel")
    									+ ", "+rl.get(0).getAttribute("firstname"));
    							r.setAttribute("surname", rl.get(0).getAttribute("surname"));
    							r.setAttribute("firstname", rl.get(0).getAttribute("firstname"));
    							
    							grid.refreshRow(rowNum);
    						}
    					}
    				},req);
    			}
    			
    		});
    When editing this listgrid field in-line (ie. by doubleclicking on a record in the grid), the selectitem-editor shows correctly, but upon changing a selection, it doesn't show the value being picked (while still in edit mode, that is). This is because setValueFormatter does not seem to be called (I put a breakpoint there to check).

    What am I missing, because if i remember correctly this worked a few nightlies ago. Is my code wrong, or is the setValueFormatter broken?
    Last edited by Sytematic; 21 Nov 2011, 01:33.

    #2
    Update, the setValueFormatter IS called, but it crashes on the following line,

    Code:
    TeacherSelectItem.setValueFormatter(new FormItemValueFormatter() {
    	public String formatValue(Object value, Record record,
    					DynamicForm form, FormItem item) {
    	Log.debug("setValueFormatter called"); //this is called, according to my logs.
    
            // this crashes, but the stacktrace below doesn't point to this line, somehow.
    	ListGridRecord selectedRecord = TeacherSelectItem.getSelectedRecord(); 
    
    		if(selectedRecord == null)
    			return "";
    
    		String t = selectedRecord.getAttribute("tussenvoegsel");
    		String tus = t == null || "".equals(t) ? "" : " " + t;
    		
    		if(selectedRecord.getAttribute("firstname") == null && selectedRecord.getAttribute("surname")==null)
    			return "";
    		
    		return selectedRecord.getAttribute("firstname")+tus+" "+selectedRecord.getAttribute("surname");
    	
    			}
    		});
    Code:
    11:18:01.479 [ERROR] [generatedcode] Uncaught exception escaped
    
    com.google.gwt.core.client.JavaScriptException: (TypeError): self.getSelectedRecord is not a function
        at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:248)
        at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136)
        at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
        at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
        at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
        at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
        at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:213)
        at sun.reflect.GeneratedMethodAccessor76.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
        at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
        at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:172)
        at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:292)
        at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:546)
        at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)
        at java.lang.Thread.run(Thread.java:680)
    I think this is related to a former thread of me, to which I have no solution as of yet, but hopefully this provides some insight to that as well, this other thread is located here: http://forums.smartclient.com/showthread.php?t=19551
    Last edited by Sytematic; 21 Nov 2011, 02:23.

    Comment


      #3
      See the docs for setEditorType, you can't call methods on the "final TeacherSelectItem" because that's not the actual live FormItem, it's just a template used to create FormItems.

      Comment


        #4
        Ah yes, to fix this, and for reference for other people, I did this:

        Code:
        	public String formatValue(Object value, Record record , DynamicForm form, FormItem item) {
        			SelectItem item2 = new SelectItem(item.getJsObj());
        			ListGridRecord r = item2.getSelectedRecord();
        	                return r.getAttribute("firstname") +" " +r.getAttribute("surname");
        		
        		}

        Comment

        Working...
        X