-
FEATURED COMPONENTS
First time here? Check out the FAQ!
In the following example, I have grid with three rows. A ListModelList binds a List<String> to the grid. There is a simple row renderer that uses a textbox in each grid cell, allowing the user to edit the contents of the cells. I expected the binding mechanism to work both ways and update the list from the user's edits but it doesn't seem to be working. There is a button that will display the contents of the grid data model. If you run this example and edit the cells and select the display button it will display the original contents of the list and overwrite your edits with the original values. I would greatly appreciate any help with this.
<?page title="Test Live Data Grid" contentType="text/html;charset=UTF-8"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?> <zk> <window id="win"> <vbox> <zscript> // make the list model list = new ArrayList(); for( String s : new String[]{"a","b","c"} ) list.add(s); listModel = new org.zkoss.zul.ListModelList( list, true ); // make the row renderer class SimpleRowRenderer implements RowRenderer { public void render( Row row, Object data ) throws Exception { new Textbox( (String) data ).setParent( row ); } } rowrenderer = new SimpleRowRenderer(); </zscript> <grid id="grid" model="@{listModel}" rowRenderer="${rowrenderer}" /> <label id="msg" value=" " /> <button label="Show model values"> <attribute name="onClick"> binder.loadComponent( grid ); msg.setValue( grid.getModel().get(0) + " " ); msg.setValue( msg.getValue() + grid.getModel().get(1) + " " ); msg.setValue( msg.getValue() + grid.getModel().get(2) ); </attribute> </button> </vbox> </window> </zk>
This is such a lively and active forum, I had expected to get some help, suggestions, or other response to this problem by now and with so many new messages I fear this topic will be lost in the crowd. If I have not explained the problem well enough or asked for help in the right way, please let me know so I can make corrections.
Sreed, although this forum is active, but community guys still have work/tasks to do and busy on it.
So, please be patience if no one responses the discussion in short days.
for your case, you should use some template-like mechanism of databinding (not renderer, renderer is for livedata)
please read this related smalltalk :
http://docs.zkoss.org/wiki/Multiple_Field_Sorting_on_Listbox
http://docs.zkoss.org/wiki/ListModel_and_Databinding_Enhanced_Combobox
sreed, a runnable example for you. :)
<?page title="Test Live Data Grid" contentType="text/html;charset=UTF-8"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?> <zk> <window id="win"> <vbox> <zscript><![CDATA[//@DECLARATION final class Bean{ public String name; public Bean(String name){this.name = name;} } ]]> <![CDATA[// ArrayList list = new ArrayList(); for( Bean s : new Bean[]{new Bean("a"),new Bean("b"),new Bean("c")} ){ list.add(s); } ]]></zscript> <grid id="grid" model="@{list}"> <rows> <row self="@{each=mybean}"> <textbox value="@{mybean.name}" /> </row> </rows> </grid> <label id="msg" value=" " /> <button label="Show model values"> <attribute name="onClick"> String val = list.get(0).name+","+list.get(1).name+","+list.get(2).name; msg.setValue(val); </attribute> </button> </vbox> </window> </zk>
Thank you for getting back to me so quickly, Dennis. I understand that you are very busy and appreciate the time you spend here helping everyone.
I didn't know how to produce a short example to demonstrate my problem with real live data so I just used an array of literals. However, in my actual program I am getting the data from a database not an array of literals. Maybe I don't know what you mean by live data.
The problem I am seeing is that when using live data (i.e. with AnnotateDataBinderInit) and the user changes the data then the listModel is not updated. Is there an example where live data can be modified by both the user and the server?
@Dennis:
From your example it looks like if I want to be able to have the user edit the data I can't use the live data ListModel and will have to update the list in my own code if the data changes on the server. Is that correct?
Dennis: Your example works well for user edits but doesn't update the display for programmatic edits (i.e. if the database changes or a new database record is to be displayed). Please see this example:
<?page title="Test Live Data Grid" contentType="text/html;charset=UTF-8"?> <?init class="org.zkoss.zkplus.databind.AnnotateDataBinderInit" ?> <zk> <window id="win"> <vbox> <zscript><![CDATA[//@DECLARATION final class Bean{ public String name; public Bean(String name){this.name = name;} } ]]> <![CDATA[// // make the list model ArrayList list = new ArrayList(); for( Bean s : new Bean[]{new Bean("a"),new Bean("b"),new Bean("c")} ) list.add(s); ]]> </zscript> <grid id="grid" model="@{list}"> <rows> <row self="@{each=mybean}"> <textbox value="@{mybean.name}" /> </row> </rows> </grid> <label id="msg" value=" " /> <button label="Show model values"> <attribute name="onClick"> String val = list.get(0).name+","+ list.get(1).name+","+ list.get(2).name; msg.setValue(val); </attribute> </button> <button label="Programmatically change values"> <attribute name="onClick"> list.get(0).name = list.get(0).name + "X"; </attribute> </button> </vbox> </window> </zk>
Ah! I get it. We just need to add "binder.loadComponent( grid );" and all is well. Thanks for your help!!!!
<button label="Programmatically change values"> <attribute name="onClick"> list.get(0).name = list.get(0).name + "X"; binder.loadComponent( grid ); </attribute> </button>
Asked: 2009-03-20 01:23:45 +0800
Seen: 363 times
Last updated: Mar 25 '09