GXT - 根据行中的一个单元格整列整个网格行

时间:2012-01-12 05:39:17

标签: grid gxt

。 。    我根据单元格的值着色了一列,但我想在gxt网格中为整行(表示包含行的单元格)着色帮助我  这是我为单元格着色的代码(我想为行而不是单元格着色)

 /*------------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); 

2 个答案:

答案 0 :(得分:8)

鉴于您正在使用GXT&gt; 2.x.x,正确的方法是将新的GridViewConfig附加到网格视图中。

你应该做的事情如下:

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 "";
        }
    });

你应该相应修改你的CSS。 (请注意, green-row 是css样式类的名称。)

请参阅此参考: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

答案 1 :(得分:2)

在每个渲染方法中,您都将模型作为参数之一,因此请尝试为每个列设置相同的渲染器,但将“property”替换为包含item类型的字符串的属性名称。我们假设您将其称为“itemName”,因此请将代码更改为:

model.get("itemName");  

可能需要进行强制转换,因为model.get()应该返回Object。

现在在每一列中都会执行相同的检查,并且所有检查都应该是一种颜色。 如果这样可行,下一步可能是一些优化:如果第一次检查返回一些颜色,将其设置为模型到颜色的hashmap(或直接作为新属性进入模型)并在渲染器中添加一个条件,它将检查如果尚未指定颜色。