Search code examples
javagridrenderingzk

How to clean a Grid on ZK


I am using a grid and a class of java to render the data to the rows, and everything works OK, the data is displayed into the grid, but the problem is when you click for second time the button that render the data to the grid, the data still there and the new data is added in the rows. I'm using Jboss 4.2

This is the RowRender Java Class:

import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;

public class MyRowRenderer implements RowRenderer<Object> {
    @Override
    public void render(final Row row, final java.lang.Object data, int arg2)
            throws Exception {
        String[] ary = (String[]) data;
        for(int i=0;i<ary.length;i++){
            new Label(ary[i]).setParent(row);
        }
    }
}

This is the Part of the Java Class Listener:

public void onClick$generar(Event e) {
        try {
            if(fecha.getValue()==null){
                Messagebox.show("Ingresa una fecha valida MM-yyyy", "Error", Messagebox.OK, Messagebox.ERROR);
            }else{
                SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
                ffecha = formato.format(fecha.getValue()).substring(0, 7);
                String Order = orderby.getSelectedItem().getValue().toString();
                String Modo = modo.getSelectedItem().getValue().toString();
                inboxGrid.setModel(new ListModelList(getUpdatedData(ffecha, Order, Modo)));
            }
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }
    }

This is the GRID in the index.zul

<grid id="inboxGrid" mold="paging" pageSize="15"
                rowRenderer="com.app.reports.MyRowRenderer"
                emptyMessage="Sin Registros!">
                <auxhead>
                    <auxheader colspan="5" class="topic">
                        REPORTE MENSUAL
                    </auxheader>
                </auxhead>
                <columns>
                    <column width="100px" label="CLAVE" align="center" />
                    <column label="SUCURSAL" align="center" />
                    <column width="100px" label="PAGINAS"
                        align="center" />
                    <column width="100px" label="EDO CUENTA"
                        align="center" />
                    <column width="100px" label="IMPRESION"
                        align="center" />
                    <column width="100px" label="MENSAJERIA"
                        align="center" />
                    <column width="100px" label="TOTAL" align="center" />
                </columns>
            </grid>

The first time showed 4 records, thats correct, only 4 records are in the database, the second time that the button "generar" is pressed showed 8 records(4x2), I try to put inboxGrid.getRows().getChildren().clear() on the button; but still showing the same behavior.


Solution

  • It must be within your getUpdatedData(ffecha, Order, Modo)) function. This is nothing to do with ZK.

    Please check the number of items that getUpdatedData(ffecha, Order, Modo)) returns, my guess is that function will return 8 items the second time. Once you set the model ZK will only output the number of items in the list passed to it.