Re: Adding Objects to an ArrayList (Checkboxes)
Re: Adding Objects to an ArrayList (Checkboxes)
- Subject: Re: Adding Objects to an ArrayList (Checkboxes)
- From: Ken Anderson <email@hidden>
- Date: Mon, 10 Oct 2005 17:51:49 -0400
Janice,
If all the checkboxes are checked, there must be something wrong.
From looking at David's code, you shouldn't do anything with
selectedObjects, it will be created lazily. Here are a few thoughts:
- Make sure the entire repetition is in the same form as the submit
button. If you have more than one submit button, make sure multiple
submit is set to true.
- Make sure 'objectSelected' is bound to 'checked', and not
something else (a common mistake).
Lastly, in the objectSelected() method, I would put a debug print
statement to make sure that it's being called for each item in your
source list. Then, setObjectSelected should only be called for the
items that were checked.
Ken
On Oct 10, 2005, at 5:04 PM, Janice Cheung wrote:
Ken,
I created two different arrays, but selectedObjects (even with
the checkboxes checked) is always returning an empy
array. I placed the objectSelected checkbox within my source
object array repetition. What am I doing wrong?
Thanks for helping me!
Janice
Ken Anderson wrote:
Janice,
The selected objects and the source objects should be 2 different
arrays. If you don't want any checked, selectedObjects should be
an empty NSMutableArray. Your repetition should be bound to
your original set of server objects.
After the form is submitted, the selectedObjects array should
contain the server objects that the user put a check next to.
Ken
On Oct 10, 2005, at 4:48 PM, Janice Cheung wrote:
Hi David,
I'm having a bit of trouble implementing the "magic" you
provided for me.
On this particular page load, I run a fetch specification to
initialize an array for all Server objects. I set
this array equal to NSMutableArray _selectedObjects, but this
seems to complicate the array of selectedObjects
so that regardless of which checkboxes I check, all Server
objects are always selected.
What am I doing incorrectly? Regardless of how I tweak, all
Server objects are always set as selected (and the
objectSelected is always "true").
Thanks so much for all of your help!
Best regards,
Janice
David LeBer wrote:
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 (Webobjects-
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:
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:
This email sent to email@hidden