grails many buttons on one form - grails

I have a form where you can select something from a selector and click and image this then fires to a controller. The problem is currently the images are submit buttons which i can send params with so i dont know which image has been clicked. my current code it as follows:
<g:form controller="profile" action='postFromAlbum'>
<g:select name="child" from="${names}" value="" noSelection="['':'All Children']"/> <br
<g:each in="${pictures}">
<g:submitButton name="submit" class="image3" type="image" src="${it.urlThumb}" alt="no Picture"></g:submitButton>
</g:each>
</g:form>
does anyone know a better way or know of a way to get around this?

One way is to create an analog of actionSubmit that will support params, like in this question.
Another hacky way is to set an invisible text/radio field and set its value in button's onclick.

Related

How can I enter a param for grails-gsp-link Tag in a gsp Page

I have a link via link tag, to a controller where a list function is coded. I want to execute the list function with a selection, e.g. a Year. I would like the user to have the possibility to enter the Year or any other selection criteria on the gsp-page and to route that user input to the params entry in the link tag. Link tag only offers:
<g:link action="list" params="[year: '2018']">myList</g:link>
with 2018 hard coded.
I need it as a user entry.
How can I manage it?
As Mike W suggested, you should use a form for this. (If that is entirely not possible, there are alternatives, but form is the best option.)
To provide a simple example:
<g:form controller="myController">
<label for="year">Year:</label>
<input type="text" id="year" name="year" />
<g:actionSubmit action="list" value="List" />
</g:form>

Capybara/Poltergeist, clicking on Hidden Checkbox?

I have some HTML for a Checkbox im trying to click:
<td class="surface center">
<div class="checkbox-inline checkbox-inline--empty">
<input type="hidden" value="0" name="stuff_check">
<input id="stuff_1" class="boolean optional" type="checkbox" name="stuff_1_checked" value="1" data-item="5">
<label class="optional" for="stuff_1">Checked</label>
</div>
</td>
When running a page.find_by_id('id').trigger('click') it does indeed work, just using click however it complains about Poltergeist possibly clicking another elements:
Capybara::Poltergeist::MouseEventFailed:
Firing a click at co-ordinates [-9468.5, 6] failed. Poltergeist detected another element with CSS selector '' at this position. It may
be overlapping the element you are trying to interact with. If you
don't care about overlapping elements, try using
node.trigger('click').
However I felt maybe this is because of it being set as "hidden", so I tried doing page.find_by_id('ID', :visible => false).click however it gave the same issue.
Any suggestions? Since I know using trigger.('click') isn't advised.
There is no way to do a proper click on a hidden element because there would be no way for a user to click on an element that doesn't appear on the screen.
Your example is confusing because the hidden element doesn't have the same name attribute as the checkbox element which is what I would normally expect in this kind of setup. Assuming that what you're really trying to do is check the "stuff_1" checkbox (and that is hidden via CSS) then you should be doing what a user of your app would have to do - click on the label.
page.find('label[for="stuff1"]').click
Try
within('.checkbox-inline checkbox-inline--empty') do
check('#stuff_1')
end
I really recommend using Pry to do this though as you'll save yourself a ton of pain finding which elements are where.

Rails will_paginate custom renderer manual page number

Hy
What i want to do is to create a custom renderer for will_paginate which renders first, previous, next and last page and a input field where the user can type in the page number manually. I already have the links for first, last etc. but i stuck at the input field. I could create a form in the view but the input field has to be rendered between the previous and next links.
Does anyone know how to do this?
Thanks for your help
You can do this as a separate form (make sure it is a GET). All you
need is the one input element named page. Something as simple as this
should work (not all browsers may like the #). I dropped it into a
site I'm playing with now and it worked. Put it anywhere on your page.
You might have to make something more complicated if you need to
incorporate search terms.
<form action="#" method="get">
Go to page: <input type="text" name="page" value="" size="2"
maxlength="4" />
<input type="submit" name="btnSubmit" />
</form>

How can I make my own check box in MVC?

I would love to use the:
Html.EditorFor(model => #Data.Test.Correct)
To create a checkbox.
However the source of my data is different from the data that needs to be updated. I know this may sound confusing but I get my data from a LINQ select query and then need to update in a different place.
The only way around this seems for me to hand code the HTML for the checkbox. But can someone give me an example of how I do this. For example, how can I code in the setting of checked=true?
You may write HTML codes like below to create a selected or unselected checkbox
<input type="checkbox" name="option1" value="1" checked="checked" />
<input type="checkbox" name="option2" value="2" />
But you will need to write additional code manually to determine which checkbox should be selected, if your checkbox is dynamic generated or being filled in with stored data.

Grails g:Button name coming from params and no form to use

Is there any possibility of having a button which will execute and action in a controller without a form ?. Also, i would like do do something like this, but i see that's not possible:
<g:form action="addFavourite">
<td>
<g:submitButton name="${it.area.name}" value="Add" class="button small blue"/><br><br>
</td>
</g:form>
To name the button with a value that comes from a controller isnt working. Any possible alternative for that? It gives me a null-error-code. And i'm 100% sure the value isnt null..
You can create a button outside a form that executes a controller action when it's clicked using the remoteFunction tag
<button type="button" name="myButton"
onclick="${remoteFunction(action:'bookByName', controller: 'book'
params:'\'bookName=\' + this.value')}">Click Here</button>
It kind of depends. If you want a button to submit to a server via a standard POST then no. HTML doesn't even have a button that works without a form. You can fake this with an image link that looks like a button, but really it just submits via a standard anchor tag. And this would perform a GET, not a POST.
However, if you want to use ajax, you could skip the Grails tags (as I often do) and use the HTML BUTTON element. Could even use the remoteFunction to make the ajax call if you want.
UPDATE: Doh! 2 of the same answers. :)
What does "it" stands here for? I think that is the culprit..
<g:submitButton name="${it.area.name}" value="Add" class="button small blue"/>

Resources