0

Listbox - using template - how can I get the previous "row" so can use in logic for current row????

asked 2012-10-24 14:26:29 +0800

rickcr gravatar image rickcr
704 7

updated 2012-10-24 14:27:53 +0800

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>

delete flag offensive retag edit

19 Replies

Sort by ยป oldest newest

answered 2012-10-24 16:41:31 +0800

rickcr gravatar image rickcr
704 7

updated 2012-10-24 16:41:54 +0800

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?

link publish delete flag offensive edit

answered 2012-10-26 20:09:34 +0800

rickcr gravatar image rickcr
704 7

Anyone? A way to get the previous row when using a template?

link publish delete flag offensive edit

answered 2012-10-29 05:39:08 +0800

Senthilchettyin gravatar image Senthilchettyin flag of India
2625 3 8
http://emrpms.blogspot.in...

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

link publish delete flag offensive edit

answered 2012-10-29 13:43:26 +0800

rickcr gravatar image rickcr
704 7

@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.)

link publish delete flag offensive edit

answered 2012-11-22 10:36:00 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

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

link publish delete flag offensive edit

answered 2012-11-22 14:34:11 +0800

rickcr gravatar image rickcr
704 7

@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.)

link publish delete flag offensive edit

answered 2012-11-22 14:54:49 +0800

rickcr gravatar image rickcr
704 7

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

link publish delete flag offensive edit

answered 2012-11-22 17:11:44 +0800

rdgrimes gravatar image rdgrimes
735 7

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.

link publish delete flag offensive edit

answered 2012-11-26 05:44:21 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

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

link publish delete flag offensive edit

answered 2012-11-27 16:24:19 +0800

rdgrimes gravatar image rdgrimes
735 7

updated 2012-11-27 18:48:14 +0800

@benbai:

That's very slick. Glad you posted it. I'm sure that will come in handy for me as well.

Ron

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-10-24 14:26:29 +0800

Seen: 309 times

Last updated: Dec 12 '12

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