Creating a Grail Jquery Modal Window and Form posting with ajax?

半城伤御伤魂 提交于 2019-12-02 01:45:42

问题


I have the following code :

<body>
        <div id="dialog-form" title="Create a new Comment">
        <form>
        <fieldset>
        <label for="name">Enter your Comment Please </label>
        <textarea rows="6" cols="2" name="commentArea" id="commentArea" ></textarea>
        </fieldset>
         </form>
        </div>
        <button id="create-user">Create new user</button>
</body>

and my Modal Window using jquery-UI

     <g:javascript>
      $(function(){
    $("#dialog-form").dialog ({
    autoOpen:false,
    height:300,
    resizable:false,
    width:350,
    modal:true,
    buttons: {
    "Attach Comment": function() {
    alert('assum it already submitted'); // ? ? ? this time what can i add to post a form to  a controller attachComments with commentArea posted.

    $(this).dialog("close");
    },
    Cancel: function() {
    $(this).dialog("close");
    }
    },
    close: function() {
    alert(Form is cloased!); 
    $(this).dialog("close");
    }
    });

   $( "#create-user" )
  .button()
  .click(function() {
    $( "#dialog-form" ).dialog( "open" );
  });

         }); 
</g:javascript>        

The above code draws my modal window but how can i post the form to attachCommentController and receive responses back to to show errors on modal window?


回答1:


You can Use remote form or remote links, or ajax calls to interact with the controllers and you will be able to get the response back to your view, you can receive it in some other div.

for example it can be done through remoteForm like below:

On your view you can use remote form like:

    <g:formRemote  
         name="productForm"
        url="[controller: 'attachCommentController', action:'save']"
        update="resultsDiv"
                >
       <fieldset>
        <label for="name">Enter your Comment Please </label>
        <textarea rows="6" cols="2" name="commentArea" id="commentArea" ></textarea>
    <inuput type="submit" name="submit" value="Save Value"/>
        </fieldset>
    </g:formRemote>
   <div id="resultsDiv">You will receive your response here</div>

This way you will be able to get the response back to your modal window.




回答2:


Ok Using implementation case 1 is to use ajax and display error or success on the opened dialog :

So , i have added the following ajax code on above code section ...

"Attach Comment": function() {
        //do form posting here
        $.ajax({ 
        context: $(this),
        url:"${resource()}"+"/issue/attachComment",
        type:"POST",
        data:{"comment":$('#commentArea').val(),"id":$("#selectedIssueInstanceId").val()},
        success:function(data){
        if(data.success)
        {
        var tobeAppendeID = $("#selectedIssueInstanceId").val();
        $('#'+'comment_'+tobeAppendeID).val(data.newComment);
        $( this ).dialog( "close" );
        }
        else
        {
        $('#error-message').addClass('ui-state-error ui-corner-all');
        $("#error-message").html(data.message).show()
        $(this).dialog("close");
        }
        $( this ).dialog( "close" );
        }


来源:https://stackoverflow.com/questions/17269570/creating-a-grail-jquery-modal-window-and-form-posting-with-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!