0

Assuming that i have the following data in my params

params:[input:[1, 2, 3]]

And i have the following form in my Grails app

<div class="block1">   
    <label class="control-label">
        <g:message code="input.label" default="Input"/>
    </label>
    <div class="controls">
        <g:textField id="input1" name="input" value="${input}" readonly="${actionName != 'show' ? false : true}"/>
    </div>
</div>
<div class="block2">   
    <label class="control-label">
        <g:message code="input.label" default="Input"/>
    </label>
    <div class="controls">
        <g:textField id="input2" name="input" value="${input}" readonly="${actionName != 'show' ? false : true}"/>
    </div>
</div>
<div class="block3">   
    <label class="control-label">
        <g:message code="input.label" default="Input"/>
    </label>
    <div class="controls">
        <g:textField id="input3" name="input" value="${input}" readonly="${actionName != 'show' ? false : true}"/>
    </div>
</div>

The form design above is correct, because in my form design, there will be several inputs of the same name (but each will be saved to the database under different primary keys) and it can be increases and decreases according to user selection.

Few questions using the above

  • How do i make it so that the value for input1 is params.input[0], input2 is params.input[1] and input3 is params.input[2] in the view? I can pass the model from controller without problem, but i couldn't distribute the value properly to each input on the form.
  • Is there any way to change the value ${input} dynamically? As in if i want to change the value to ${input[0]} or ${input[1]}
  • Can i automatically set the amount of block appended into the form using the g:each tag? Say like if from controller i want to set the rendering block amount to 3, so can i use the g:each tag to render the block 3 times in the form?

Thanks

Fred A
  • 1,602
  • 1
  • 24
  • 41
  • Hi Fred, Sorry I hope I have understood your request. I think as it is now your current variable input is an arraylist since you have defined it 3 times (just like it would be if it was a checkbox list) so in your first question is actually what it should be by default? unsure what you mean by 2nd question since it is down to you what you wish to assign where so your value could be ${input[0]} where you want it to be that.. 3rd question yes why not for each input then create the same element textField give it an id based on counter – V H Nov 18 '14 at 14:56
  • Hi Vahid. For first question yes, those repeatable inputs are an arraylist. As for my 2nd question, its about editing the grails tag dynamically. Because i can put ${input[0]} in my grails input tag and it would grab the value of input[0] from my controller, but once the web is up, there is no way to modify the ${input[0]} to say like ${inputThis[0]} dynamically. And using jquery's attr('value', '${inputThis[0]}') wouldn't help. – Fred A Nov 18 '14 at 23:46
  • Hi Fred, presume when you say inputThis you mean some other array value all together... You could still possibly do something like this using jquery/ajax get call to get something upon a trigger onclick or onload of page or possibly by adding something like angularjs to your default grails app. For jquery/ajax take a look at some of the samples in this plugin..https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/views/autoComplete/_filterWord.gsp https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/views/autoComplete/_filterField.gsp – V H Nov 19 '14 at 09:39
  • I don't quite get what you tried to say.. ${inputThis} is basically the same as ${input}, just different name. What i want to ask was how to modify the gsp tag attribute dynamically. Like for example, gsp style text input is but in web it will look just like a normal input tag . I cannot change the source of where the g:textField would get its value from when its running on the web. Anyway i will try to look into the link that you provide and see for any hints that could help me. Thanks – Fred A Nov 19 '14 at 14:17
  • sorry too long for comment added it as an answer - hope it is of use/help – V H Nov 19 '14 at 15:03
  • I was actually thinking maybe what you are trying achieve I have already done all the hard work in another plugin..https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListUploader/_mailerUploaderDisplay.gsp this is being triggered from https://github.com/vahidhedayati/mailinglist/blob/master/grails-app/views/mailingListEmail/contactclients.gsp which is an array that appears in checkboxes, the plugin uses the modaldynamix plugin to generate popup forms that user adds new values to ? if this is what you want reuse the code get rid of fromplugin= in taglib call.. – V H Nov 19 '14 at 19:19

1 Answers1

1

The links are examples of how to use ajax/jquery to get values from a remote call and replace html element (divId) within a page - this divId could be the entire

<input type="text" name="input" value="newvalue"/>  

upon triggering some form of call as above to get the new value.. in regards to

g:textField

  • yes it works like all other grails tags in the end they are transformed back to the correct HTML terminology...

The actual variable value is dynamic if you defined

<input name="existingvariable" value="${something}" ...

where something was a parameter from the given controller - and then you updated the call so

://YOURHOST:8080/yourapp/controller?existingvariable=newvalue

and refreshed or clicked this link which is what ajax would be doing for you doing a new call to either another controller to generate new values or same and passing new value to it and then grabbing data and pushing it back onto the divId ... (all in the background)

Groovy loading into divs Grails - Select menu not rendering I want my selects dropdowns to auto populate with Ajax in Grails website

The above are all related to using ajax to populate / update existing form elements

If you wish to update a live form with a new live value (non existant in DB) take a look at modaldynamix plugin. //github.com/vahidhedayati/modaldynamix

Community
  • 1
  • 1
V H
  • 8,382
  • 2
  • 28
  • 48