2

Like in play tutorial for the Tags Set (customize crud field); i want to add a new functionality when user selects a tag then javascript should get current user (models.user) and call a method user.doSomething(). is this possible to do in a CRUD custom field ? (like in tutorial for yabe tags?).

is there a way I can make a custom CRUD field as a list of checkboxes and when user check some checkboxes then i should write code (maybe javascript) that gets current logged in user and then call a method on that user Model?

thanks.

1
  • it will be very simple for you to create your own form than using the one generated by crud... Commented Feb 19, 2012 at 19:44

1 Answer 1

1

Here is the example from the play tutorial (customize crud field). i suppose that you want to execute an action when a tag is clicked. that can be done simply as shown below

#{crud.form}
    #{crud.custom 'tags'}
        <label for="tags">
            &{'tags'}
        </label>
        (...)
        <div class="tags-list">
            #{list items:models.Tag.findAll(), as:'tag'}
               <span id="${tag.id}" onclick="toggle(this)" class="tag ${object.tags.contains(tag) ? 'selected' : ''}">
                     /**** ADD An ACTION CALL HERE ******/
                    <a href="@{Application.getConnected().doSomething()}">${tag}</a>
                     /**** END *****/
               </span> 
               <input id="h${tag.id}" type="hidden" name="${fieldName}" 
                        value="${object.tags.contains(tag) ? tag.id : ''}" />
            #{/list}
        </div>
    #{/crud.custom}
#{/crud.form}

you have to implement the method getConnected() by your self in you controller (maybe Application.java). supposing that you use the Secure Module it will be something like

void getConnected(){ 
        if(Security.isConnected()) {
            User user = User.find("byEmail", Security.connected()).first();
            renderArgs.put("user", user);
        }

}
Sign up to request clarification or add additional context in comments.

5 Comments

thank you ; i'll give it a try and let you know if it works. i noticed how you wrapp the tag name inside a href and point it to @{User.getConnected().doSomething()} . seems a clever workaround. i'll try to see if this works
itried it but got this error : No route found (In {module:crud}/app/views/tags/crud/form.html around line 37) No route able to invoke action User.getConnected was found. it seems i need to add a route for the model user ? how can we do this? it is unable to detect User.getConnected()
you have to implement the method by your self. i updated my answer, have a look..
It still gives this error No route found (In {module:crud}/app/views/tags/crud/form.html around line 37) No route able to invoke action User.getConnected was found. i need to implement a controller action method and call it instead of a method in the models.User . i don't think calling a models.user method will work. it must be a controller action method with route defined.
So the workaround that works for me: is to implement a controllers.Users.dosomething() action method. make this method last call renderJSON(user). add url map to route file. then in javascript make the call to Users.dosomething(). and better use jquery $.get(url, function(data) to make it an ajax call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.