1

I'm using HTML and JSF Primefaces, for this part of the System, PrimeFaces are not enabled (don't know why), the matter is:

I have a button

<h:commandButton id="btnPrint"
    onclick = "switchDisabled();"
    actionListener="#{componentTrackingController.printReport()}"
    value="Print Report"
/>

onClick its executing and working well (it just disables some other buttons) but actionListener is not. The fact is that when I delete the "onClick" line, actionsListener works fine.

My JavaScript Code is simple (but I think the problem is not here)

<script type="text/javascript">

    function switchDisabled(){
        askDisabled(document.getElementById('mainForm:cboURdependences'));
        askDisabled(document.getElementById('mainForm:btnPrint'));
        askDisabled(document.getElementById('mainForm:btnCloseReport'));
    }

    function askDisabled(elem){
        if (elem.disabled != true){
            elem.disabled = true;
        }else{
            elem.disabled = false;
        }
    }  

</script>    
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Alex Andrade
  • 342
  • 1
  • 3
  • 14
  • 2
    You're disabling your button when you do onclick, it's called prior to the submission, so it then cant submit / click a disabled button. You can create a ajax hook into 'onsuccess' for your ajaxListener that then calls switchDisabled() – VeenarM Mar 19 '14 at 22:29
  • it automatically loads the report at loadingPage – Alex Andrade Mar 19 '14 at 22:53

2 Answers2

0

I'm not familiar with jsf/PrimeFaces but per this question: Primefaces onclick and onsuccess differences

Try using "oncomplete":

<h:commandButton id="btnPrint"
    oncomplete = "switchDisabled();"
    actionListener="#{componentTrackingController.printReport()}"
    value="Print Report"
/>

From PrimeFaces documentation (3.5):

oncomplete: Javascript handler to execute when ajax request is completed.

Community
  • 1
  • 1
Allende
  • 1,480
  • 2
  • 22
  • 39
  • 3
    You can't call oncomplete on a h:commandButton - it requires ajax embedded, need to use p:commandbutton for oncomplete. – VeenarM Mar 19 '14 at 23:47
  • 1
    Mmm I assume p is for,partial, and the h:button works similar to the onclick/postback couple on aspx, am i kind of right ? – Allende Mar 20 '14 at 00:11
  • It's primefaces component, which wraps the standard h:commandButton, and does ajax submit by default, unless you use ajax="false" then it does standard form submit. It has its own oncomplete tags. – VeenarM Mar 20 '14 at 00:15
0

As per my comment, it's doing it instantly. There are a few ways you could fix this, (the ajax hook) or setTimeout in javascript.

<h:commandButton id="btnPrint"
    onclick = "setTimeout(function(){ switchDisabled() }, 1000);"
    actionListener="#{componentTrackingController.printReport()}"
    value="Print Report"
/>

This binds a timout to call your switchDisabled tag in 1 second (1000ms) - thus your ajax etc should of already been run and it binds it, you'll find you can probably change it to 500 or something even less, but you dont want it to fire it before your submit is called.

Refer to: http://www.w3schools.com/jsref/met_win_settimeout.asp for more information

VeenarM
  • 1,263
  • 1
  • 10
  • 14