LSPS documentation logo
LSPS Documentation
Filter over Grid and Table with a Custom Data Source

Note: This tutorial uses the forms module as its form implementation.

Required outcome: A form with a grid and table that use a custom data source and support filtering.

Note: You can download the tutorial model here: once you have imported the archive, you can test the model by running the filterGrid module.

Implementing a Custom Data Source

  1. Make sure the record of the custom data source implements the forms::DataSource interface.
  2. Add fields to the record for the filters as set on the Columns.
    filteroncustomds_customdsrecord.png
    Custom data source record
  3. Adapt the data source methods so that the getCount() and getData() methods handle the filtering.

    The filters are passed as input parameters to the methods.

Example data source methods

ApplicantDataSource {
   
  public Integer getCount(Collection<forms::Filter> filters){
   
       def String firstnameFilterSubstring := getFilterValue("firstName", filters);
       def String surnameFilterSubstring := getFilterValue("surname", filters);
   
     //count query that filters the results:
     getApplicants_count(firstnameFilterSubstring, surnameFilterSubstring);
    }
   
  public List<Object> getData(Integer startIndex*, Integer count*, Collection<forms::Filter> filters, Set<Sort> sortSpecs){
   
       def String firstnameFilterSubstring := getFilterValue("firstName", filters);
       def String surnameFilterSubstring := getFilterValue("surname", filters);
   
       //query that gets results and applies filters:
       getApplicants(firstnameFilterSubstring, surnameFilterSubstring);
    }
   
    private String getFilterValue (String filterParameterName, Collection<forms::Filter> filters){
       //get first filter with matching name:
       def forms::Filter firstMatchingFilter := getFirst(filters, { f -> f.id == filterParameterName});
       // get search substring in filters:
       def String filterSubstring := firstMatchingFilter == null ? null : (firstMatchingFilter as SubstringFilter).value;
       filterSubstring
    }
   
  public Boolean supportsFilter(forms::Filter filter*){
    if
      filter.id == "firstName" || filter.id == "surname" then true;
    else
      false
    end
  }
   
  public Boolean supportsSort(Sort sort*){
    false
    }
  public String toString() {
    #"ApplicantDataSource"
  }
}

Creating the Form

  1. On the Grid, set the data source to Custom with the value of the data source instance.
    new ApplicantDataSource()
  2. On the columns of the respective component, do the following:
    1. Enable filtering.
      filteroncustomds_filterablecolumn.png
    2. Configure the filter ID on the column:
      • For a Grid column, set on the Init tab as c.setFilterConfig(new FilterConfig(filterId -> "FILTERNAME")).
      • For a Table column, set on the Filtering tab as new FilterConfig(filterId -> "FILTERNAME").