如何根据单元格的值(GWT)设置单元格表中特定单元格的样式?
好的,我有一个如何根据单元格的值(GWT)设置单元格表中特定单元格的样式?,gwt,Gwt,好的,我有一个CellTable,它有3列2行。我希望表格中某些特定单元格(不是所有单元格)中的文本为粗体 请看下面的代码: ListDataProvider<List<String>> dataProvider = new ListDataProvider<List<String>>(); dataProvider.addDataDisplay(table); List<List<String>> list = dataP
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");
然后,它将使整个专栏加粗
因此,我认为我们需要在第3列中的每个单元格上循环并相应地设置样式,这样我们可以得到如下结果:
Col1 - Col2 - Col3
223 - 546 - 698
123 - 876 - 898
123 - 896 - 438
Col1-Col2-Col3
223 - 546 - 698
123 - 876 - 898
123 - 896 - 438
为列重写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";
}
};
Column dueColumn=新列(新的DateCell(DateTimeFormat.getFormat(预定义格式.MONTH\u ABBR\u DAY))){
@凌驾
公共日期getValue(文档){
返回文档。getDueDate();
}
@凌驾
公共字符串GetCellStyleName(上下文、文档){
if(document.getDueDate().getTime()
您也可以尝试扩展AbstractCell
请在这里阅读
示例代码:
Cell Cell=new BoldCell();
Column name Column=新列(单元格){
@凌驾
公共字符串getValue(联系人对象){
返回object.name;
}
};
表.addColumn(名称栏,“名称”);
您拥有您的对象。您可以在决定应用于此特定单元格的样式时使用此对象的任何属性。它不必是此列中使用的属性。例如,您可以比较document.getDueDate()和document.getStartDate()。问题是,我只能根据单元格和列之间的交叉位置设置单元格的样式,你可以在这里看到我的新问题。我提供的解决方案专门针对行和列之间的交叉。行有不同的解决方案,列有另一种解决方案。此解决方案针对单个c嗯。如果您不清楚是否需要完整的代码,请告诉我。非常感谢Braj,我这么做了。关键是我们可以通过IndexedColumn中getCellStyleNames的上下文参数获取行索引。我以前不知道如何获取IndexedColumn中的行索引。无论如何,我做的与您的略有不同,但逻辑是相同的&我修复了艾德,很简单,非常感谢。
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";
}
};
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);
}
}
...
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");