如何根据该单元格的值(GWT)设置CellTable中的特定单元格?

时间:2014-03-23 23:36:03

标签: gwt

好的,我的CellTable有3列2行。我希望表中某些特定单元格(不是所有单元格)中的文本都是粗体。

请看这段代码:

ListDataProvider<List<String>> dataProvider = new ListDataProvider<List<String>>();
dataProvider.addDataDisplay(table);
List<List<String>> list = dataProvider.getList();
List<String> sublist1= Arrays.asList("223","546","698");
List<String> sublist2= Arrays.asList("123","876","898");
List<String> sublist2= Arrays.asList("123","896","438");
IndexedColumn column1=new IndexedColumn(0);
table.addColumn(column1, "Col1");
IndexedColumn column2=new IndexedColumn(1);
table.addColumn(column2, "Col2");
IndexedColumn column3=new IndexedColumn(2);
table.addColumn(column3, "Col3");

现在,我想要与第2行和第2行相交的Cell。 col3(即&#34; 898&#34;)要大胆,所以如果我喜欢这个

column3.setCellStyleNames(getView().getRes().css().myBoldColor());

然后它将使整个列变为BOLD。

所以,我认为我们需要在第3列和第3列中循环每个单元格。相应地设置样式,以便我们可以得到如下结果:

Col1 - Col2 - Col3
223 - 546 - 698
123 - 876 - 898
123 - 896 - 438

2 个答案:

答案 0 :(得分:1)

覆盖列的getCellStyleNames():

Column<Document, Date> dueColumn = new Column<Document, Date>(new DateCell(DateTimeFormat.getFormat(PredefinedFormat.MONTH_ABBR_DAY))) {

    @Override
    public Date getValue(Document document) {
        return document.getDueDate();
    }

    @Override
    public String getCellStyleNames(Context context, Document document) {

    if (document.getDueDate().getTime() < new Date().getTime()) {
        return "boldStyle";
    }
};

答案 1 :(得分:1)

您可以尝试延长AbstractCell

请在此处阅读Implementing the render() Method

示例代码:

static class BoldCell extends AbstractCell<String> {

    /**
     * The HTML templates used to render the cell.
     */
    interface Templates extends SafeHtmlTemplates {
        @SafeHtmlTemplates.Template("<div style=\"{0}\">{1}</div>")
        SafeHtml cell(SafeStyles styles, SafeHtml value);
    }

    /**
     * Create a singleton instance of the templates used to render the cell.
     */
    private static Templates templates = GWT.create(Templates.class);

    @Override
    public void render(Context context, String value, SafeHtmlBuilder sb) {
        /*
         * Always do a null check on the value. Cell widgets can pass null to cells if the
         * underlying data contains a null, or if the data arrives out of order.
         */
        if (value == null) {
            return;
        }

        // If the value comes from the user, we escape it to avoid XSS attacks.
        SafeHtml safeValue = SafeHtmlUtils.fromString(value);

        // Use the template to create the Cell's html.
        FontWeight weight = FontWeight.NORMAL;
        if (safeValue.asString().equals("898")) {
            weight = FontWeight.BOLD;
        }
        SafeStyles styles = SafeStylesUtils.forFontWeight(weight);
        SafeHtml rendered = templates.cell(styles, safeValue);
        sb.append(rendered);
    }
}

在上面的代码中,您可以尝试使用行号(第3行的第0列的粗体值)

        ...
        FontWeight weight = FontWeight.NORMAL;
        if (context.getIndex()==2) {
            weight = FontWeight.BOLD;
        }
        ...

    Cell<String> cell = new BoldCell();

    Column<Contact, String> nameColumn = new Column<Contact, String>(cell) {
        @Override
        public String getValue(Contact object) {
            return object.name;
        }
    };
    table.addColumn(nameColumn, "Name");
相关问题