GXT -coloring entire grid row according to one cel

2019-05-18 03:31发布

问题:

. . I colored one column according to the value of cell but i want to color the entire row (means the cell contained row ) in gxt grid help me here is my code for coloring the cell (i want to color the row instead of the cell)

 /*------------Coloring Area------------*/
                    GridCellRenderer<BeanModelType> ColoredGrid = new GridCellRenderer<BeanModelType>() {

                        @Override
                        public Object render(BeanModelType model,
                                String property, ColumnData config,
                                int rowIndex, int colIndex,
                                ListStore<BeanModelType> store,
                                Grid<BeanModelType> grid) {

                            String valueOfCell =  model.get(property);    
                            String style = valueOfCell.equals("Book") ? "GREEN":
                            valueOfCell.equals("Ersr") ? "red":
                            valueOfCell.equals("Pen") ? "yellow":
                            valueOfCell.equals("comp") ? "blue": "";
                            //Config is the cell and we are setting style here

                            config.style ="background-color:"+style;
                            return valueOfCell; 



                        }    

                        };  
                        System.out.println("COLORRRRR   "+cleanColoredGrid.toString());
                        column.setRenderer(ColoredGrid);  

                    /*-------------Coloring Area Ends-------*/
                    configs.add(column); 

回答1:

In every render method you got model as one of parameter, so try to set the same renderer to each column, but replace 'property' to name of attribute which holds string with type of item. Let's suppose you called it 'itemName', so change your code to:

model.get("itemName");  

Maybe casting will be required, because model.get() should return Object.

Now in every column the same check will be performed and all of them should be in one color. If that will work, next step could be some optimizations: if first check returns some color, set it into hashmap of model-to-color (or into the model directly as a new attribute) and add in the renderer a condition which will check if color wasn't already assigned.



回答2:

Given you are using GXT > 2.x.x, the correct way to do this is to attach a new GridViewConfig to your grid's view.

You should probably do something like:

grid.getView().setViewConfig(new GridViewConfig() {
        @Override
        public String getRowStyle(ModelData model, int rowIndex, ListStore<ModelData> ds) {
            if (model != null) {
                                    //TODO: put your conditions here
                if ("YOUR_CONDITION".equals(model.get("BOOK_COLOR))) {
                    return "green-row";
                }
            }
            return "";
        }
    });

You should amend your css accordingly. (note that green-row is a name of a css style class).

See this for reference: http://www.jarvana.com/jarvana/view/com/extjs/gxt/2.1.1/gxt-2.1.1-javadoc.jar!/gxt-2.1.1-javadoc/com/extjs/gxt/ui/client/widget/grid/GridViewConfig.html



标签: grid gxt