0

Live data not workign with user edits

asked 2009-03-20 01:23:45 +0800

sreed gravatar image sreed
195 1 3 5

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>

delete flag offensive retag edit

7 Replies

Sort by ยป oldest newest

answered 2009-03-23 02:29:22 +0800

sreed gravatar image sreed
195 1 3 5

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.

link publish delete flag offensive edit

answered 2009-03-23 03:54:16 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2009-03-23 04:19:40 +0800

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

link publish delete flag offensive edit

answered 2009-03-23 04:12:09 +0800

dennis gravatar image dennis
3679 1 6
http://www.javaworld.com....

updated 2009-03-23 04:14:27 +0800

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>

link publish delete flag offensive edit

answered 2009-03-23 04:23:19 +0800

sreed gravatar image sreed
195 1 3 5

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?

link publish delete flag offensive edit

answered 2009-03-23 15:45:35 +0800

sreed gravatar image sreed
195 1 3 5

@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?

link publish delete flag offensive edit

answered 2009-03-25 20:36:41 +0800

sreed gravatar image sreed
195 1 3 5

updated 2009-03-25 20:38:52 +0800

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>

link publish delete flag offensive edit

answered 2009-03-25 20:41:49 +0800

sreed gravatar image sreed
195 1 3 5

updated 2009-03-25 20:44:13 +0800

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>

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-03-20 01:23:45 +0800

Seen: 363 times

Last updated: Mar 25 '09

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