2015-06-25 56 views
1

我在jsp中有兩個html按鈕。一個是添加,另一個是刪除。 現在,如果ony的按鈕被點擊,然後會顯示一個dojo確認對話框。點擊對話框中的「確定」,將執行添加或刪除功能。這個Dojo對話框出現在其中一個父JSP頁面中,該頁面將被存在添加或刪除功能的其他JSP頁面重用。點擊添加/刪除按鈕,點擊確認消息也需要更改。防爆。對於添加,消息應該是'你想添加',因爲刪除消息將是'你想刪除'。我能夠在DOJO Cinfirm對話框中動態設置消息,但無法設置onExecute回調函數,即。當Ok將在Dialog中被點擊。代碼如下所示。在DOJO確認對話框中動態設置onExecute回調函數

注:我使用Dojo 1.10庫

確認的對話框代碼:

require(["dijit/ConfirmDialog", "dojo/domReady!"], function(ConfirmDialog){ 
    myDialog = new ConfirmDialog({ 
     title: "GCLDW - Confirm Dialog", 
     content: "", 
     style: "width: 300px;", 
     onExecute:function(){ 
      removeCustomer(); 
     } 
    }); 
}); 

HTML按鈕代碼:

<button type="button" id="removeCustomerButton" 
     onclick="myDialog.set('content', 'Do you want to remove the selected item ?<br><br>');myDialog.show();"> 
           <SPAN class=grey><EM><s:message 
            code="customer.removeCustomer" text="" /></EM> 
           </SPAN> 
</button> 

<button type="button" id="addCustomerButton" 
     onclick="myDialog.set('content', 'Do you want to Add the selected item ?<br><br>');myDialog.show();"> 
           <SPAN class=grey><EM><s:message 
            code="customer.addCustomer" text=""/></EM> 
           </SPAN> 
</button> 

現在怎麼設置onExecute回調函數依賴在按鈕單擊上,它可能是addCustomer()或removeCustomer(),並且使用此對話框的任何頁面都將擁有它們自己的imp這兩種方法的運用。

回答

1

只需設置onExecute塊 - 以相同的方式 - 如何設置內容。

另外一個建議是-把JS代碼從HTML中分離出來。

這裏去工作,around-

HTML代碼 -

<button type="button" id="removeCustomerButton"> 
     <SPAN class=grey><EM>Remove</EM></SPAN> 
</button> 

<button type="button" id="addCustomerButton"> 
     <SPAN class=grey><EM>Add</EM></SPAN> 
</button> 

&的DOJO-

require(["dijit/ConfirmDialog", "dojo/domReady!"], 
      function(ConfirmDialog){ 
       var myDialog = new ConfirmDialog({ 
        title: "GCLDW - Confirm Dialog", 
        content: "", 
        style: "width: 300px;" 
       }); 

       dojo.query('#removeCustomerButton').onclick(function() { 
        myDialog.set('content', 'Do you want to remove the selected item ?<br><br>'); 
        myDialog.set('onExecute', function(){removeCustomer()}); // cant call it directly- must wrap within a function 
        myDialog.show(); 
       }); 

       dojo.query('#addCustomerButton').onclick(function() { 
        myDialog.set('content', 'Do you want to Add the selected item ?<br><br>'); 
        myDialog.set('onExecute', function(){addCustomer()}); // cant call it directly- must wrap within a function 
        myDialog.show(); 
       }); 


      }); 


    function removeCustomer(){ 
     console.log("removeCustomer called on execute"); 
    } 

    function addCustomer(){ 
     console.log("addCustomer called on execute"); 
    } 
+0

讓我知道,如果你需要的jsfiddle鏈接運行 - 在上面的代碼片段。 –

+1

謝謝Dude ..它工作:) – Sambuddha

0

/dojo-release-1.10.4 -src/dijit/_DialogMixin.js,評論狀態:

execute: function(/*Object*/ /*===== formContents =====*/){ 
// summary: 
//  Callback when the user hits the submit button. 
//  Override this method to handle Dialog execution. 

我實現了一個ConfirmDialog像這樣:

var confirmDialog = new ConfirmDialog({ 
    title: core.confirm 
}); 
confirmDialog.startup(); 

function confirmAction(text, callbackFn) { 
    confirmDialog.set("execute", callbackFn); 
    confirmDialog.set("content", text); 
    confirmDialog.show(); 
} 

,並把它稱爲如下:

lib.confirmAction(core.areyousure, function() { 
    markedForDeletion.forEach(function (node) { 
     // Do deletion 
    }); 
});