Search code examples
gwtsmartgwt

Change grid column filter in SmartGWT


If you create a custom DataSource in SmartGWT, is it possible to remove a field filter instead of hiding it entirely from the grid columns?

As seen here:

enter image description here

The Country code field exists, but is hidden in the above image.

Clarification: I want to hide the Country code field at the start, but still have it visible in the Columns context menu. If you use setHidden(true) the field disappears from the Columns menu above.

Example code:

public class MyDataSource extends DataSource {

    public MyDataSource() {
        DataSourceField countryField = new DataSourceIntegerField("country", "Country code");
        // TODO Find a method that disables the filter, aka hides but not removes the field from the grid.
        countryField.setHidden(true); // Completely hides/removes the field, not desireable.
        countryField.setCanFilter(true); // Doesn't seem to change anything. 
        addField(countryField);

        // Other fields...
    }

}

How would this be achieved in a ListGrid with the above DataSource?


Solution

  • I'm not sure if I fully understood the question.

    Are you looking for a way to hide the "Country code" option from the Columns context menu? You can do that by declaring ListGridField.setCanHide(false) to the corresponding ListGridField.

    ListGridField countryCode = new ListGridField("countryCode");
    countryCode.setHidden(true);
    countryCode.setCanHide(false); // won't be shown in the context menu
    

    Or, are you trying to disable filtering?

    If so, does the user have to have the option to see the country code column in some cases? If not, you can just leave the MyDataSource as it is and define only those ListGridFields that you want the user to see.

    ListGrid grid = new ListGrid();
    ListGridField country = new ListGridField("country");
    ListGridField capital = new ListGridField("capital");
    ListGridField continent = new ListGridField("continent");
    // no countryCode here
    grid.setFields(country, capital, continent);
    

    The underlying country code attribute is still available in the code, eg. via record.getAttribute("countryCode");, it's just not shown in the ListGrid.

    Alternatively, you can define the filtering on grid level with ListGridField.canFilter(Boolean canFilter).

    ListGridField countryCode = new ListGridField("countryCode ");
    countryCode.setCanFilter(false);
    

    EDIT:

    So, don't set the hidden attribute in the datasource, but instead directly to the ListGridField.

    DataSource

    public class MyDataSource extends DataSource {
    
      public MyDataSource() {
        DataSourceField countryCode = new DataSourceStringField("countryCode", "Country code");
        DataSourceField country = new DataSourceStringField("country", "Country");
        DataSourceField capital = new DataSourceStringField("capital", "Capital");
        DataSourceField continent = new DataSourceStringField("continent", "Continent");
    
        setFields(countryCode, country, capital, continent);
      }
    }
    

    ListGrid

    ListGrid grid = new ListGrid();
    ListGridField countryCode = new ListGridField("countryCode");
    countryCode.setHidden(true);
    countryCode.setCanHide(true); // I don't think this is even necessary.
    ListGridField country = new ListGridField("country");
    ListGridField capital = new ListGridField("capital");
    ListGridField continent = new ListGridField("continent");
    grid.setFields(countryCode, country, capital, continent);
    

    That should do the trick.