PDA

View Full Version : retaining choosen value in child window


rama
07-12-2002, 03:32 PM
hi,

I have an array in javascript(rejectmessage used in the below code.).On click of a hyperlink i am opening a window and using the values in array i am painting some records.In the opened window there will be some records with a radio button against each record.When the user ckicks any radio ,ie he has choosen some value and when he closes the window i am assigining the value to a hidden variable.But when the user again clicks the same hyperlink again i have to retain the value he has choosen earlier and show it to him in the window.There will be 3 hyper links with same records,each representing a message type

The following code is used to paint the records in the window.So the user can click any hyper link and choose a radio button.But when he closes and comes again the value should be shown .



agbrwin=window.open("","RejectMessage","toolbar=no,status=yes,maximize=no,me
nubar=no,scrollbars=yes,width=500,height=500");
agbrwin.document.clear();
var messageHtml="<HTML><BODY onLoad='window.focus()'>\n";
messageHtml+="<form name='rejectmess'><table><tr><td>Choose Reject Message</td><
/tr></table><br><table><tr><td>\n";
for(j=0;j<rejectmessage.length;j++)
{

messageHtml+="<input type=radio name=agbrmessage "+"value='"+mess
agecode[j]+"'>"+rejectmessage[j]+"<br>";

}
messageHtml+="</td></tr></table><br><br><table width=100%><tr><td align='center'
><input type='button' value=OK onClick=opener.checkmessage(window);window.close()><input type='button' value=Cancel onClick=javascript:window.close()></td></tr>
</table></FORM></body></HTML>\n";
agbrwin.document.writeln(messageHtml);
agbrwin.document.close();



How do i achieve this using javascript.

Thanks
Rama

Soldier Bob
07-12-2002, 05:29 PM
you can use

agbrwin.LAST_DEFAULT_FOR_RADIO_BUTTON = value_set_by_check_message;

the LAST_DEFAULT_FOR_RADIO_BUTTON will be created as a global for that window.
but I believe you still need to do a:

var LAST_DEFAULT_FOR_RADIO_BUTTON;

in the html of the popup, so that you have this variable to write to on window creation.
for that matter you might be able to do a

i=1;
agbrwin.document.rejectmess.agbrmessage[i].checked = "1";

do note that you need to place the correct selected index in the [ ] or this code will not work. I used 'i' for the example.

-S. Bob

ShriekForth
07-12-2002, 07:02 PM
I did something similar a few months back. I handled it sort of like this, I can't find the one I built...

<script language="JavaScript" type="text/javascript">

var messagecode = new Array('11','22','33');
var rejectmessage = new Array('delete','copy','move');

function selectagbrwin(){
agbrwin=window.open("","RejectMessage","toolbar=no,status=yes,maximize=no,menubar=no,scrollbars=yes,width=500,height=500");
agbrwin.document.clear();
var messageHtml="<HTML><BODY onLoad='window.focus(); selectID()'>\n";
messageHtml+="<form name='rejectmess'><table><tr><td>Choose Reject Message</td></tr></table><br><table><tr><td>\n";
for(j=0;j<rejectmessage.length;j++){
messageHtml+="<input type=radio name=agbrmessage "+"value='"+messagecode[j]+"' onChange=setID('"+j+"')>"+rejectmessage[j]+"<br>";
}
messageHtml+="</td></tr></table>\n<br><br><table width=100%><tr><td align='center'><input type='button' value=OK onClick='getDelete();' >\n";
messageHtml+="<input type='button' value=Cancel onClick='javascript: self.close()'></td></tr></table></FORM></body></HTML>\n";
// new window script to handle the selection
messageHtml+="\<script\> \ntempID ="+document.temp.hiddenIndex.value + " \n "
// function to get the index, and value of the radio selections and save them in the parent window
messageHtml+="function getDelete(){\n opener.document.temp.hiddenValue.value = document.rejectmess.agbrmessage[tempID].value;\n ";
messageHtml+=" opener.document.temp.hiddenIndex.value = tempID; \n self.close();\n} \n";
//used to gather the selected radio rather than loop through to find the checked radio button
messageHtml+="function setID(id){tempID = id;}\n"
// default the last selection upon entering the window.
messageHtml+="function selectID(){ document.rejectmess.agbrmessage["+document.temp.hiddenIndex.value+"].checked = true; } \<\/script\>";
agbrwin.document.writeln(messageHtml);
agbrwin.document.close();
}
</script>

<form name=temp>
<input type="hidden" name="hiddenValue" value="22">
<input type="hidden" name="hiddenIndex" value="0">
<a href="javascript:selectagbrwin()";>Select One</a>
</form>
It will hold the value of the checkbox, as well as the selected index to pre-select the value. I guess if you get right down to it, you only really need the selected index. Depends on what you are doing with the result I guess.

ShriekForth

rama
07-13-2002, 04:14 PM
Hi,


The code works fine,But one thing is that if i repalce the input type radio to check box it is not eorking.I will also show the code.If there is any mistake kindly point out


function depprocess(){
processwin=window.open("","DependentProcess"," toolbar=no,status=yes,maximize=n
o,menubar=no,scrollbars=yes,width=400,height=400");
processwin.document.clear();
var processHtml="<HTML><LINK REL='stylesheet' TYPE='text/css' HREF='/cd/param/j
s/style.css'><BODY onLoad='window.focus(); selectID()'>\n";
processHtml+="<form name='dependentprocess'><table><tr class=ez1><td class=labe
lText>Choose Dependent Process</td></tr></table><br><table><tr class=ez1><td cla
ss=labelText>\n";
for(j=0;j<process.length;j++){
processHtml+="<input type=checkbox name=depproc "+"value='"+processkey[j]+"' on
blur=setID('"+j+"')>"+process[j]+"<br>";
}
processHtml+="</td></tr></table>\n<br><br><table width=100%><tr><td align='cent
er'><input type='button' value=OK onClick='getDelete();' >\n";
processHtml+="<input type='button' value=Cancel onClick='javascript:self.close(
)'></td></tr></table></FORM></body></HTML>\n";
// new window script to handle the selection
processHtml+="\<script\> \ntempID ="+document.forms[0].hiddenIndexdep.va
lue + " \n "
// function to get the index, and value of the radio selections and save them in
the parent window
processHtml+="function getDelete(){\n opener.document.forms[0].depproc.value = d
ocument.dependentprocess.depproc[tempID].value;\n ";
processHtml+=" opener.document.forms[0].hiddenIndexdep.value = tempID; \n self.
close();\n} \n";
//used to gather the selected radio rather than loop through to find the checked
radio button
processHtml+="function setID(id){tempID = id;}\n"
// default the last selection upon entering the window.
processHtml+="function selectID() { document.dependentprocess.depproc["+documen
t.forms[0].hiddenIndexdep.value+"].checked= true; } \<\/script\>";
processwin.document.writeln(processHtml);
processwin.document.close();
}


Thanks

RamaNote: BY Default no radio button or check box should be checked.,when the user clicks the hyperlink.If he wants he can choose.It is not mandatory


Rama

rama
07-15-2002, 02:56 PM
Hi,

I am not able to solve the problem ,can anybody look into it


Thanks

Rama

ShriekForth
07-16-2002, 12:58 AM
Checkboxes make this a little more difficult problem. As a checkbox type the user can select more than one.
You would then need to store all selections, (in an array, comma delimited in the field and split back out, or one item in the opener for each one in the pop-up). The reference to the objects will be slightly different as well. You would need a pair of loops, one at the begining to iterate through the items in the pop-up to see what was selected befre, and another to interate through all the checkboxes available to save all the checked boxes back to the opener page.

<script language="JavaScript" type="text/javascript">

var messagecode = new Array('11','22','33');
var rejectmessage = new Array('delete','copy','move');

function selectagbrwin(){
agbrwin=window.open("","RejectMessage"," toolbar=no,status=yes,maximize=no,menubar=no,scrollbars=yes,width=500,height=500");
agbrwin.document.clear();
var messageHtml="<HTML><BODY onLoad='window.focus(); )'>\n";
messageHtml+="<form name='rejectmess'><table><tr><td>Choose Reject Message</td></tr></table><br><table><tr><td>\n";
for(j=0;j<rejectmessage.length;j++){
messageHtml+="<input type=checkbox name=agbrmessage "+"value='"+messagecode[j]+"' >"+rejectmessage[j]+"<br>\n";
}
messageHtml+="</td></tr></table>\n<br><br><table width=100%><tr><td align='center'><input type='button' value=OK onClick='getDelete();' >\n";
messageHtml+="<input type='button' value=Cancel onClick='javascript: self.close()'></td></tr></table></FORM></body></HTML>\n";
// new window script to handle the selection
messageHtml+="\<script\> \ntempID ="+document.temp.hiddenIndex.value + " \n "
// function to get the index, and value of the radio selections and save them in the parent window
messageHtml+="function getDelete(){\ntempStr = \"\"\; \n for(j=0\; j< " + rejectmessage.length + "\;j++){\n if(document.rejectmess.agbrmessage[j].checked){\ntempStr += \j+\",\"\;\n}\n}";
messageHtml+=" opener.document.temp.hiddenIndex.value = tempStr; \n self.close();\n} \n";
//used to gather the selected radio rather than loop through to find the checked radio button
messageHtml+="function setID(id){tempID = id;}\n"
// default the last selection upon entering the window.
messageHtml+="function selectID(){ document.rejectmess.agbrmessage["+document.temp.hiddenIndex.value+"].checked = true; } \<\/script\>";
agbrwin.document.writeln(messageHtml);
agbrwin.document.close();
}
</script>
</head>
<body>
<form name=temp>
<input type="hidden" name="hiddenValue" value="22">
<input type="hidden" name="hiddenIndex" value="0">
<a href="javascript:selectagbrwin()";>Select One</a>
</form>
That will save which checkboxes are selected, you could easily modify it to be the value if that is more useful to you. From there put the setID back in place another loop for selectedID to reset all the values passed from the opener.

ShriekForth

learner
07-16-2002, 11:45 AM
Hi,,

The problem is when more than one check box is clicked the value is passed as 0,1,



That extra comma which is put is creating syntax error.How to remove the comma


Thanks

Rama

ShriekForth
07-16-2002, 04:39 PM
You can have the pop-up remove it, or the opener remove it. There are a couple of ways I can think of doing it. Using split to pull all the commas out leave you an array of items,

selections = document.temp.hiddenIndex.value.split(",")

this would leave you with an array that has one item extra in it left from the last ',' so be aware of that when working with it.

or truncate the string with substring

newString = document.temp.hiddenIndex.value.substring(0,document.temp.hiddenIndex.length -2);


ShriekForth

learner
07-16-2002, 04:50 PM
Hi ,


Can u tell me where exactly i have to change in the above code that u have given.



Thanks

ShriekForth
07-16-2002, 04:55 PM
where are you going to need the information? I would assume in the opener window where you are saving the checks. So add it to what ever function in the opener window is going to be reading those fields, and is going to do something with them. I don't know enough about the page this will be on to know where it will be best used.

ShriekForth

learner
07-18-2002, 04:48 PM
Hi,

I tried to seperate the values by comma, ,but i was not able to pass it to the function as index ie 0,1,so that both the values will be checked if the user selects 2 values close s and comes back.


Can u modify in the above code ,if possible



Thanks

ShriekForth
07-19-2002, 09:36 PM
A little tricky with the checkboxes, more work anyway.


<html>
<head></head>
<script language="JavaScript" type="text/javascript">
function splitMe(){
str = "";
myStr = document.temp.hiddenIndex.value;
tempArray = myStr.split(',')
tempStr = "";
if (tempArray.length > 1){ // if we only have one value, don't bother parsing it
for (i=0;i<tempArray.length; i++){
tempStr += "'"+tempArray[i]+"',";
}
if (tempStr.charAt(tempStr.length -1) == ','){ // have a string, but still a , on the end. Remove it here.
short = tempStr.length - 1;
str = tempStr.substring(0,short)
}
else{
str = myStr
}
}
else{ // only one value, put it in the format we need in the array on the pop-up
str = "\""+tempArray[0]+"\"";
}
return str;
}

var messagecode = new Array('11','22','33');
var rejectmessage = new Array('delete','copy','move');

function selectagbrwin(){
agbrwin=window.open("","RejectMessage"," toolbar=no,status=yes,maximize=no,menubar=no,scrollbars=yes,width=500,height=500");
agbrwin.document.clear();
var messageHtml="<HTML><BODY onLoad='window.focus(); )'>\n";
messageHtml+="<form name='rejectmess'><table><tr><td>Choose Reject Message</td></tr></table><br><table><tr><td>\n";
for(j=0;j<rejectmessage.length;j++){
messageHtml+="<input type=checkbox name=agbrmessage "+"value='"+messagecode[j]+"' >"+rejectmessage[j]+"<br>\n";
}
messageHtml+="</td></tr></table>\n<br><br><table width=100%><tr><td align='center'><input type='button' value=OK onClick='getDelete();' >\n";
messageHtml+="<input type='button' value=Cancel onClick='javascript: self.close()'></td></tr></table></FORM></body></HTML>\n";
// new window script to handle the selection
messageHtml+="\<script\> \n "
// function to get the index, and value of the radio selections and save them in the parent window
messageHtml+="function getDelete(){\ntempStr = \"\"\; \n for(j=0\; j< " + rejectmessage.length + "\;j++){\n if(document.rejectmess.agbrmessage[j].checked){\ntempStr += \j+\",\"\;\n}\n}";
messageHtml+=" opener.document.temp.hiddenIndex.value = tempStr; \n self.close();\n} \n";
//used to gather the selected radio rather than loop through to find the checked radio button
messageHtml+="function setID(id){tempID = id;}\n"
// default the last selection upon entering the window.
str= splitMe();
messageHtml+="function selectID(){ \n var args = new Array (" +str + "); \nfor (i\=0; i \< args.length\; i++){ \n document.rejectmess.agbrmessage[args[i]].checked = true\;}\n } \nselectID()\;\n\<\/script\>";
agbrwin.document.writeln(messageHtml);
agbrwin.document.close();

}

</script>
</head>
<body>
<form name=temp>
<input type="hidden" name="hiddenValue" value="22">
<input type="text" name="hiddenIndex" value="0,1,2,">
<a href="javascript:selectagbrwin()";>Select One</a>
<a href="javascript:alert(splitMe());">splitMe(); Test</a>
</form>
<script>
</script>
</body>
</html>


That should do it.

ShriekForth

rama
07-27-2002, 07:33 PM
Hi Shiekh.


It is working fine but the problem is when i click on the hyperlink 3 values are choosed by default.

Is it possible to leave it blank and only when the user chooses a value i have to take that value and submit the form.Since it is not a mandotary field ,the user may leave without giving any values.



Thnx
Rama

ShriekForth
07-29-2002, 03:02 PM
Leave hiddenValue, and hiddenIndex values blank.

<input type="hidden" name="hiddenValue" value="">
<input type="text" name="hiddenIndex" value="">

ShriekForth