Re: Adding Objects to an ArrayList (Checkboxes)
Re: Adding Objects to an ArrayList (Checkboxes)
- Subject: Re: Adding Objects to an ArrayList (Checkboxes)
- From: David LeBer <email@hidden>
- Date: Fri, 7 Oct 2005 18:08:31 -0400
Repeat after me Janice... "Let WebObjects do the heavy lifting" :-)
First: I don't recommend adding public ivars to your EO's to track
state. That's mucks with MVC separation.
Second: In general you'll find most WebO apps use the Foundation
classes ie: NSArray, NSMutableArray instead of standard Java data
structures. Amongst other things, they respond to KVC which can be
very helpful.
Third: To answer your question. I'm assuming you have a repetition on
the page with a checkbox that you hope will be associated with each
item. When you click on a performSomeActionWithTheSelectedObjects()
action some magic should happen with the items that have their
selection checked. Correct?
If so, try building the array like this:
protected NSMutableArray _selectedObjects;
public MyObjectInTheRepetition item;
// accessors
public NSMutableArray selectedObjects() {
// lazy initialization, alternately initialize the array in the
component
// constructor.
if (_selectedObjects == null) {
_selectedObjects = new NSMutableArray();
}
return _selectedObjects;
}
public void setSelectedObjects(NSMutableArray newArray) {
_setSelectedObjects = newArray;
}
// now the magic, bind the checked binding of your WOCheckbox
// to objectSelected the accessors are below, notice there is *no*
// ivar. The value of objectSelected is dynamic.
// when checking to see if the checkbox should display as checked if the
// selectedObjects array contains the current lines item, return true;
public boolean objectSelected() {
return selectedObjects().containsObject(item);
}
// when reading the values of the checkbox, add or remove the item where
// appropriate
public boolean setObjectSelected(boolean value) {
if (value) {
if (! selectedObjects().containsObject(item)) {
selectedObjects().addObject(item);
}
} else {
if (selectedObjects().containsObject(item)) {
slectedObjects().removeObject(item);
}
}
}
Now you have an array of the selectedObjects. Do with them what you
will (assign them to the next page, whatever)
Hope that helps.
On 7-Oct-05, at 5:14 PM, Janice Cheung wrote:
Ken,
I call addServer() when I click on an "EmailServerContacts"
button, to bring all the servers clicked to the next page.
I will also have it to set the ArrayList for downloadable
reports (a collection of server data for all server checkboxes
clicked).
Basically the nextPage contains all the server contact
information for all the servers clicked (basically for Server
Broadcast emails).
Here is my EmailServerContacts() function (I just wanted to
confirm that serverIsCheckedList was retrieving data):
public WOComponent EmailServerContacts(){
EmailServerContacts nextPage=(EmailServerContacts)
pageWithName("EmailServerContacts");
addServer();
return nextPage;
}
Prior to the checkbox functionality request, my
EmailServerContacts() function basically just set one Server object
for the next page.
public WOComponent EmailServerContacts(){
EmailServerContacts nextPage=(EmailServerContacts)
pageWithName("EmailServerContacts");
nextPage.setServerGroup(serverDisplayGroup);
nextPage.setServer(server);
return nextPage;
}
I do not quite know how I'll extract the server attributes from
the ArrayList yet ... (probably set an array to the arraylist?)
but I figured I would think about that after making sure my
"serverIsChecked" ArrayList contained some data.
What do you think I am doing wrong?
Thanks for responding to me.. I really appreciate it.
Best regards,
Janice
Ken Anderson wrote:
Janice,
What are you trying to do exactly? I understand you have a page,
and it seems like you have a list of servers on the page with
checkboxes next to them. Is that correct?
Is addServer an action? At what point are you trying to get the
list of checked servers?
Ken
On Oct 7, 2005, at 4:52 PM, Janice Cheung wrote:
Greetings,
I am using an ArrayList to add server items for all servers
that have a boolean isChecked value equal to true.
I am using a WORepetition to display a list of "m_Servers",
with "server" object items.
I added the following to my Server class:
public boolean isChecked;
public boolean isChecked(){
return isChecked;
}
I use a fetch specification to populate the WORepetition with
an "m_Servers" list binding and a "server" item binding.
Repetition1:WORepetition{
item=server;
list=m_Servers;
}
CheckBox1: WOCheckBox{
checked=server.isChecked;
}
I am aiming to provide the functionality where, when a Server
item in the WORepetition is checked, it is added to
a ArrayList named "serverIsCheckedList". This ArrayList will
enable me to retrieve all the server attributes for
all servers checked. However, my ArrayList is showing null
and no items are being added to my ArrayList. What
am I doing wrong?
This is my addServer function:
public void addServer(){
for (Iterator it=m_Servers().iterator();it.hasNext();)
{
Server s=(Server)it.next();
if((s.isChecked==true))
{
serverIsCheckedList.add(s);
NSLog.out.appendln("=========here is my
serverIsCheckedList=========="+serverIsCheckedList);
}
}
}
What am I doing incorrectly? Am I iterating through the
m_Servers array properly? No servers are being added to my
serverIsCheckedList and no items are being printed out from
my NSLog.out.appendln.
I would really appreciate any advice or guidance. I am
tearing my hair out. Thanks for helping me.
Best regards,
Janice
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40anderhome.com
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40codeferous.com
This email sent to email@hidden
--
;david
--
David LeBer
"I am codeferous!"
Codeferous Software
site: http://www.codeferous.com
blog: http://david.codeferous.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden