0

Please help with Listbox and data binding

asked 2012-09-06 03:34:06 +0800

czynga gravatar image czynga
171

I have a Listbox with two columns. There is a Label in the first column and a Timebox in the second column. I'm using ListModelList<Day> as my data model and I'm displaying its content like this:

<listitem self="@{each='day'}">
     <listcell label="@{day.date}"></listcell>
     <timebox value="@{day.time}"></timebox>
</listitem>

Then in response to some user interaction I have to change the "date" fields programatically and I'm doing it like this:

for (int i = 0; i < model.getSize(); i++) {
     model.getElementAt(i).updateDate(newDate);
     newDate.add(GregorianCalendar.DAY_OF_YEAR, +1);
}

My data is changed properly but the problem is that Label's value in the first column is not updated/refreshed immediately. It's only after I change the Timebox value via GUI when Label in the same row is refreshed. Is that normal behaviour? And how can I fix my code so that the Labels are refreshed immediately after I change the data they bind with?

As a workaround I'm using this extra line of code:

model.set(i, model.getElementAt(i));

And it looks like it's working i.e. the Label is updated but somehow it doesn't feel right to me to do it that way. Please correct me if I'm wrong.

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2012-09-06 05:09:24 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

Hi I think in timebox you call onchange attribute or some other attribute(you have to check i am not confirm abut timebox attributes) which will call a java method and use NotifyChanage("LableNameHere") it will change the value of label also when you are changing timebox

link publish delete flag offensive edit

answered 2012-09-06 17:50:08 +0800

czynga gravatar image czynga
171

But what if the Label and Bandbox are not related. They describe the same object (Listbox row) but are changing independently.

Can I somehow inform data model manager that I've changed the data and it needs to refresh some component?

link publish delete flag offensive edit

answered 2012-09-07 04:49:04 +0800

sjoshi gravatar image sjoshi flag of India
3493 1 8
http://zkframeworkhint.bl...

Yes i previously told you to check if onSelect and any other attribute is in <timebox> then you can define like this.

<timebox onChange="@command('updatetextBox')">

and in your Java Class do like this

@NotifyChange("listModelObj")
@Command(updatetextBox)
public void updateTextBox(){
//Get value here and change the Label value and again set Model it will automatically change value
}

link publish delete flag offensive edit

answered 2012-09-08 13:50:40 +0800

czynga gravatar image czynga
171

Sorry I think we are having some communication problem here :) In my example the Timebox works absolutely fine, to make it easier we can even assume that it's not there, lets say it's only labels in my Listbox.
The only problem I have is with a Label. That Label is bonded with some data. Initially when the page loads it shows data. But when I programmatically change data then the Label's value is not updated on the screen. Maybe I could try to do something like label.setValue() but then where is the point of data binding.

link publish delete flag offensive edit

answered 2012-09-08 15:07:42 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

updated 2012-09-08 15:16:24 +0800

<listitem self="@{each='day'}" value="@{day}">
 
    <listcell label="@{day.date}"></listcell>
   <listcell>
       <timebox value="@{day.time}"></timebox>
   </listcell>

</listitem>


Normally you have to change the model. Than call binder.loadAll();

binder.loadAll() ==> Load all value from data beans to UI components.

binderSaveAll() ==> Save all values from UI components to beans.

Seems that there is something wrong with your architecture. The code that you are posted is too small to find the reason.

best
Stephan

link publish delete flag offensive edit

answered 2012-09-08 16:18:02 +0800

czynga gravatar image czynga
171

Stephan here is the full code.
View:

<zk xmlns="http://www.zkoss.org/2005/zul">

    <window title="Data Binding in MVC" border="normal"
            apply="MyComposer, org.zkoss.zkplus.databind.AnnotateDataBindingComposer">

        <button id="changeButton" label="Change data"></button>

        <listbox model="@{$composer.dataModel}">
            <listhead>
                <listheader>Name</listheader>
            </listhead>
            <listitem self="@{each='person'}">
                <listcell>
                    <label value="@{person.name}"></label>
                </listcell>
            </listitem>
        </listbox>

    </window>

</zk>

Controller:

public final class MyComposer extends SelectorComposer<Component> {

    @Wire
    private Button changeButton;

    private ListModelList<Person> dataModel = new ListModelList<Person>();


    public ListModelList<Person> getDataModel() {
        return dataModel;
    }


    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        for (int i = 0; i < 10; i++) {
            dataModel.add(new Person());
        }
    }


    @Listen("onClick = #changeButton")
    public void onButtonClick() {
        for (int i = 0; i < dataModel.getSize(); i++) {
            dataModel.getElementAt(i).changeName();
        }
        Clients.showNotification("Data has been changed");
    }
}

And model:

public class Person {

    private String name;


    public String getName() {
        return name;
    }


    public void setName(String newName) {
        name = newName;
    }


    public Person() {
        name = "Initial Value";
    }


    public void changeName() {
        name = "Changed Value";
    }
}

I realize it's my fault i.e. I'm missing something but I don't have a clue what it is.

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: 2012-09-06 03:34:06 +0800

Seen: 145 times

Last updated: Sep 08 '12

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