Announcement

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

    How to work with multi-value canvas item

    Hi, we use smartgwt 2.5.1 on firefox.

    The goal is following: I tried the example on smartgwt showcase with ListGrid CanvasItem. It renders nicely. The functionality i want to achieve is like SelectionItem Grid and picklist combined appearance - so you have a plain list of data with checkbox on the left. I was not able to do it with SelectionItem as in Grid mode it has not very useful selection method.

    The problem: The canvas item should return list of records selected. Same should be the value for the canvas item - the data to Grid are loaded from dataSource and the value defines which records are selected. The problem is that im unable to set the value of CanvasItem to Record nor RecordList, only JavaScriptObject. In onShowValue method the event contains the value correctly so there im able to handle it(construct recordList from the JSO), but as i use dataSource the data is retrieved asynchronously and therefore later. So i've added dataArrivedHandler but there im unable to get the value - its JavaScriptObject but im unable to construct Record nor RecordList out of it.

    This is the listGridItem:
    Code:
    	public class ListGridItem extends CanvasItem {
    		ListGridItem(String name, String title, final String listGridIdField) {
    			super(name, title);
    			setShouldSaveValue(true);
    			addShowValueHandler(new ShowValueHandler() {
    				@Override
    				public void onShowValue(ShowValueEvent event) {
    					CanvasItem item = (CanvasItem) event.getSource();
    
    					ListGrid grid = (ListGrid) item.getCanvas();
    					if (grid == null) return;
    
    					grid.deselectAllRecords();
    
    					RecordList recordList = grid.getDataAsRecordList();
    					Record[] itemRecords = event.getDataValueAsRecordList().toArray();
    					item.storeValue(event.getDataValueAsRecordList());
    					for (Record itemRecord : itemRecords) {
    						grid.selectRecord(recordList.findIndex(listGridIdField, itemRecord.getAttribute(listGridIdField)));
    					}
    				}
    			});
    
    			setInitHandler(new FormItemInitHandler() {
    				@Override
    				public void onInit(FormItem item) {
    					ListGrid grid = new ListGrid();
    					grid.setLeaveScrollbarGap(false);
    					grid.setShowHeader(false);
    					grid.setSelectionAppearance(SelectionAppearance.CHECKBOX);
    					grid.setFields(((ListGridItem) item).getGridFields());
    					if (gridDataSource != null) {
    						grid.setDataSource(gridDataSource);
    					} else {
    						grid.setData(((ListGridItem) item).getGridData());
    					}
    					if (dataGridCriteria != null) grid.setInitialCriteria(dataGridCriteria);
    					grid.addFetchDataHandler(new FetchDataHandler() {
    						@Override
    						public void onFilterData(FetchDataEvent event) {
    							event.getRequestProperties().setAttribute(DomainClassDataSource.WIDGET_TYPE_ATTRIBUTE, ConfigurationType.TABLE_CONFIGURATION.getCode());
    						}
    					});
    					grid.setAutoFetchData(true);
    
    					grid.addDataArrivedHandler(new DataArrivedHandler() {
    						@Override
    						public void onDataArrived(DataArrivedEvent event) {
    							ListGrid grid = (ListGrid) event.getSource();
    							if (grid == null) return;
    
    							grid.deselectAllRecords();
    
    							RecordList recordList = grid.getDataAsRecordList();
    							Record[] itemRecords = ((RecordList) ListGridItem.this.getValue()).toArray();
    
    							for (Record itemRecord : itemRecords) {
    								grid.selectRecord(recordList.findIndex(listGridIdField, itemRecord.getAttribute(listGridIdField)));
    							}
    						}
    					});
    
    					grid.addSelectionUpdatedHandler(new SelectionUpdatedHandler() {
    						@Override
    						public void onSelectionUpdated(SelectionUpdatedEvent event) {
    							ListGrid grid = (ListGrid) event.getSource();
    							CanvasItem item = grid.getCanvasItem();
    							item.storeValue(new RecordList(grid.getSelectedRecords()));
    						}
    					});
    
    					((CanvasItem) item).setCanvas(grid);
    				}
    			});
    		}
    
    		private ListGridRecord[] gridData;
    		private DataSource gridDataSource;
    		private ListGridField[] gridFields;
    		private Criteria dataGridCriteria;
    
    		public void setGridData(ListGridRecord[] gridData) {
    			this.gridData = gridData;
    		}
    
    		public ListGridRecord[] getGridData() {
    			return gridData;
    		}
    
    		public void setGridDataSource(DataSource gridDataSource) {
    			this.gridDataSource = gridDataSource;
    		}
    
    		public DataSource getGridDataSource() {
    			return gridDataSource;
    		}
    
    		public void setGridFields(ListGridField... gridFields) {
    			this.gridFields = gridFields;
    		}
    
    		public ListGridField[] getGridFields() {
    			return gridFields;
    		}
    
    		public Criteria getDataGridCriteria() {
    			return dataGridCriteria;
    		}
    
    		public void setDataGridCriteria(Criteria dataGridCriteria) {
    			this.dataGridCriteria = dataGridCriteria;
    		}
    	}

    #2
    You need to store the value in onShowValue then apply it when DataArrived fires.

    Comment


      #3
      You mean item.storeValue() or just put it into my variable? How it then whould work with form record value?

      Comment


        #4
        Just put it in a variable, and apply it on DataArrived. Call storeValue() only once the user has changed the value, as with CanvasItem in general.

        Comment


          #5
          Hi, you might want to see my MultipleItem:
          http://forums.smartclient.com/showpo...9&postcount=13

          Comment

          Working...
          X