简体   繁体   中英

How to add presenter to UiRenderer in GWT

I have created the CellRenderer using UiRenderer in GWT as mentioned at this page Link . Everything is working fine except I just don't know how to how to reset all elements in that list, Here is my code.

View Class:

/**
 * Created by ashish on 2/23/15.
 */
public class SimpleListCell extends AbstractCell<NewsDto> {

    public interface SimpleListCellRenderer extends UiRenderer {
        void render(SafeHtmlBuilder sb,String title,String description);
        void onBrowserEvent(SimpleListCell newsCell, NativeEvent event, Element parent, NewsDto value);
        SpanElement getDescriptionSpan(Element parent);
    }

    private static SimpleListCellRenderer uiNewsCellRenderer = GWT.create(SimpleListCellRenderer.class);

    public SimpleListCell(){
        super("click");
    }

    @Override
    public void onBrowserEvent(Context context, Element parent, NewsDto value,
                               NativeEvent event, ValueUpdater<NewsDto> updater) {
        uiNewsCellRenderer.onBrowserEvent(this, event, parent, value);
    }

    @Override
    public void render(Context context, NewsDto newsDto, SafeHtmlBuilder safeHtmlBuilder) {
        String title = newsDto.getTitle();
        String description = newsDto.getShortDescription();
        uiNewsCellRenderer.render(safeHtmlBuilder,title,description);
    }

    @UiHandler({"selectedNews"})
    void onSelectNews(ClickEvent event,Element parent,NewsDto value){
        GWT.log(value.getDescription());
        uiNewsCellRenderer.getDescriptionSpan(parent).setInnerText(value.getDescription());
    }
}

UiBinder class.

<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'
             xmlns:g='urn:import:com.google.gwt.user.client.ui'>
    <ui:with field="title" type="java.lang.String"/>
    <ui:with field="description" type="java.lang.String" />
    <ui:style>
    </ui:style>
    <div >
        <span ui:field="selectedNews">
            <h3><ui:text from="{title}" /> </h3>
            <p><span ui:field="descriptionSpan"><ui:text from="{description}"/></span></p>
            <hr/>
        </span>
    </div>
</ui:UiBinder>

Now when I am clicking on some news item it is updating the description with the big description but I wants to reset the description of rest of the news. Any thought how to achieve that.

You can check in your render method if the input argument newsDto is the selected object in your list and render it differently. In this case, you won't use uiNewsCellRenderer. You can define a SafeHtmlTemplate (not needed if you don't have have html) and use it to render SafeHtml into SafeHtmlBuilder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM