Loading foreach using core methods in ZK MVVM

1.1k views Asked by At

I would like to read some values from my vm. Here's an example of what I've done so far

<div forEach="${vm.activityData.activityList}">
    <label
        value="@load(c:cat3('vm.activitiData.', each, '.organizer' ))" />
</div>

But it seems like it is unable to read the each inside the cat3. My objective is to get the organizer for each activity. Any idea how to achieve this ?


Update

For example, I want to simplify from this codes :

<div>
    <label value="@load(vm.activityData.A.name)" />
    <label value="@load(vm.activityData.ASponsor)" />
    <label value="@load(vm.activityData.AOrganizer)" />
    <separator/>
    <label value="@load(vm.activityData.B.name)" />
    <label value="@load(vm.activityData.BSponsor)" />
    <label value="@load(vm.activityData.BOrganizer)" />
    <separator/>
</div>

into

<div forEach="${vm.activityData.activityList}">
    <label value="@load(c:cat3('vm.activityData.', each, '.name'))" />
    <label value="@load(c:cat3('vm.activityData.', each, 'Sponsor'))" />
    <label value="@load(c:cat3('vm.activityData.', each, 'Organizer'))" />
    <separator/>
</div>

Basically what I want to do is to dynamically combine part of strings into one string. And then use @load(string).

1

There are 1 answers

2
chillworld On BEST ANSWER

Today I had the time to test it and I prepared a fiddle for you.
Your ZK version is good but I did forget to add one more thing.
You have to make use of the custom-attributes otherwise it wouldn't work.

Solution :

<div forEach="${vm.activityData.activityList}">
    <custom-attributes field="${each}"/>
    <label value="@load(vm.activityData[field].name)" />
    <label value="@load(vm.activityData[c:cat(field, 'Sponsor')]" />
    <label value="@load(vm.activityData[c:cat(field, 'Organizer')]" />
    <separator/>
</div>

I created fast a working fiddle where you can test or look how it works.