0

ListModelList update property

asked 2021-01-11 22:54:16 +0800

aUser gravatar image aUser
113 4

updated 2021-01-12 19:13:55 +0800

Hello,

I'm trying to use ListModelList in order to avoid NotifyChange.

In my .zul page I have something like this:

<grid model="@load(vm.list) @template('row')">
    <rows>
        <template name="row">
            <row>
                <checkbox checked="@load(each.isModified)" />
            </row>
        </template>
    </rows>
</grid>

And in my bean object i have a utility method that is like this:

public Boolean getIsModified() {
    return "Y".equalsIgnoreCase(this.aString); // aString is a bean property that has getter and setter
}

Being an utility method, isModified is not a bean property, it's just a simple method.

Whenever i change the aString bean property however, the ListModelList doesn't call this utility method to update the state of the checkbox. Is there a way (except using NotifyChange) to make the ListModelList call this utility method when the property aString is changed?

Also, if i have to reload the data of a ListModelList, why can't i use this?

ListModelList<Object> list = new ListModelList<Object>(); // initialization
....
@Command    
public void updateCollection() {
    list = new ListModelList<Object>(collection);
}

Currently i'm using this instead, which is kinda ugly in my opinion..

ListModelList<Object> list = new ListModelList<Object>(); // initialization
....
@Command    
public void updateCollection() {
    list.clear();
    list.addAll(collection);
}

Thank you

EDIT: I solved the first problem using list.notifyChange(item); from java code when i update an item.

delete flag offensive retag edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2021-01-13 11:57:32 +0800

cor3000 gravatar image cor3000
5673 2 7
ZK Team

updated 2021-01-13 12:03:39 +0800

good you found the notifyChange(item) method, that's the dedicated way to rerender a specific row.

A more granular way to reload just the checked property would have been:

BindUtils.postNotifyChange(item, "modified")

even though it's not an instance variable on your bean, it's still a readonly bean-property just by having a getXXX or isXXX method available.

If you read the docs carefully they only mention methods defining a bean property not the actual instance variable.

This is a very useful feature for computed properties for something like fullName or totalPrice

public String getFullname() {
    return this.lastName + ", " + this.firstName;
}

Your second problem is not a problem... clearing a collection and adding items back into it is not ugly. It's perfectly reusing the existing listModelList, in the same way as if you'd just add/remove individual items - can't get simpler than that. And everyone reading your code will instantly know what's your intention.

If you really want to build a new ListModelList you have to notify the viewmodel about the updated property to trigger the load-binding.

@Command
@NotifyChange("list")
public void updateCollection() {
    list = new ListModelList<Object>(collection);
}

or via java

@Command
public void updateCollection() {
    list = new ListModelList<Object>(collection);
    BindUtils.postNotifyChange(this, "list");
}
link publish delete flag offensive edit
0

answered 2021-01-13 21:58:22 +0800

aUser gravatar image aUser
113 4
BindUtils.postNotifyChange(item, "modified");

is definitely better in this case i guess.. In fact it makes no sense to update the whole row if only one element is changed.

Thanks for the hint about the collection. Yeah, i don't really need to create a new ListModelList, i will simply stick to clear and adding the new collection then

link publish delete flag offensive edit
Your answer
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
1 follower

RSS

Stats

Asked: 2021-01-11 22:54:16 +0800

Seen: 6 times

Last updated: Jan 13

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