-
FEATURED COMPONENTS
First time here? Check out the FAQ!
I know how to these kinds of things in old-skool JSP with EL, but having difficulty finding examples of this with ZK.
I'm using a template, as described in most of the current examples, but since I don't see iterations and I'm not sure how to integrate some kind of "c:set " concept between iterations, I'm not sure how I go about setting the previous value in scope so I can make decisions on how to display the next iteration? For example rather than see a list like:
Mammal Squirrel Mammal Fox Mammal Raccoon Bird Hawk Bird Sparrow
I want to hide the first column display if it was previously set so that it looks like:
Mammal Squirrel Fox Raccoon Bird Hawk Sparrow
But how do I do this within the following type of code:
<listbox model="@bind(vm.animals)" selectedItem="@bind(vm.selectedAnimal)" vflex="true"> <template name="model" var="animal"> <listitem> <!--- SOMETHING LIKE THIS??? How do I set "previous" animal ?? --> <listcell label="@bind(animal.class)" visible=${previous.class ne animal.class}></listcell> <listcell label="@bind(animal.family)"></listcell> </listitem> </template> </listbox>
I thought I could do something like this to get a handle to the previous item, but I think from the docs it mentions you lose scope of the previous item when using a template? Or maybe that's only when nesting templates? Regardless, still stuck. I guess I can look into how to do this with a forEach loop. But what I tried to see if I can get a handle to the previous item:
<listbox model="@bind(vm.animals)" selectedItem="@bind(vm.selectedAnimal)" vflex="true"> <template name="model"> <listitem> <listcell label="${forEachStatus.index} - ${forEachStatus.previous.each.class}"></listcell> <listcell label="@bind(each.class)" > <listcell label="@bind(each.family)"></listcell> </listitem> </template> </listbox>
In the above index will show up, but I can't get a handle to the previous item?
I am not sure whether you are looking something like below
http://www.zkoss.org/zkdemo/grid/spreadsheet_functionalities
http://www.zkoss.org/zkdemo/grid/grouping_model
http://www.zkoss.org/zkdemo/grid/grouping
@Senthilchettyin,
Thanks. I saw that group box example, but it's a bit overkill for what I need at the moment. (The UI space I have is limited so I don't want a header group box but I still need the 'first' label to show up.)
Hi rickcr,
You can try handle the current index and get the value from model directly,
e.g., http://zkfiddle.org/sample/4m1u8i/2-Get-previous-value-in-template-with-MVVM-VM-way
Regards,
Ben
@Benbai. Very clever.
Although, sort of annoying nothing built in to handle it. It would be nice if Zk provided a hook "beforeIteration" and "afterIteration" that would allow you to set something in the VM as it iterated using a Template.
It's sort of expensive in java to look up a value by index from a list. I think I might try to tweak it some using a ListIterator (assuming using a List) since it has a previous() method. (Not sure if 'under the hood' it's any more efficient or not then looking up by index.)
@Benbai...
Actually I did it using a regular Iterator. This should be pretty efficient (it's just a straightforward iteration vs having to do a lookup.) Thanks for geting me on the right path!
http://zkfiddle.org/sample/23m3med/5-Get-previous-value-in-template-with-MVVM-VM-way2#source-2
Note, I didn't wrap Animals in a ListModel. I'm still sort of new and could look this up.. but what's the advantage in doing that?
This may not be ideal, but I had a similar situation where I am showing batches by location. I only wanted to show the location when the location changed. So, I just spun off the ArrayList into a second ArrayList, from which I remove the redundant location names. This second ArrayList then is used to populate the listbox model. Not ideal, and it does mean you'll have to disable sorting on that column, but it's an alternative in lieu of having the type of syntax you're looking for.
Hi rickcr,
Regarding ListModel, nope, there is not any advantage, but listbox require a model to work. The List<Animal> will be wrapped by a model automatically as needed, see AbstractListModelConverter.java#coerceToUi
(https://github.com/zkoss/zk/blob/master/zkbind/src/org/zkoss/bind/converter/sys/AbstractListModelConverter.java#L62)
Regarding get previous component (value), the implementation of Template is not rely on any specific component
(refer to https://github.com/zkoss/zk/blob/master/zk/src/org/zkoss/zk/ui/impl/UiEngineImpl.java#L2134), and I think it is better to do it within component side instead of VM, you can also try the component way:
http://zkfiddle.org/sample/2r4031g/1-Get-previous-value-in-template-with-MVVM-Component-way
Regards,
Ben
@benbai:
That's very slick. Glad you posted it. I'm sure that will come in handy for me as well.
Ron
Asked: 2012-10-24 14:26:29 +0800
Seen: 310 times
Last updated: Dec 12 '12