Announcement

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

    How do you add a row to a Client-Side DataSource in Smartgwt?

    If you double click inside the `ListGrid`, you should see **3** rows instead of **2**. What is happening is the `DataSource` is refreshing the `CacheData` but the `ListGrid` isn't showing the added row.

    What am I missing?


    Code:
    public void onModuleLoad() {
            DataSourceTextField continentField = new DataSourceTextField("continent");
            continentField.setPrimaryKey(true);
            
            DataSource dataSource = new DataSource();
            dataSource.setClientOnly(true);
            dataSource.setFields(continentField);
            dataSource.setCacheData(CountryData.getNewRecords());
            
            final ListGrid myGrid = new ListGrid();
            myGrid.setWidth(200);
            myGrid.setHeight(100);
            myGrid.setDataSource(dataSource);
            myGrid.fetchData();
            myGrid.draw();
            
            myGrid.addDoubleClickHandler(new DoubleClickHandler() {
                
                @Override
                public void onDoubleClick(DoubleClickEvent event) {
                    List<Record> records = new ArrayList<Record>(Arrays.asList(myGrid.getDataSource().getCacheData()));
                    
                    records.add(new CountryRecord("Japan"));
                    
                    myGrid.getDataSource().setCacheData(records.toArray(new Record[0]));
                    myGrid.fetchData();
                    SC.say("Number of records: " + myGrid.getDataSource().getCacheData().length);
                }
            });
        }
        
        static class CountryData {
        
            public static CountryRecord[] getNewRecords() {
                return new CountryRecord[] { 
                        new CountryRecord("North America"), 
                        new CountryRecord("Asia") };
            }
        }
        
        static class CountryRecord extends ListGridRecord {
            public CountryRecord(String continent) {
                setContinent(continent);
            }
        
            public void setContinent(String continent) {
                setAttribute("continent", continent);
            }
        
            public String getContinent() {
                return getAttributeAsString("continent");
            }
        }

    #2
    Your fetchData() is a no-op because there's been no change in criteria - see ListGrid.fetchData() docs. See also DataSource.updateCaches() for an approach that probably makes more sense here.

    Comment


      #3
      in the docs, it is said that DataSource.updateCaches must NOT be used on a client-side dataSource.

      So my approach would be to set the cache on the first call of the dataSource and then use addData and removeData to add/remove. but in an issue, it was said that addData and removeData are server calls. so is my approach correct? and will addData and removeData modify the cachedData?

      Comment


        #4
        That's correct, for a clientOnly DataSource, use addData/removeData/updateData. These are normally server calls, but are not for a clientOnly DataSource.

        Comment


          #5
          I still have 1 problem:

          Using a SelectionStyle.CHECKBOX will generate a "can't select that many records at once" warning message in the header CheckBox which will be also disabled because the cacheData is not filled immediately but delayed and the DataSource is drawn with an empty cacheData.

          How can I solve this? and do you need a test case?

          PS: This only happens on the initial load of the dataSource. If the DataSource was empty from start and was drawn on the screen, If you then add a record, the header checkbox will work!!

          Comment


            #6
            If you're doing programmatic selection, you need to do it from DataArrived.

            Comment

            Working...
            X