HELP - "foreach" command! |
Author: |
Message: |
whiz
Senior Member
Posts: 568 Reputation: 8
– / – /
Joined: Nov 2008
|
O.P. HELP - "foreach" command!
Is there a way of doing something for every item in an array? I thought there was something called "foreach" that does this... then again, I don't really know.
For example:
js code: var Test = new Array("test1", "test2", "test3");
var TestDesc = new Array("This is Test 1.", "This is Test 2.", "This is Test 3.");
var TestWnd = MsgPlus.CreateWnd("Windows.xml", "WndTest", 0);
foreach (Test)
{
TestWnd.LstView_AddItem("LstTest", Test);
TestWnd.LstView_SetItemText("LstTest", 0, 1, TestDesc)
}
This post was edited on 05-22-2009 at 04:18 PM by whiz.
|
|
05-21-2009 06:36 PM |
|
|
SmokingCookie
Senior Member
Posts: 815 Reputation: 15
30 / /
Joined: Jul 2007
|
RE: HELP - "foreach" command!
You mean something like:
JScript code: for(var i = 0; i < MyArray.length; i++) {
<code_goes_here>
}
I may recommend using a different solution than using 2 arrays and hoping they will remain synchronised:
JScript code: /* CLASS Item([string] ID: the ID you stored in the Test array,
[string] Desc: the description you stored in the TestDesc array);*/
var Item = function(ID,Desc) {
return {
"ID": ID,
"Desc": Desc
}
// Array of Item instances.
var MyArray = [
new Item(
"test 1",
"This is test 1"),
new Item(
"test 2",
"This is test 2"),
new Item(
"test N",
"This is test N")
]
// Assume window is loaded and is assigned to WndTest;
for(var i = 0; i < MyArray.length; i++) {
with(WndTest) {
LstView_AddItem("ListViewID",MyArray[i].ID);
LstView_SetItemText("ListViewID",i,1,MyArray[i].Desc);
}
}
Note that this is untested code.
This post was edited on 05-21-2009 at 07:38 PM by SmokingCookie.
|
|
05-21-2009 07:35 PM |
|
|
Spunky
Former Super Mod
Posts: 3658 Reputation: 61
36 / /
Joined: Aug 2006
|
RE: HELP - "foreach" command!
js code: for (var s in test){
Debug.Trace(test[s]);
}
EDIT: foreach is used in PHP
This post was edited on 05-21-2009 at 07:39 PM by Spunky.
<Eljay> "Problems encountered: shit blew up"
|
|
05-21-2009 07:38 PM |
|
|
SmokingCookie
Senior Member
Posts: 815 Reputation: 15
30 / /
Joined: Jul 2007
|
RE: HELP - "foreach" command!
[offtopic] LOL 3-minute intervals [/offtopic]
for...in is also a solution, but more used for Objects.
EDIT::
Looking at Whiz' code again, adding an Array to a list-view won;t work too well.
This post was edited on 05-21-2009 at 07:43 PM by SmokingCookie.
|
|
05-21-2009 07:41 PM |
|
|
whiz
Senior Member
Posts: 568 Reputation: 8
– / – /
Joined: Nov 2008
|
O.P. RE: HELP - "foreach" command!
Umm... I can't seem to get that to work - I just get an error saying "Expected }".
Basically, I have a main (editor) window, like this:
js code: var WndWriterEditor = MsgPlus.CreateWnd("Windows.xml", "WndWriterEditor", 0);
Let's say, the user creates a new item with the AddItem window:
js code: var WndWriterAddItem = MsgPlus.CreateWnd("Windows.xml", "WndWriterAddItem", 0);
The form includes an ID, title, description. The user enters the following information:
ID = "Test", Title = "My Little Test", and Description = "This is a little test of mine...".
js code: var WndID = WndWriterAddItem.GetControlText("EdtID");
var WndTitle = WndWriterAddItem.GetControlText("EdtTitle");
var WndDesc = WndWriterAddItem.GetControlText("EdtDesc");
I need that to be added to a ListViewControl, like:
js code: WndWriterEditor.LstView_AddItem("LstWindows", WndID);
WndWriterEditor.LstView_SetItemText("LstWindows", 0, 1, WndTitle);
WndWriterEditor.LstView_SetItemText("LstWindows", 0, 2, WndDesc);
But it needs to work out how many items have been made, so it can add data to the right ones, and it needs to be done with every filled-in form. I also need it so that they can be edited and deleted. I know that you can check if a particular ListViewControl entry is selected:
js code: WndWriterEditor.LstView_GetSelectedState("LstWindows", 0);
But can you get it so it checks the entire list, and returns the identifier of the selected item, rather than true or false for one entry?
|
|
05-21-2009 08:01 PM |
|
|
andrewdodd13
Senior Member
Oh so retro
Posts: 870 Reputation: 16
34 / /
Joined: Jan 2005
|
RE: RE: HELP - "foreach" command!
quote: Originally posted by Spunky
js code: for (var s in test){
Debug.Trace(test[s]);
}
EDIT: foreach is used in PHP
And several other languages, it's just not called foreach. [Java has for(Item : Collection), etc.]
Jscript code: for (var enumerator = new Enumerator(myCollection) ; !enumerator.atEnd(); enumerator.moveNext()) {
// code
}
quote: for...in is also a solution, but more used for Objects.
This does not work in JScript. In JScript for ... in is used to enumerate the *properties* of an object, whereas in VBScript it is used to enumerate members of a collection. Ref
|
|
05-21-2009 08:01 PM |
|
|
whiz
Senior Member
Posts: 568 Reputation: 8
– / – /
Joined: Nov 2008
|
O.P. RE: HELP - "foreach" command!
Actually, I think I get that bit now.
But I still have 2 questions:
1) I know that you can check if a particular ListViewControl entry is selected, using "LstView_GetSelectedState()", but can you get it so it checks the entire list, and returns the identifier of the selected item, rather than true or false for one entry? Or would that have to use the "for (s in test)" thing as well?
2) How can you add another item to an array, without having to use "variable[n]"? Can you do something like "variable += 'value'"?
|
|
05-21-2009 08:17 PM |
|
|
SmokingCookie
Senior Member
Posts: 815 Reputation: 15
30 / /
Joined: Jul 2007
|
RE: HELP - "foreach" command!
Question 2: myArray.push(ItemVar); whereas ItemVar can be anything (even an array)
Question 1: No, there is not a method to retrieve the current selection. You can, however, do this:
JScript code: var SelectedItem = -1;
function OnWndTestEvent_LstViewClicked(PlusWnd,ControlId,ItemIdx) {
if(ControlId == "vLstTest") {
SelectedItem = ItemIdx;
<do_other_stuff>
}
}
Make sure though that you catch all selection-related events and set SelectedItem accordingly. They are:
- OnWindowIdEvent_LstViewClicked();
- OnWindowIdEvent_LstViewDblClicked();
- OnWindowIdEvent_LstViewRightClicked();
A value of -1 means that no item is currently selected. That means, for example, there is only one item in the list-view control, and you click below that item.
Also, make sure to set SelectedItem back to -1 when the window is destroyed.
|
|
05-22-2009 07:11 AM |
|
|
whiz
Senior Member
Posts: 568 Reputation: 8
– / – /
Joined: Nov 2008
|
O.P. RE: HELP - "foreach" command!
js code: var WndID = new Array();
var WndTitle = new Array();
var WndDesc = new Array();
function OnWndWriterAddItemEvent_CtrlClicked(objWnd, strControlId)
{
switch (strControlId)
{
case "BtnAdd":
WndID.push(objWnd.GetControlText("EdtID");
WndTitle.push(objWnd.GetControlText("EdtTitle");
WndDesc.push(objWnd.GetControlText("EdtDesc");
break;
}
}
Will that work?
|
|
05-22-2009 12:49 PM |
|
|
SmokingCookie
Senior Member
Posts: 815 Reputation: 15
30 / /
Joined: Jul 2007
|
RE: HELP - "foreach" command!
Short answer: yes.
|
|
05-22-2009 12:54 PM |
|
|
Pages: (2):
« First
[ 1 ]
2
»
Last »
|
|