Revision history [back]

click to hide/show revision 1
initial version

asked 2011-10-10 16:25:47 +0800

carpolva gravatar image carpolva

How to add or delete a Grid row (or register) without reseting all the Grid components values

Hello guys.

My issue is simple: I have a Grid composed of textboxes, comboboxes, etc., in my screen I have a button for adding new registers (or rows) to this grid but when I add a new register all the Grid components are reset and I lose previously entered values in the components. My question is similar to: http://www.zkoss.org/forum/listComment/9175/1/20/

Here's part of my code for better understanding:

page.zul grid (simplified)

<grid model="@{ejemploBean.allEvents, load-after='btnAdd.onClick'}" width="1600px">
        <columns>
            <column width="40px">Nro.</column>
            <column width="180px">Actividades</column>
            <column width="50px">Lunes</column>
        </columns>
        <rows>
            <row self="@{each='data'}">
                <!-- Nro. -->
                <label value="@{data.counter}"/>

                <!-- Actividades -->
                <combobox  model="@{data.actividades}" onAfterRender="self.setSelectedIndex(0)"/>

                <!-- Lunes -->
                <intbox value="0" constraint="no negative,no empty">
                    <attribute name="onChange">
                    <![CDATA[
                        lblLunes.setValue(ejemploBean.sumarValorColumna(self,"Lunes"));
                        ejemploBean.sumaTotales();
                    ]]>
                    </attribute>
                </intbox>
            </row>
        </rows>
    </grid>

Next is the class EjemploBean.java where the grid's model is the variable named "lista" composed with a normal bean (TablaBean) and is returned in the getAllEvents() method, "bandera" is a boolean (if it's false is because the page is loaded first time, so, don't create again the list when I add new registers, I put this 'cause getAllEvents() method is invoked every time I add a new register because of the load-after='btnAdd.onClick' in my .zul page). In the onClick$btnAdd I add the new records to the list, and the Grid is reloaded again by the load-after method with the new rows or registers.

public List<TablaBean> getAllEvents() {
        if(!bandera){
            for(int i=1; i<=4; i++){
                TablaBean myBean = new TablaBean();
                myBean.setCounter(i);
                lista.add(myBean);

                for(int j=0; j<7; j++){
                    observaciones.add("");
                }
            }
        }
        bandera = true;

        return lista;
    }

This is onClick of the Add Button:

public void onClick$btnAdd() {
        int size = 0;
        Integer cantidad = iboxRegistros.getValue();

        for(int i=0; i<cantidad; i++){
            size = lista.size();

            TablaBean myBean = new TablaBean();
            myBean.setCounter(size+1);

            lista.add(myBean);
            for(int j=0; j<7; j++){
                observaciones.add("");
            }
        }
    }

I'll appreciate any help, maybe I'll bind each row component value in a TablaBean object and save each one in memory.

Is there a better way to do this?.

Best regards.

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More