You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Puneet Lakhina <pu...@gmail.com> on 2006/09/21 11:43:08 UTC

Indexed Properties. Maintaining Order

Hi,
I have a no. of fields in a form arranged in a table of rows. I am using
indexed properties for these fields. The number of these fields isn't known
to me at the time of page loading for which I have used some DHTML to
properly add the fields. And all the values are getting submitted.
Now my problem is that the order is getting jumbled up.

My table is like this

No.           Date               Description
1.              date[0]                description[0]
2.               date[1]                description[1]

But after submitting, the order gets jumbled up. i.e. i get something like
this

No.           Date               Description
1.              date[0]                description[1]
2.               date[1]                description[0]

This I have figured is because of the following setter methods

public void setDate(int index,String value) {
datesList.add(value);
}

public void setDescription(int index,String value) {
descriptionsList.add(value);
}

So basically the order in which the setter methods are called is random. i.e.
setDate(0,value) - >  setDescription(1,value)  and so on.
Does this problem mean we have no way of maintaining order when using lists?


What I have thought of is instead of using Indexed Properties I will use
map backed properties. But if somebody can offer me some sort of solution
that doesn't require change from Lists to Map it would be great.


-- 
Puneet

Re: Indexed Properties. Maintaining Order

Posted by Puneet Lakhina <pu...@gmail.com>.
On 9/21/06, Niall Pemberton <ni...@gmail.com> wrote:
>
> As you found out there is no way of knowing the order the
> browser/client will submit request parameters, so if you want to use
> indexed properties in this way you need to "grow" the list to
> accomodate the size of the indexed property being set.
>
> So you could do something like the following:
>
>   public void setDate(int index,String value) {
>       while (datesList.size() <= index) {
>           datesList.add(null);
>        }
>       datesList.set(index, value);
>   }


I  kinda did this before posting as a temp fix. But I though this was like a
too inefficinet solution. But Im glad it isnt all that bad a thing :-).

Thanks a lot.
-- 
Puneet

Re: Indexed Properties. Maintaining Order

Posted by Niall Pemberton <ni...@gmail.com>.
As you found out there is no way of knowing the order the
browser/client will submit request parameters, so if you want to use
indexed properties in this way you need to "grow" the list to
accomodate the size of the indexed property being set.

So you could do something like the following:

  public void setDate(int index,String value) {
      while (datesList.size() <= index) {
          datesList.add(null);
       }
      datesList.set(index, value);
  }

This is what LazyDynaBean / LazyDynaForm does:

http://struts.apache.org/1.x/userGuide/building_controller.html#lazy_action_form_classes

Theres also this page on the wiki:
http://wiki.apache.org/struts/StrutsCatalogLazyList

Niall

On 9/21/06, Puneet Lakhina <pu...@gmail.com> wrote:
> Hi,
> I have a no. of fields in a form arranged in a table of rows. I am using
> indexed properties for these fields. The number of these fields isn't known
> to me at the time of page loading for which I have used some DHTML to
> properly add the fields. And all the values are getting submitted.
> Now my problem is that the order is getting jumbled up.
>
> My table is like this
>
> No.           Date               Description
> 1.              date[0]                description[0]
> 2.               date[1]                description[1]
>
> But after submitting, the order gets jumbled up. i.e. i get something like
> this
>
> No.           Date               Description
> 1.              date[0]                description[1]
> 2.               date[1]                description[0]
>
> This I have figured is because of the following setter methods
>
> public void setDate(int index,String value) {
> datesList.add(value);
> }
>
> public void setDescription(int index,String value) {
> descriptionsList.add(value);
> }
>
> So basically the order in which the setter methods are called is random. i.e.
> setDate(0,value) - >  setDescription(1,value)  and so on.
> Does this problem mean we have no way of maintaining order when using lists?
>
>
> What I have thought of is instead of using Indexed Properties I will use
> map backed properties. But if somebody can offer me some sort of solution
> that doesn't require change from Lists to Map it would be great.
>
>
> --
> Puneet
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Indexed Properties. Maintaining Order

Posted by Puneet Lakhina <pu...@gmail.com>.
On 9/21/06, Strachan, Paul <Pa...@det.nsw.edu.au> wrote:
>
> A List is an ordered collection so it doesnt make sense that the order
> changes.  Also, depending on which Map implementation you use ordering may
> not be guaranteed.


I would basically have the index in case of indexed properties as my key. SO
the question of ordering doest come
publiv void setDate(String key, String value) {
map.put(key,value);
}
This way I would always have the correct values.

I frequently use indexed properties with List and have not experienced this
> problem.  You may need to check the generated html source and follow through
> the request cycle.
>
> Your method signatures for your setters dont seem appropriate in the
> context of managing indexed properties on a list of objects (at least to
> me).


Could you please give an example of how you manage lists with indexed
properties, that would really help.

Thanks
-- 
Puneet

RE: Indexed Properties. Maintaining Order

Posted by "Strachan, Paul" <Pa...@det.nsw.edu.au>.
A List is an ordered collection so it doesnt make sense that the order changes.  Also, depending on which Map implementation you use ordering may not be guaranteed.  I frequently use indexed properties with List and have not experienced this problem.  You may need to check the generated html source and follow through the request cycle.
 
Your method signatures for your setters dont seem appropriate in the context of managing indexed properties on a list of objects (at least to me).  Perhaps you are managing your indexed properties in a way that would better suit a Map implementation, but I cant tell from the provided code.

________________________________

From: Puneet Lakhina [mailto:puneet.lakhina@gmail.com]
Sent: Thu 21/09/2006 7:43 PM
To: Struts Users Mailing List
Subject: Indexed Properties. Maintaining Order



Hi,
I have a no. of fields in a form arranged in a table of rows. I am using
indexed properties for these fields. The number of these fields isn't known
to me at the time of page loading for which I have used some DHTML to
properly add the fields. And all the values are getting submitted.
Now my problem is that the order is getting jumbled up.

My table is like this

No.           Date               Description
1.              date[0]                description[0]
2.               date[1]                description[1]

But after submitting, the order gets jumbled up. i.e. i get something like
this

No.           Date               Description
1.              date[0]                description[1]
2.               date[1]                description[0]

This I have figured is because of the following setter methods

public void setDate(int index,String value) {
datesList.add(value);
}

public void setDescription(int index,String value) {
descriptionsList.add(value);
}

So basically the order in which the setter methods are called is random. i.e.
setDate(0,value) - >  setDescription(1,value)  and so on.
Does this problem mean we have no way of maintaining order when using lists?


What I have thought of is instead of using Indexed Properties I will use
map backed properties. But if somebody can offer me some sort of solution
that doesn't require change from Lists to Map it would be great.


--
Puneet


**********************************************************************
This message is intended for the addressee named and may contain
privileged information or confidential information or both. If you
are not the intended recipient please delete it and notify the sender.
**********************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org