grails remoteFunction update attribute - grails

The Grails documentation for the remoteFunction update attribute states:
update (optional) - Either a Map containing the elements to update for 'success' or 'failure' states, or a string with the element id to update, in which case failure events would be ignored
I'm a little confused why the indicate a Map and not a List here, is this wrong? I imagine I want my remote action to return a Map, where the keys match the name of the list elements specified in the update attribute??
Thanks

Use update, if you want to replace a part of the current page, which is identified by the (html)-id in the update tag. E.g.:
<div id="resultOfAJAX"></div>
<g:remoteFunction update="resultOfAJAX" [..] />
However this is not useful, if the AJAX call fails. That is why you can define which element is to be updated in the failure case:
<div id="resultOfAJAX"></div>
<div id="someErrorField"></div>
<g:remoteFunction update="[success: 'resultOfAJAX', failure: 'someErrorField']" [..] />
I am not sure, whether this clarifies your question regarding update, but maybe it helps ;)

Related

How to have conditional autofocus in Svelte 3

I would like to shift focus on my page in response to keyboard input. With Svelte it seems that the simplest way is to use the autofocus attribute. However I cannot get conditional autofocus attributes to work.
Here's a simple repro of my problem: https://svelte.dev/repl/0861d097921d4a35957f016a8c35cfe6?version=3.44.3
Am I doing something wrong or is this not possible in Svelte 3?
Conditional autofocus was possible in a previous version of Svelte according to this question: Sveltejs render html attribute conditionally
And conditionality is possible for other attributes in Svelte 3 according to this question: How to have a conditional attribute in Svelte 3?
One solution could be to add a key block around the thing to make it re-run on every update of the focusIndex.
Example
{#key focusIndex}
{#each items as item, index}
<input autofocus="{index === focusIndex}" value={item} on:keydown={(ev) => test(ev)} />
{/each}
{/key}
Not the prettiest solution, but might unblock you.
Not strictly an answer to the exact question I asked, but the alternative that I've gone with is to use bind:this on the elements to create an array of the DOM elements and then in the keydown handler explicitly call .focus() on the DOM elements.
Example: https://svelte.dev/repl/83934cd7924741a8a4c5a620f79d5bb5?version=3.44.3

adding an id attribute to q-input

Say I have the following q-input:
<q-input
v-model="form.email"
inverted-light
color="white"
stack-label="Email:"
type="email"
#blur="$v.form.email.$touch"
:error="$v.form.email.$error"/>
I'd like to be able to make it so that if the domain of the email is mydomain.com that the form action will change to another website (without csrf protection) and the POST will be made to that website instead of the main one.
To do this I was thinking I could use jQuery. eg. $('#email').val().replace(/^.+#/, '') == 'mydomain.com' then change the form action and submit.
The only problem is: I don't know how to set an id attribute on q-input.
Any ideas?
As of early Quasar 1.4.2 (November of this year) you can specify the id value on the resulting html generated by q-input by using the "for" property (see the end of the behavior properties: https://quasar.dev/vue-components/input#QInput-API).
So, for example, you could add for="myInputId":
<q-input
v-model="form.email"
inverted-light
color="white"
stack-label="Email:"
type="email"
#blur="$v.form.email.$touch"
:error="$v.form.email.$error"
for="myInputId"
/>
The id attribute with value "myInputField" will end up on the resulting <input> element in your HTML.
Not using the "for" in the elements gave me a lot of headaches because the Jest snapshot generated random IDs

How to get the values true or false depending on a list of checkbox checked or not in Grails

I have check boxes in a list
HTML Code
<g:checkBox id="isBookable_${index}" class=" isBookable" style = "width :auto" name='isBookable' value='${careProviderScheduleExceptionInstance?.isBookable}' />
Problem
When I get the list of this checkbox i.e isBookable in params, the values for only the checkboxes that are checked comes in the list and the value comes as on and not true.
I want the values should come as true or false based on if the checkbox is checked or not.
Thanks in advance.
Grails g:checkbox does some shenanigans for the binding magic to work correctly when you do something like:
def schedule = new Schedule(params)
So that schedule.bookable would get populated correctly. I've never been a big fan of how it works but it is what it is and at the end of the day, it does work for typical use cases.
You have to remember that per the HTML spec, you're not going to get unchecked checkboxes passed in on a form submit. So looking for false is a moot point. What you would have to do is take the ones that do get checked (and submitted) and compare that against a list of possible bookable 'schedules'.
A work around would be to use Javascript to submit your form and pass in all the checkbox's with whatever value you want mapped to the correct instance.

Accessing a list of params in controller

Im very new to grails (1.3.7) so please be patient :-)
I have a gsp where I have various checkboxes. A user can click on them and then send his answer to a controller. The controller is receiving this request correctly.
My problem is, that, for working with what the user chose, I have to check every parameter - to see if this checkbox was really checked. Thats really cumbersome and doesnt work very well, because the page displaying the checkboxes is dynamic - so the checkboxes which can be clicked are dynamic too. In my controller I dont know for which params I have to check then.
Is there any possibility to receive a list of all checkboxes (or better: all checked checkboxes) in my controller? I researched but didnt find an answer!
Thanks for answering! :-)
[EDIT]
Thank you,
params.name.each{i->
System.out.println(i);
}
is very simple and works :-) It just gives back the checked ones
It must be passed as an extra request parameter (it's a limitation of http). You can add following field into your form, for example:
<input type="hidden" name="checkboxes" value="${myCheckboxesNames.join(',')}"/>
or making same using JavaScript, as it names are dynamic on client side.
BTW, you can also check all request parameters, by
params.each { name, value ->
// so something
}
so if you are using some special prefix/suffix for this checkbox names, it would be:
params.entrySet().findAll {
it.key.startsWith(prefix)
}.each {
println "Checkbox $it.key = $it.value"
}

Grails: Updating multiple page elements with ajax

It seems that with g:formRemote and g:submitToRemote, I can only specify one update target div. What would be a viable strategy for updating multiple divs?
if you want to stick with using the g:formRemote tags to perform your ajax, it might not be possible. Why not write some jQuery, and roll a custom ajax update? it couldnt be easier!
You could instead of using update, use onSuccess and parse the response and update the elements you need to update like this:
<g:formRemote name='loginForm' url="[action:'login']"
onSuccess='loginOK(e)' >
<!-- form fields -->
</g:formRemote>
<g:javascript>
function loginOK( resp ) {
// parse the resp.responseText and update
}
</g:javascript>
Or you could just use jQuery and roll your own like the previous answer suggests.
If you want to update one element based on success vs another on failure you can use a map like this:
<g:formRemote name='loginForm' url="[action:'login']" update="[success:'message',failure:'error']">
there is asolution to this grails problem, you can use taconite, where taconite allows to update muliple elements with one single ajax call
http://malsup.com/jquery/taconite/
and there is another solution posted at someone's blog, but my reputation does only allow me to post one link here on stackoverflow!!!
so i give the title of the blog post "Updating multiple page elements with Grails and Ajax"
From the grails doc for g:formremote, http://grails.org/doc/2.2.0/ref/Tags/formRemote.html , you can use the onSuccess callback to define a js function for a succesfull submit, so you can update all your target elements within that function, otherwise making an ajax call yourself also is a good option.

Resources