0

我正在使用以下方法单独设置 CellList 项目的样式:

cellList.getRowElement(0).addClassName("style-name-A");
cellList.getRowElement(1).addClassName("style-name-B");

这反映在我运行应用程序时。但是,当我单击任何行项目时,所有项目都会丢失这些样式并恢复为 CellList 的标准样式(在我的 CellListResources css 文件中定义)。我怎样才能阻止这种行为?

我通过 ListDataProvider 向 CellList 添加项目;使用 MultiSelectionModel 的子类来处理选择,并将我自己的 CellListResources 传递给 CellList 构造函数以定义基本样式。

4

1 回答 1

0

发生这种情况是因为选择重新呈现了该行,因此丢失了您对此元素所做的任何更改。

我为此找到的解决方法是在单个单元格级别处理它。

在实践中,我将每个单元格包装成一个“StyleAwareCellDecorator”,它创建一个具有给定样式的跨度。然后我根据一些逻辑决定应用什么样式。在您的情况下,它可能基于 context.getIndex() 。

public class StyleAwareCellDecorator<C> extends AbtractCell<C> {

    private final Cell<C> decorated;
    private final MyLogic logic;

    public StyleAwareCellDecorator( Cell<C> decorated, MyLogic logic ) {
        this.decorated = decorated;
        this.logic = logic;
    }

    void render(Context context, C value, SafeHtmlBuilder sb) {
        String theCssClass = myLogic.decideWhatStyleToUse( context.getIndex() );
        sb.append( ... some tag with class="theCssClass" ... );
        delegated.render( context, value, sb );
        sb.append( ... close tag ... );
    }
}

有了这个设施,您可以将您的代码替换为:

MyLogic myCellStyleLogic;
myCellStyleLogic.useStyleForRowIndex( "style-name-A", 0 );
myCellStyleLogic.useStyleForRowIndex( "style-name-B", 1 );

假设将其存储在地图中

于 2013-03-27T19:55:59.030 回答