Change span element style for GWT Cell using UiRenderer

你离开我真会死。 提交于 2019-12-04 16:51:38

Because the class of the element is not a constant, you'll want to pass it as an argument to the render method so the cell's render reads:

public void render(Cell.Context context, QuoteProxy value, SafeHtmlBuilder sb) {
  renderer.render(sb, value.getAmount(), value.getChange(),
      value.getChangeSign().contentequals('d') ? res.newstyle.positive() : res.newstyle.negative());
}

I just thought that I would provide an example solution for those that are still struggling with this. In the case where you want to set the style prior to rendering, like in the case of rendering a positive value as "green" and a negative value as "red", you would do the following:

This would be your cell class:

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.uibinder.client.UiRenderer;

public class ExpenseInfoCell extends AbstractCell<YourClassProxy> {

    interface ExpenseInfoCellUiRenderer extends UiRenderer {
        void render(SafeHtmlBuilder sb, String cost, String newStyle);

        ValueStyle getCostStyle();
    }

    private static ExpenseInfoCellUiRenderer renderer = GWT
            .create(ExpenseInfoCellUiRenderer.class);

    @Override
    public void render(Context context, YourClassProxy value, SafeHtmlBuilder sb) {

        String coloredStyle = (value.getCost() < 0) ? renderer.getCostStyle()
                .red() : renderer.getCostStyle().green();

        renderer.render(sb, value.getCost()),
                coloredStyle);
    }

}

And this would be the accompanying UiBinder xml file

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder">
    <ui:style field="costStyle" type="com.myproject.client.tables.MyValueStyle">
        .red {
            color: red;
        }

        .green {
            color: green;
        }
    </ui:style>

    <ui:with type="java.lang.String" field="cost" />
    <ui:with type="java.lang.String" field="newStyle" />
    <div>
        <span class='{newStyle}'>
            <ui:text from='{cost}' />
        </span>
    </div>
</ui:UiBinder> 

Also, note that the field="costStyle" matches the getter in the class "getCostStyle". You must follow this naming convention otherwise the renderer will throw an error.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!