このエントリーをはてなブックマークに追加

スタイル/CSSの変更

CellTable全体のCSSの変更

色だったり、空白の幅を変更など

新しくCellTableResourceを作成して、それを使ってTableを初期化する
ただし、cssはオリジナルの CellTableBasic.cssベースに作成するべき。 そうでない場合、必須のスタイル名が足りない場合エラーになる。
最初にデフォルト渡して、上書きする方法もある

@Source({CellTable.Style.DEFAULT_CSS,"test.css"})
CellTable.Style cellTableStyle();

cellの空白をゼロにする場合のStyle例

.cellTableCell {
  padding: 0px 0px;
  overflow: hidden;
}

実際にCellTableを作成する例

public void onModuleLoad() {
         CellTableResources.INSTANCE.cellTableStyle().ensureInjected();
         CellTable<String>  myCellTable = new CellTable<String>(Integer.MAX_VALUE,CellTableResources.INSTANCE);

         myCellTable.addColumn(new TextColumn<String>() {

            @Override
            public String getValue(String object) {
                return object;
            }

        });
         myCellTable.setRowData(Lists.newArrayList("hello","world"));
         RootLayoutPanel.get().add(myCellTable);
    }

CellTableResources.java

package com.akjava.gwt.test2.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.CellTable.Resources;

public interface CellTableResources extends Resources {

        public CellTableResources INSTANCE =
                GWT.create(CellTableResources.class);

        /**
         * The styles used in this widget.
         */
        @Source("test.css")
        CellTable.Style cellTableStyle();
}

情報源 http://stackoverflow.com/questions/5993918

Columnごとの変更

Columnには、setCellStyleNamesのメソッドがあるので、それを使う。

TextColumn<String> column=new TextColumn<String>() {
                @Override
                public String getValue(String object) {
                    return object;
                } 
            };
column.setCellStyleNames("mystyle");

各データごとの変更

SafeHtml Column使って、style付きのHTMLを返すようにする。 応用としては以下の様なクラスを作成する。

package com.akjava.gwt.lib.client.widget.cell;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.SafeHtmlCell;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.Column;

public abstract class StyledTextColumn<T> extends Column<T,SafeHtml>{
    public static class StyleAndLabel{
        public StyleAndLabel(String style,String label){
            if(style==null){
                style="";
            }
            this.style=style;
            if(label==null){
                label="";
            }
            this.label=label;
        }
        private String style;
        private String label;
        public String getStyle() {
            return style;
        }
        public void setStyle(String style) {
            this.style = style;
        }
        public String getLabel() {
            return label;
        }
        public void setLabel(String label) {
            this.label = label;
        }
    }

    public StyledTextColumn(AbstractCell<SafeHtml> cell) {
        super(cell);
    }

    public StyledTextColumn() {
        this(new SafeHtmlCell());
    }

    @Override
    public SafeHtml getValue(T object) {
        StyleAndLabel styleAndLabel=getStyleAndLabel(object);
         SafeHtmlBuilder sb = new SafeHtmlBuilder();
         sb.appendHtmlConstant("<span class=\"" + styleAndLabel.getStyle()
                 + "\">");
             sb.appendEscaped(styleAndLabel.getLabel());
             sb.appendHtmlConstant("</span>");
         return sb.toSafeHtml();
    }

    public abstract StyleAndLabel getStyleAndLabel(T object);

}