Announcement

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

    How to use a ListGrid with a DataSource

    I managed to create a working ListGrid in the following manner :

    Code:
    private ListGrid listGrid = new ListGrid();
    
    listGrid.setShowAllRecords(true);
    ListGridField employeeNameField = new ListGridField("employeeName","EmployeeName");
    ListGridField emailField = new ListGridField("email", "Email");
    ListGridField phoneWorkField = new ListGridField("phonework", "Phone work");
    ListGridField phoneMobileField = new ListGridField("phonemobile", "Phone mobile);
    
    listGrid.setFields(employeeNameField, emailField, phoneWorkField, phoneMobileField);
    
    listGrid.setAutoFetchData(true);
    and then filling the grid like this using a RecordList

    Code:
    RecordList data = new RecordList();
    for (EmployeeResponse employee:response)
    {
       ListGridRecord record = new ListGridRecord();
       record.setAttribute("employeeName", employee.getEmployeeName());
       record.setAttribute("email", employee.getEmail1());
       record.setAttribute("phonework", employee.getPhoneWork());
       record.setAttribute("phonemobile", employee.getPhoneMobile());
       data.add(record);
    }
    
                listGrid.setData(data);

    This is all working fine but now I want to use a filter on the ListGrid (using listGrid.setShowFilterEditor(true); and listGrid.setFilterOnKeyPress(true);
    This however does not seem to work when using a RecordList as data for the ListGrid.
    It seems that a DataSource must be used for this.
    I tried creating a DataSource but the problem is that my table is always empty.

    The things I did.
    Create a DataSource class :
    Code:
    public class EmployeeDataSource extends DataSource
    {
       public EmployeeDataSource()
       {
          setID("abc");
    
          DataSourceTextField employeeNameField = new DataSourceTextField("employeeName", I18NMessages.getMessage(4182));
          DataSourceTextField emailField = new DataSourceTextField("email", I18NMessages.getMessage(4377));
          DataSourceTextField phoneWorkField = new DataSourceTextField("phonework", I18NMessages.getMessage(2338));
          DataSourceTextField phoneMobileField = new DataSourceTextField("phonemobile", I18NMessages.getMessage(2196));
    
          this.setFields(employeeNameField, emailField, phoneWorkField, phoneMobileField);
    
          this.setClientOnly(true);
       }
    }
    Then I created a ListGrid.
    Code:
    private ListGrid listGrid = new ListGrid();
    listGrid.setShowAllRecords(true);
    listGrid.setShowFilterEditor(true);
    listGrid.setFilterOnKeypress(true);
    listGrid.setAutoFetchData(true);
    And finally
    Code:
    ListGridRecord[] records = new ListGridRecord[response.size()];
    for (int i = 0; i < records.length; i++)
    {
       records[i] = new ListGridRecord();
       records[i].setAttribute("employeeName", response.get(i).getEmployeeName());
       records[i].setAttribute("email", response.get(i).getEmail1());
       records[i].setAttribute("phonework", response.get(i).getPhoneWork());
       records[i].setAttribute("phonemobile", response.get(i).getPhoneMobile());
    }
    dataSource.setCacheData(records);
    dataSource.invalidateCache();

    The result of all this is that I get an empty ListGrid.
    The headers are shown correctly however. Since I removed listGrid.setFields() when swithing to a DataSource this means that the column headers really are coming from the DataSource correctly. But I am unable to fetch the data itself.


    Anyone has a quick overview of how to create and use a clientOnly dataSource. And not using a setDataUrl() like all examples I find in the showcase do, but really filling up the datasource with data from an array or a list in the code.

    Thanks

    #2
    Do what you did but don't call DataSource.invalidateCache(), which, as doc'd, removes the data from the DataSource.

    You may have been thinking of ListGrid.invalidateCache(), but you don't need to call that either; just fetchData() or setting autoFetchData.

    Comment


      #3
      Originally posted by Isomorphic View Post
      Do what you did but don't call DataSource.invalidateCache(), which, as doc'd, removes the data from the DataSource.

      You may have been thinking of ListGrid.invalidateCache(), but you don't need to call that either; just fetchData() or setting autoFetchData.
      Thanks.
      That indeed solved the problem.

      Comment

      Working...
      X