Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Can I Maniuplate This Field via JavaScript?

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a field on my form named "labExceptionBean[0].markedForDeletion". Is there any way I can manipulate that field?

The reason for the odd naming is due to the fact that I'm using indexed fields in Struts and that's the naming convention used. I don't get to pick the name - it's picked for me.

My problem is that, if I try to do this:



It tries to index the array labExceptionBean, which isn't an array at all. Of course, this subsequently fails.

What I'm trying to accomplish is to make a simple "select all" button for a series of checkboxes on my form. I'll have some variable number of checkboxes on the form, but they'll be named as such:

labExceptionBean[0].markedForDeletion
labExceptionBean[1].markedForDeletion
labExceptionBean[2].markedForDeletion
labExceptionBean[3].markedForDeletion
...

Any ideas? I seem to be fresh out.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why it does not work is because it sees it was more items down the document tree. What you need to do is use the form/element array with the name.

Basic idea:



Here is the basic idea I tested it with:


Hope that helps
Eric
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome. Muchas gracias, senor. (That's Spanish for Many gracias. )

Here's my final function:



Works like a charm. Thanks, Eric.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note - I put the goofy periods in the "EVAL" statements above because the Ranch doesn't like the eval term in posts.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a way that I would approach it:



Reasons why I would do it this way, eval tends to lag down large loops. Down fall to this method above is if you have tons of form elements, it has to loop through them all, which can also be slow.

Eric
 
Manuel Sanchez Iba�ez
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i just have the same problem with Struts. Thanks of this post, i have found one solution but now i have a new problem.

I have a Form with one <html:select> and severals fields <html:radio> associated. When i click in one radio field, the list of values change in a select field (others values).

Contenido : <html:select property="contenido" size="1"></html:select>

<html:radio property="tipocontenido" value="conindata" onclic="changeContenidosEjercicio(this.value);"/>Conducta

<html:radio property="tipocontenido" value="gesindata" onclic="changeContenidosEjercicio(this.value);"/>Gesto

I can realize this operation fine with this javascript function (Previosly i have loaded in various arrays (Arrayconindata,Arraygesindata) the diferent values:
------------------------------------------------------
function changeContenidosEjercicio(control){
contenidosArray = new Array();
contenidosEjercicio = document.forms['ejercicioForm'].contenido;
contenidosEjercicio.options.length=0;

if (control == 'conindata') {contenidosArray = Arrayconindata;}
if (control == 'gesindata') {contenidosArray = Arraygesindata;}

for (i=0; i < contenidosArray.length;i++)
contenidosEjercicio.options[i] = contenidosArray[i];
}
-------------------------------------------------------------


Now, i have to recover several beans and show it in a jsp with their values stored. For realize this, i have used indexed="true" in a fields and the javascript function have been modified a litle (adding a index):

<logic:iterate id="controles" name="listaejercicios" indexId="contador">

Contenido : <html:select name="controles" property="contenido" indexed="true" size="1"></html:select>

<html:radio name="controles" property="tipocontenido" value="conindata" indexed="true" onclic="changeContenidosControl(this.value,this.name);"/>Conducta

<html:radio name="controles" property="tipocontenido" value="gesindata" indexed="true" onclic="changeContenidosControl(this.value,this.name);"/>Gesto I
.....


----------------------------------
function changeContenidosControl(control,name){
var contenidosArray = new Array();
var ind = name.substring( name.indexOf('[') +1 ,name.lastIndexOf(']') );
var id = parseInt(ind,10);

var contenidosControl = document.forms['controldinamicoForm'].elements["controles["+ id +"].contenido"];
contenidosControl.options.length = 0;

if (control == 'conindata') {contenidosArray = Arrayconindata;}
if (control == 'gesindata') {contenidosArray = Arraygesindata;}

for (i=0; i < contenidosArray.length;i++)
contenidosControl.options[i] = contenidosArray[i];

}


If i show various records (beans) in a jsp, if i only ckecked one radio button works fine, but if i ckecked the same radio button of other record (bean) throws an error: "the object don�t accept this property or method"

Sorry for my english. Any idea?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic