Announcement

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

    How can I tell SelectItem that the datasource is updated?

    Hello, everybody

    I'm using SmartGWT 2.1. The underlying data (Oracle database) have changed and I need to tell the SelectItem control to update its options.

    I tried DataSource.invalidateCaches but it does not work. Commenting out setClientOnly(true) does not help either.

    Could you help me on this? Below are the code snippets and please tell me if there is anything unclear. (The general idea of the code is that, there is a SelectItem in the guideform to select a "template"; now a new "template" is added by the user, the SelectItem need to fetch the data again to see the changes.)

    Code:
    //This is the guideform:
    public class GuideForm extends HLayout
    	public GuideForm() {
            ... ...
            templateItem = new SelectItem("template", "select a template");
    		templateItem.setOptionDataSource(BasicOptionDataSource
    				.getInstance("GUIDETEMPLATE"));
    		templateItem.setRedrawOnChange(true);
    		templateItem.setValueField("value");
    		templateItem.setDisplayField("display");
    		templateItem.setWidth("*");
            ... ...
         }
    Code:
    //This is the datasource:
    public class BasicOptionDataSource extends DataSource
         	public BasicOptionDataSource(String category) {
                this.category = category;
                setDataProtocol(DSProtocol.CLIENTCUSTOM);
                setDataFormat(DSDataFormat.CUSTOM);
                setID(category + "__OptionDS");
                DataSourceTextField valueField = new DataSourceTextField("value",
                        "value");
                valueField.setPrimaryKey(true);
                valueField.setRequired(true);
                DataSourceTextField displayField = new DataSourceTextField("display",
                        "display");
                setFields(valueField, displayField);
                setClientOnly(true);
                //setCacheAllData(true);
    	    }
        
    	protected void executeFetch(final String requestId,
    			final DSRequest request, final DSResponse response) {
    		UtilService.Util.getInstance().getOptions(category,
                new AsyncCallback<List<OptionItem>>() {
                    @Override
                    public void onSuccess(List<OptionItem> result) {
                        List<ListGridRecord> records = new ArrayList<ListGridRecord>();
                        addTo(result, records);
                        response.setData(records.toArray(new ListGridRecord[0]));
                        response.setTotalRows(result.size());
                        processResponse(requestId, response);
                    }
    
                    @Override
                    public void onFailure(Throwable caught) {
                        response.setStatus(RPCResponse.STATUS_FAILURE);
                        processResponse(requestId, response);
                    }
    
                    private void addTo(List<OptionItem> result,
                            List<ListGridRecord> records) {
                        for (OptionItem service : result) {
                            ListGridRecord record = new ListGridRecord();
                            record.setAttribute("value", service.getValue());
                            record.setAttribute("display", service.getDisplay());
                            records.add(record);
                        }
                    }
                });
    	}
    Code:
    //The underlining data are changed (a new template is added) 
    //so I need to update the options in SelectItem 
    public class GuideTemplateForm extends HLayout
        private void initHandlers() {
    	insertBtn.addClickHandler(new ClickHandler() {
    	     public void onClick(ClickEvent event) {
    		    if (!dynamicForm.validate()) {
    			return;
    		    }
    
    		    GuideTemplate template = getData();
    		    BaseMgtService.Util.getInstance().insertGuideTemplate(
    			    template, new AsyncCallback<Void>() {
                            @Override
                            public void onSuccess(Void result) {
                                Tab tab = MyTabSet.getInstance()
                                        .getSelectedTab();
                                MyTabSet.getInstance().removeTab(tab);
                                String tabId = MyTabSet.TAB_GUIDETEMPLATE_MANAGEMENT;
                                Tab tab1 = MyTabSet.getInstance().getTab(tabId);
                                if (tab1 != null) {
                                    tab1.getPane().redraw();
                                    MyTabSet.getInstance().selectTab(tab1);
                                }
                                
                                //Tried to invalidate the cache but failed
                                BasicOptionDataSource.getInstance("GUIDETEMPLATE").invalidateCache();								 
                            }
    
                            @Override
                            public void onFailure(Throwable caught) {
                                MessengerHelper.error(caught.getMessage());
                            }
    					});
    			}
    		});
    Regards,
    Warren Tang
    Last edited by warrentang; 9 Dec 2010, 02:54.

    #2
    Updates like this are intended to be happen automatically via cache sync - see the FAQ on grids not updating.

    Comment


      #3
      Thanks for the hint.

      After a lot of trials I solved the problem by implementing the executeUpdate method, which does nothing but redirects to executeFetch. Then I call DataSource.updateData(new Record()) to trigger the fake update. Finally the cache is refreshed. Maybe there are better ways, but it just works.

      The link to the FAQ: http://forums.smartclient.com/showth...p?t=8159#aGrid

      Code:
      public class BasicOptionDataSource extends DataSource {
      	protected void executeUpdate(String requestId, DSRequest request,
      			DSResponse response) {	
      		//Just redirect to Fetch
      		executeFetch(requestId, request, response);
      	}
          
      	public void refreshCache()
      	{
      		this.updateData(new Record());
          }
      Last edited by warrentang; 9 Dec 2010, 19:47.

      Comment

      Working...
      X