代码之家  ›  专栏  ›  技术社区  ›  Tum

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

gwt
  •  0
  • Tum  · 技术社区  · 11 年前

    好的,我有一个 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行&col3(即“898”)是粗体字,所以如果我喜欢这个

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

    然后它将使整个列粗体。

    所以,我认为我们需要对第3列中的每个单元格进行循环;相应地设置样式,以便获得如下结果:

    Col1 - Col2 - Col3
    223 - 546 - 698
    123 - 876 - 898
    123 - 896 - 438
    
    2 回复  |  直到 11 年前
        1
  •  1
  •   Braj    11 年前

    你可以尝试扩展 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);
        }
    }
    

    在上面的代码中,您也可以尝试使用no行(第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");
    
        2
  •  1
  •   Andrei Volgin    11 年前

    为列重写getCellStyleName():

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