× {{alert.msg}} Never ask again
Receive New Tutorials
GET IT FREE

How to Add Custom CSS to a Sencha GXT 3 Grid

– {{showDate(postTime)}}

Codementor Java Expert Tobias Bayer is one of the top 7% answerers on Stack Overflow. He has a focus on UI frameworks such as GXT and Vaadin and has 10 years experience of programming as a full-stack Java developer.

This tutorial was originally posted on his blog.


 

Every time I try to add some custom CSS to a Sencha GXT 3 Grid I have to look up the details again because of the relatively large amount of unintuitive code that is necessary for that simple task.

So I decided to write a short wrap-up of the process as a reference.

First create a file in the client classpath with your custom CSS in it.

com/codebrickie/sample/client/css/MyGrid.css

.cellDisabled {
  background-color: #EEEEEE !important;
  color: #000000;
}

Next, create a custom MyGridAppearance that extends the GridAppearance your application is using (e.g. GrayGridAppearance).

com/codebrickie/sample/client/MyGridAppearance.java

public class MyGridAppearance extends GrayGridAppearance {

}

In this Appearance include an interface that extends the style of the component you want to add your custom style to. The interface must include a method named after your custom CSS style.

com/codebrickie/sample/client/MyGridAppearance.java

	
public class MyGridAppearance extends GrayGridAppearance {

    public interface MyGridStyle extends GrayGridStyle {
        String cellDisabled();
    }
}

Now include another interface that pulls your CSS file into the bundle. It must have a @Source annotation with all the CSS files along the inheritance path and your custom CSS file as well.

com/codebrickie/sample/client/MyGridAppearance.java

public class MyGridAppearance extends GrayGridAppearance {

    public interface MyGridStyle extends GrayGridStyle {
        String cellDisabled();
    }

    public interface MyGridResources extends GrayGridResources, ClientBundle {

        @Override
        @Source({ "com/sencha/gxt/theme/base/client/grid/Grid.css",
                "com/sencha/gxt/theme/gray/client/grid/GrayGrid.css",
                "com/codebrickie/sample/client/css/MyGrid.css" })
        MyGridStyle css();
    }
}

The last step for the Appearance is to implement two constructors.

com/codebrickie/sample/client/MyGridAppearance.java


public class MyGridAppearance extends GrayGridAppearance {

    public interface MyGridStyle extends GrayGridStyle {
        String cellDisabled();
    }

    public interface MyGridResources extends GrayGridResources, ClientBundle {

        @Override
        @Source({ "com/sencha/gxt/theme/base/client/grid/Grid.css",
                "com/sencha/gxt/theme/gray/client/grid/GrayGrid.css",
                "com/codebrickie/sample/client/css/MyGrid.css" })
        MyGridStyle css();
    }

    public MyGridAppearance() {
        this(GWT. create(MyGridResources.class));
    }

    public MyGridAppearance(final MyGridResources resources) {
        super(resources);
    }
}

Now that the Appearance is finished, you have to set it to your grid upon creation.

com/codebrickie/sample/client/MyGridCreation.java


//...

grid = new Grid(store, columnModel, new GridView(
                new MyGridAppearance()));
//...
}

Afterwards you can use your custom style e.g. in getColStyle().

com/codebrickie/sample/client/MyGridCreation.java

//...

GridViewConfig viewConfig = new GridViewConfig() {

  final MyGridStyle styles = (MyGridStyle) ((MyGridAppearance) grid
      .getView().getAppearance())
      .styles();

  @Override
  public String getColStyle(final MyModel model,
          final ValueProvider<? super MyModel, ?> valueProvider, final int rowIndex,
          final int colIndex) {
      String style = null;

      if (!model.isEnabled()) {
          style = styles.cellDisabled();
      }

      return style;
  }

  @Override
  public String getRowStyle(final MyModel model, final int rowIndex) {
      return null;
  }
};

//...

Tobias BayerNeed Tobias’s help? Book a 1-on-1 session!

View Tobias’s Profile

or join us as an expert mentor!



Author
Tobias Bayer
Tobias Bayer
5.0
Java and iOS developer with some affection for Clojure.
Over ten years experience in full stack Java development. More than three years in iOS. Amongst top 7% reputation on Stack Overflow. I speak English and German. I'm quite friendly. :)
Hire the Author

Questions about this tutorial?  Get Live 1:1 help from Java experts!
Duc Duy Bui
Duc Duy Bui
5.0
Senior Software Developer / Freelancer
Freelancer, Professional with over 13 years of experience specializing in developing solutions for finance and insurance companies. My strength is...
Hire this Expert
Ahsan Zia
Ahsan Zia
5.0
Full stack software developer with 6 years of experience
I am a full stack software developer and a Master of Android development while also having expertise on Python, Spring boot, .NET core, Laravel and...
Hire this Expert
comments powered by Disqus