CellList keep selected Cells selected?

175 views Asked by At

I use the MGWT CellList which works perfect.

I have the following problem. How can I keep selected cells selected such that the remain selected after the user releases the cell?

Here is my implementation:

CellList<Item> myCellList = new CellList<Item>(new ItemCell());

My ItemCell class:

public class ItemCell implements Cell<Item> {

    private static Template TEMPLATE = GWT.create(Template.class);

    public interface Template extends SafeHtmlTemplates {
        @SafeHtmlTemplates.Template("<div>{0}</div>")
        SafeHtml content(String cellContents);
    }

    @Override
    public void render(SafeHtmlBuilder safeHtmlBuilder, Item model) {
        SafeHtml content = TEMPLATE.content(model.getName());

        safeHtmlBuilder.append(content); 

    }

    @Override
    public boolean canBeSelected(Item model) {
        return true;
    }

}

My Item class:

public class Item {

    private String name;

    public Item() {
        setName("");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
1

There are 1 answers

0
Onkar On BEST ANSWER

What you want is selection handler. If you want single slection use SingleSelectionHandler, if you want multiple selections use MultiSelectionHandler, Sample code :

 SelectionModel<Item> selectionModel = new SingleSelectionModel<Item>();
    cellList.setSelectionModel(selectionModel);

if you want to do anything on selection you can do it here

selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
      public void onSelectionChange(SelectionChangeEvent event) {
                   /** Do your thing here **/
           selectionModel.getSelectedObject();  //for single Selection
           selectionModel.getSelectedSet(); //for multiple Selection   
      }
    });