%OnAfterCreatePage/Adding Values to a List or ComboBox

181 views
Skip to first unread message

Mike

unread,
Dec 25, 2007, 7:49:10 PM12/25/07
to InterSystems: Zen Community
Anyone know how to populate a List or ComboBox while in the
%OnAfterCreatePage method?

I've been trying various ways with the following but am unsucessful.
d %page.%SetValueById("country",Name)

Thanks,
Mike

Jill Singer

unread,
Dec 26, 2007, 10:33:09 AM12/26/07
to InterSys...@googlegroups.com
Why don't you use javascript - the onloadHandler?

for example:

/// Fired when the page is loaded
Method onloadHandler() [ Language = javascript ]
{
var country = zenPage.getComponentById("country");
country.setValue("USA");
}

-Jill

Mike

unread,
Dec 26, 2007, 11:39:15 AM12/26/07
to InterSystems: Zen Community
I'll use whatever works. How do I call your method from the Xdata
block?
Also, is that "setValue" adding to the list? I want to create a list
of countries but the list is coming from a Cache database object.

Thanks.

On Dec 26, 10:33 am, "Jill Singer" <Jill.Sin...@intersystems.com>
wrote:
> Mike- Hide quoted text -
>
> - Show quoted text -

Jill Singer

unread,
Dec 26, 2007, 11:48:27 AM12/26/07
to InterSys...@googlegroups.com
Mike-

The onloadHandler is just automatically called whenever the page is loaded.

I misread your initial post- if you are using a datacombo which gets values from a db, there is not a way to programmatically add to it-

From using zen:

A <dataCombo> is a specialized type of <combobox> that presents the user with a list of options obtained at runtime via an SQL query. Unlike <combobox>, <dataCombo> does not allow you to specify <option> elements or to set styles for individual options in the list. This is because <dataCombo> uses a query to obtain a dynamic list of options. There are no statically defined options in a <dataCombo>, so there are no <option> elements.

Therefore, what you may want to do is use a normal combo box, and manually get the information out of the database and then add to it; if it's a list then by adding to both the displayList and the valueList;
If it's a combo box, then by making additional option elements.

See using zen- the Lists chapter for reference.

The:

country.setValue("USA");

is just shorthand for: country.setProperty('value', 'USA');

Mike

unread,
Dec 26, 2007, 7:16:12 PM12/26/07
to InterSystems: Zen Community
Trying the following but still getting an error. I checked the
documentation but don't see how to do this.
Appreciate the help!

Method %OnAfterCreatePage() As %Status
{
Try {
s x="" f s x=$O(^WMA.CountryI("NameIndex",x)) q:x="" d
. s y=$o(^WMA.CountryI("NameIndex",x,"")) d
.. s id=##class(WMA.Country).%OpenId(y)
.. s Name=id.Name
.. d %page.%SetValueById.setValue("country",Name)
}
Catch(ex) {
&js<alert('Error in Server method:
\n#($ZCVT(ex.DisplayString(),"O","JS"))#');>
}
Quit $$$OK
}

On Dec 26, 11:48 am, "Jill Singer" <Jill.Sin...@intersystems.com>
> > - Show quoted text -- Hide quoted text -

Randy Stewart

unread,
Dec 26, 2007, 10:03:47 PM12/26/07
to InterSys...@googlegroups.com
Mike,

I think I see the issue.

Instead of

.. d %page.%SetValueById.setValue("country",Name)

Try
.. d %page.%SetValueById("country",Name)

%SetValueById is the Method itself. It does not contain an additional setValue method.


Also, since you are on the server side in an instance method this should work, too:
.. d ..%SetValueById("country",Name)


Randy L Stewart
Sales Engineer
InterSystems Corporation

Randy....@intersystems.com
214-551-5781

Anne

unread,
Dec 27, 2007, 3:49:05 AM12/27/07
to InterSystems: Zen Community
Hi Mike,

You have loop there, are you there giving a value to a component or
updating the valuelist?

If you update the value that's ok told before, but in that case I
don't understand your loop (I use to do this way):

set comp=..%GetComponentById("country")
set comp.value=Name

But if you want to update the valuelist, then you should collect the
list in your loop:
set valuelist=valuelist_","_Name

and after you have list, you should update it to component:
set comp=..%GetComponentById("country")
set comp.valuelist=$extract(valuelist,2,1E9)
-Anne-

Mike

unread,
Dec 27, 2007, 9:45:50 AM12/27/07
to InterSystems: Zen Community
Thanks from everyone. I now am not getting an error but also nothing
in the listbox.
When I change my "country" field" to another edit field I see my data
there (for both methods including the commented line) but it does not
appear in the listbox.


<listBox id="country" label="country">
<option value="" text="" />
<option value="1" text="Apple" />
<option value="2" text="Banana" />
<option value="3" text="Carnitas" />
</listBox>

Method %OnAfterCreatePage() As %Status
{
Try {
s (str,x)="" f s x=$O(^WMA.CountryI("NameIndex",x)) q:x="" d
. s y=$o(^WMA.CountryI("NameIndex",x,"")) d
.. s id=##class(WMA.Country).%OpenId(y)
.. s Name=id.Name,str=str_$s(str]"":",",1:"")_Name
s item=%page.%GetComponentById("country")
s item.value=str
;d %page.%SetValueById("country",str)
}
Catch(ex) {
&js<alert('Error in Server method:
\n#($ZCVT(ex.DisplayString(),"O","JS"))#');>
}
Quit $$$OK
}

Jill R. Singer

unread,
Dec 27, 2007, 10:44:00 AM12/27/07
to InterSystems: Zen Community
mike- is the below the actual code (or at least the same structure)
that is on your page?

On Dec 27, 9:45 am, Mike <m...@adsc.com> wrote:
>
> <listBox id="country" label="country">
> <option value="" text="" />
> <option value="1" text="Apple" />
> <option value="2" text="Banana" />
> <option value="3" text="Carnitas" />
> </listBox>
>

if it is, then all you need to do is add to both the display and value
lists, else - if you are using a datacombo, it is more complicated.

could you please send us the complete, whole page (or at least both
the xdata content and all relevant methods) that you are using?

thanks

-Jill


Randy Stewart

unread,
Dec 27, 2007, 12:56:20 PM12/27/07
to InterSys...@googlegroups.com
Mike,

Earlier I failed to realize you were working with a listBox rather than just a text field. Below are two examples that should allow you to add listBox values in either the %OnAfterCreatePage() method or in the onloadHandler() method depending on which you would rather use. The examples assume a listBox defined in the XData like this:

<listBox id="country" label="country" onchange="zenPage.notifyOnChange(zenThis);" listWidth="240px"/>

I won't worry about your loop to get the values for the box because that looks fine. I will just put a comment as a placeholder for your loop. But when you get to the point where you want to add to the list box here is how it goes:

Method %OnAfterCreatePage() As %Status
{
s tOptionCount=0
s tListBox=%page.%GetComponentById("country")

// Add a blank option at the top if you want it
s tOption=##class(%ZEN.Auxiliary.option).%New()
s tOption.value=""
s tOption.text=""
d %page.%AddChild(tOption)
d tListBox.options.Insert(tOption)

// Your loop to get values
// For each value
s tOptionCount=$increment(tOptionCount)
s tOption=##class(%ZEN.Auxiliary.option).%New()
s tOption.value=tOptionCount
s tOption.text=Name
d %page.%AddChild(tOption)
d tListBox.options.Insert(tOption)
// End loop
}

You could also use the onloadHandler method. The code is simpler but then you would have to figure out how to get your values from the server.

Method onloadHandler() [ Language = javascript ]
{

var c=zenPage.getComponentById('country');
c.appendOption('',''); // blank option
c.appendOption(1,'CountryOne');
c.appendOption(2,'CountryTwo');
}

I included the notifyOnChange() for testing

Method notifyOnChange(comp) [ Language = javascript ]
{
alert(comp.value+' '+comp.text);
}


Since you are retrieving the option values from the server it looks like the %OnAfterCreatePage method is the way to go here.

I hope I was more thorough in handling your issue this time.


Best regards!

Randy L Stewart
Sales Engineer
InterSystems Corporation

Randy....@intersystems.com
214-551-5781

Mike

unread,
Dec 27, 2007, 7:46:08 PM12/27/07
to InterSystems: Zen Community
It now works. Thanks to all again.
I've changed the list box to a combo box. Some questions.

Why is it "listBox" but "combobox"? Capital "B" for list but not
combo.

When it loads and also when I click on the drop down it takes about 4
seconds (even 2nd and subsequent times).
Am I doing something the "slow" way? It was also slow when I used the
listBox.
I have 6 routine buffers, 50 8k buffers on WinVista, 2GB RAM.

Also, any "better" way to $order the object index?

Is the solution I was given and am using in the documentation? I
could not find it.

Method %OnAfterCreatePage() As %Status
{
Try {
s tListBox=%page.%GetComponentById("country")
s tOption=##class(%ZEN.Auxiliary.option).%New()
s tOption.value=""
s tOption.text=""
d %page.%AddChild(tOption)
d tListBox.options.Insert(tOption)

s x="" f s x=$O(^WMA.CountryI("NameIndex",x)) q:x="" d
. s y=$o(^WMA.CountryI("NameIndex",x,"")) d
.. s id=##class(WMA.Country).%OpenId(y)
.. s Name=id.Name
.. s tOption=##class(%ZEN.Auxiliary.option).%New()
.. s tOption.value=y
.. s tOption.text=Name
.. d %page.%AddChild(tOption)
.. d tListBox.options.Insert(tOption)

}
Catch(ex) {
&js<alert('Error in Server method:
\n#($ZCVT(ex.DisplayString(),"O","JS"))#');>
}
Quit $$$OK
}

On Dec 27, 12:56 pm, "Randy Stewart" <Randy.Stew...@intersystems.com>
wrote:
> Randy.Stew...@intersystems.com
> > - Show quoted text -- Hide quoted text -

Randy Stewart

unread,
Dec 27, 2007, 10:17:14 PM12/27/07
to InterSys...@googlegroups.com
I can answer the question about documentation. There was not a direct example that I could find. The example sent was created based on a couple of sources and past experience. The two most helpful things I found in this case were:

1) The examples in Zen Application Programming, one under the heading "Zen Properties on Client and Server" and the other under the heading of "Defining Page Contents Programmatically". These examples showed how to work with %page objects on the server side, including the need to create new objects and add them to the %page.

2) Reading the code of the %ZEN.Component.listBox. It was in the %SetDefaultValues method that I discovered the options.Insert method. It also showed adding new objects to the %page.

With a little experimentation these were cobbled together into a working example.

The Zen documentation is very good. Every once in a while an examples is found to be needed that was not thought of at first. I will make the proper request to get this one added for future reference.

Thanks.

Randy L Stewart
Sales Engineer
InterSystems Corporation

Randy....@intersystems.com

Reply all
Reply to author
Forward
0 new messages