Announcement

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

    ListGrid to Database directly without xml?

    Hi does anyone have a simple example that shows how to connect a ListGrid to a database directly WITHOUT going through the xml?

    Sorry if this was asked but i searched everywhere and couldnt find an example. All the grids demo codes just have setDataSource(someDataSource), then when you look at the actual *DS.java file, all it does is pointing to a xml file. I was able to create my own pipes for the dropdowns..but for listgrid i need the serverside row fetching / lazy rendering etc..that's built into the grid.

    How do you create a datasource that run a select sql then hook the result to the listgrid? For example in the *ds.java file how to replace setDataURL("ds/test_data/supplyItem.data.xml"); with a select sql that gets the data instead of reading it from some static xml.

    I am using the free version of smartgwt.

    thank you
    Last edited by gagaliya; 29 Jul 2010, 07:14.

    #2
    That's not a feature of the free SGWT. You will need to develop your own web services that return the data you want and connect the DataSource to those URLs.

    OR use the SGWT Pro or higher with the provided server framework to EASILY connect to SQL databases without extra coding.

    Comment


      #3
      Originally posted by davidj6
      That's not a feature of the free SGWT. You will need to develop your own web services that return the data you want and connect the DataSource to those URLs.

      OR use the SGWT Pro or higher with the provided server framework to EASILY connect to SQL databases without extra coding.
      1 more question. If i have my own web module, for example: http://www.website.com/returnData will generate and return the required xml. Can i then use something like setDataUrl("http://www.website.com/returnData"). Or do i have to hit the url myself, save the xml data to a local filepath, then point the datasource to it. This becomes unmanageable for a website that has hundreds of users accessing the table, you basically have to save hundreds of temp xml to your local storage then delete them later. Thanks

      Comment


        #4
        The dataUrl will be queried automatically - that's the purpose. Note you can only use relative Url's back to the server from which you app is served due to web security restrictions.

        Comment


          #5
          perfect,thank you so much. Will post back once i have everything working

          Comment


            #6
            Hi, i got this almost working however stuck on the part to pass input params to the servlet. It seems once you call DataSource.setDataURL(SomeUrl), you CANNOT call it again. For example:

            setDataURL("someservlet?input=yes");
            then if you call
            setDataURL("someservlet?input=no");
            it will throw exception saying cannot data url once created.

            Please see below for an example of what i have so far, it illustrates the problem i am having trying to set the inputvar. I tried many different ways to set the inputvar but none of them worked.

            How do you change the setDataURL input dynamically? Thanks!

            Servlet (with web.xml path set to someapp/testservlet)
            Code:
            public class TestServlet extends HttpServlet 
            {
            	
              public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException 
              {
                  
                PrintWriter out = response.getWriter();
                String input= request.getParameter("input");
                if(input="yes")  out.println("some_xml_data_for_grid");
                else out.println("some_OTHER_xml_data_for_grid");
              }
            }
            DataSource:
            Code:
            public class WorldXmlDS extends DataSource {
            
                private static WorldXmlDS instance = null;
            
                public static WorldXmlDS getInstance(String inputvar) {
                    if (instance == null) {
                        instance = new WorldXmlDS("worldDS",inputvar);
                    }
                    else 
                   {
                         //tried many different ways...none works
                        instance.setDataURL("someapp/testservlet?input="+inputvar);
                   }
                    return instance;
                }
            
                public WorldXmlDS(String id, String inputvar) {
            
                    setID(id);
                    setRecordXPath("/List/country");
                    ......etc....
            
                    setDataURL("someapp/testservlet?input="+inputvar);
                    setClientOnly(false);
                }
            }
            GUI
            Code:
            String inputvar = alternateYesAndNo();
            someListGrid.setDataSource(WorldXmlDS.getInstance(inputvar));
            inputvar =  alternateYesAndNo();
            someListGrid.setDataSource(WorldXmlDS.getInstance(inputvar)); //doesnt work

            Comment


              #7
              Your ?input=yes is a parameter. You can apply parameters to DSRequests or provide them as part of the criteria. Read through Client-side Data Integration for more details. In particular, look near the bottom.

              Comment


                #8
                David, thank you so much for your help. I now have a working version (posted below). There are still a few parts(bug?) i am confused about, but there is light at the end of the tunnel now!

                1) 1 single fetchdata() call somehow is calling the servlet multiple times? For example in the debug console i can see the message "servlet called" printed 3 times when i click the button which calls fetchdata() once.

                2) Incorrect number of rows are displayed in the grid, even though the servlet only returns a xml with 1 record, in the grid it is displayed twice (this only happens when param = no)

                SERVLET
                Code:
                public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException 
                  {
                      
                    
                    PrintWriter out = response.getWriter();
                    String param = request.getParameter("param");
                    System.out.println("servlet called "+param);
                    out.println(getTestData(param));
                  }
                
                   private String getTestData(String param)
                   {
                //return 3 rows
                	 if(param!=null && param.equalsIgnoreCase("yes"))
                   		 return "<List><country> ....</country><country> ....</country><country>....</country></List>";
                
                //return 1 row
                	else 
                		 return "<List><country>....</country></List>"; 
                   }
                DATASOURCE
                Code:
                public class DataSourceXmlDS extends DataSource {  
                  
                    private static DataSourceXmlDS instance = null;  
                    
                    public static DataSourceXmlDS getInstance() {  
                    	
                        if (instance == null) {  
                            instance = new DataSourceXmlDS ("worldDS");  
                        }  
                       
                        return instance;  
                    }  
                  
                    public DataSourceXmlDS (String id) {  
                    	
                        setID(id);  
                        setRecordXPath("/List/country");  
                       
                        DataSourceTextField countryCodeField = new DataSourceTextField("countryCode", "Code");  
                        countryCodeField.setRequired(true);  
                         ...etc...
                      
                        setFields( countryCodeField, countryNameField, capitalField, governmentField,  
                                memberG8Field, continentField, independenceField, areaField, populationField,  
                                gdpField);  
                  
                        setDataURL("myservlet/mysearch");  
                    
                    }  
                }

                GUI CLIENT
                Code:
                DSRequest dsRequest = new DSRequest();
                final ListGrid listgrid = new ListGrid();
                int count=0;
                
                public void onModuleLoad() 
                {
                         listgrid.setCanEdit(false);  
                         listgrid.setDataPageSize(50);  
                         listgrid.setDrawAheadRatio(4); 
                         listgrid.setDataSource(DataSourceXmlDS.getInstance());
                
                         somebutton.addClickHandler(new ClickHandler() 
                	 {  
                	        	     public void onClick(ClickEvent event) 
                	        	     {  
                	        	   
                	        	    	 
                	        	    	 listgrid.invalidateCache();
                	        	         HashMap<String, String> hashmap = new HashMap<String,String>();
                	        	    	 
                	        	    	 if(count%2==0) 
                	        	    		 hashmap.put("param","yes");
                		           	 else
                        	    	    		 hashmap.put("param","no");
                		           	 
                                                 count++;
                	        	    	 dsRequest.setParams(hashmap);
                	        	    	 listgrid.fetchData(null,null,dsRequest);
                	        	    	    	        	    	 
                	        	      }  
                	        	               
                      	      });  
                
                }
                ----------------------------------

                1) i click the button, it prints "servlet called param=yes" 3 times, then shows the correct number of rows (3). - why is the servlet called 3 times?

                2) i click the button again, it prints "servlet called param=no" 3 times again, then shows 2 rows - why is it showing 2 rows when the servlet xml only returns 1 row?

                the same behavior is then repeated cycling between 1) and 2) as i keep clicking the button.
                Last edited by gagaliya; 4 Aug 2010, 13:11.

                Comment


                  #9
                  yes you can do it.. no problem, by using RPC calls, If you do with eclipse it will automatically created the RPC call,,
                  All you have to do in in server side (yourprojectnameimpl), write the server connection. then in service class and Async class add that method then you can pass values server to client

                  I recommend in to use eclipse, it already create a project like "hello world program " like taking data from server

                  Comment


                    #10
                    Marbot's rpc implementation was my next try but i fixed both issues (incorrect rows, multiple servlet calls for 1 fetchdata) by using the datasource fetchdata instead of listgrid, then a dscallback to set the data.

                    From the above example change from:
                    Code:
                    listgrid.fetchData(null,null,dsRequest);
                    To:
                    Code:
                      listgrid.getDataSource().fetchData(null,new DSCallback() {
                    	public void execute(DSResponse response, Object rawData, DSRequest request) {
                          	   listgrid.setData(response.getData());
                    	   }} ,dsRequest);
                    So everything looks good now, this basically will send a request to servlet with whatever params desired, servlet will then run its process, return the xml back to the grid, to be displayed.

                    Comment

                    Working...
                    X