Anonymous Human
Anonymous Human

Reputation: 1948

Grails launch controller's remote functions from within javascript

I have this button on my GSP

<button id = "select"
            onclick = "${remoteFunction(controller: 'customer', action: 'selectBatch',
                            params: '\'batchID=\' + selectedBatchID')}">Select</button>

I also have this javascript function on the same button:

$('#select').click(function () {
    if (selectedBatchID == undefined) {
        $("#errorMessages p").text("Click a row to choose a batch first.");
        $("#errorMessages p").show();
        $("#errorMessages p").fadeOut(3000);
    }
    else {
        $("#choosebatch h2").text("Batch ID " + selectedBatchID + ": " + selectedBatchDesc);
    }

what I would like to do is to move the button's onclick function declared in the GSP code into the else clause of the javascript function so that the controller function doesn't launch all the time, only when the javascript else clause is executed. I just can't seem to figure out the right syntax. Any help/tips appreciated.

Upvotes: 0

Views: 65

Answers (2)

user3578266
user3578266

Reputation:

Something similar to previous answer is that you can try this:

$.ajax({
               type: 'POST',
               url: "${createLink(action:'customer', controller:'selectBatch')}/batchID=" + selectedBatchID
        });

Upvotes: 1

user3718614
user3718614

Reputation: 530

I guess you can use AJAX in else block. Something like that:

$.ajax({
    url: '/PROJECT_NAME/customer/selectBatch',
    type: 'GET',
    data: 'batchID=' + XXX,
    success: function(data) {
        console.log(data);
        XXX
    }
});

Upvotes: 1

Related Questions