开发者

Disable image submit button in PrimeFaces p:datatable

I have a DataTable with a CommandButton which acts like this:

<h:commandButton action="#{communicator.requestFavoriteDetails}"
onclick="disableSubmit(this.id);"
update="@navi:tableFavorites">
<f:setPropertyActionListener value="#{book}"
target="#{favorites.selectedFavorite}" />
<f:ajax />  
</h:commandButton>

(I just removed some tags...)

This button works fine. But on some (mobile) devices, a request is sent twice on click. I don`t know why.. I tried everything but nothing worked to prevent this. (e.g. )

This code is rendered to an: tag and I know this can not be disabled.

I want to add a timer. When a user clicks on this button the "onclick" event should be replaced with an empty function and after 2.. seconds it should be restored. This way I can prevent a request to be sent twice.

The function disableSubmit(id) acts like this:

function disableSubmit(id) {
    v开发者_开发问答ar new_func = "function() { return false }";
    if ( typeof(document.getElementById(id)) == "object" && typeof(document.getElementById(id).onclick) == "function") {
        if ($("#" + escapeJSFid(id)).attr('onclick') != new_func) {
        var old_func = $("#" + escapeJSFid(id)).attr("onclick");
        $("#" + escapeJSFid(id)).attr('onclick','"+new_func+"').delay(2000).attr('onclick','"+old_func+"');
        }
    }
}

But this code does not work. FF does not return any errors.. Oh.. and escapeJSFid is just a simple function:

function(id){ id.replace(/:/g,"\\:"); }

Does anybody know how I can prevent this request to be sent twice? I know there are many other solutions and my code is poorely written.. but I just want it to work.

Best regards


Seems like I solved my problem:

function disableSubmit(id) {
    $("#" + escapeJSFid(id)).click( function() {
            // alert ("DISABLED");
        return false;
    });
}

Onclick: Function disableSubmit(id) will be executed. The onClick function will be removed. But, I guess PrimeFaces datatable stores its inforamtions about buttons etc. in a bean (somewhere). The buttons are getting restored automatically with their old values and events (onclick). When I doubleclick my button I see the DISABLED alert message. But when I click on the button, wait 2-3 seconds and click then again on the button no message appears. The normal function is being executed. So I guess my button gets restored automatically after the "action=''" event is completed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜