Search code examples
gwtgxt

updating GWT(GXT) Buttoncell in a grid at runtime


I am creating a button cell in GXT Grid, and adding the cell in my grid column like this

  myCol.setCell(getButtonCell());

Now all cells are drawn , after that depends on the server call i want to update my cell with a new Value of MyDTO.

So on my RPC success , I want to call this render for all of my cells and update there values. How can i achieve this.

public ButtonCell<MyDTO> getButtonCell()
{
    ButtonCell<MyDTO> cellTest = new ButtonCell<MyDTO>()
    {

        @Override
        public void render(Context context, MyDTO value, SafeHtmlBuilder 
      sb)
        {
           sb.appendHtmlConstant(value.getName());
        }

Solution

  • A Sencha GXT grid uses a ListStore. To show new data inside the grid, the store needs to be updated. This can be done by calling store.addAll(theNewDataList). Keep in mind to clear the store before adding new values.

    Updating the store will force the grid to redraw. During the redraw, the render-method of the ButtonCell will be called. To change the layout of the button, implement inside the render-method what you want to do.

    This is an example from the Sencha GXT Explorer (https://examples.sencha.com/gxt/examples/#ExamplePlace:grid_aggregationgrid):

          final NumberFormat numberFormat = NumberFormat.getFormat("0.00");
          changeColumn.setCell(new PropertyDisplayCell<Double>(new DoublePropertyEditor(numberFormat)) {
            @Override
            public void render(com.google.gwt.cell.client.Cell.Context context, Double value, SafeHtmlBuilder sb) {
              String style = value < 0 ? "red" : "green";
              sb.appendHtmlConstant("<span style='color:" + style + "'>");
              super.render(context, value, sb);
              sb.appendHtmlConstant("</span>");
            }
          });
    

    In case the value is less than 0 it will render a red string otherwise a green one.

    Hope that helps.