2016-11-16 62 views
0

我想知道是否有任何方法来显示基于在函数中传递的参数的打印按钮。这里是我的代码创建对话框:在JQuery中显示/隐藏打印按钮?

alertInfo: function (message, title, height, width, print) { 
    $("<div></div>").dialog({ 
     buttons: { 
      "Ok": function() { 
       $(this).dialog("close"); 
      }, 
      //show/hide button if argument 'print' equals to 'Yes' 
      "Print": function() { 
       $(this).dialog().printArea(); 
      }, 
     }, 
     close: function (event, ui) { $(this).remove(); }, 
      resizable: false, 
      title: title, 
      modal: true, 
      width: height, 
      height: width, 
      overflow:'auto', 
      position: { 
       my: "center", 
       at: "center", 
       of: window 
      } 
    }).html(message); 
} 

这是我传递的参数在alerInfo()函数的代码:

$.alertInfo(infoTable,'User Info',800,600,'Yes'); 

我还没有得到这个工作,如果我试图如果发生“打印”按钮错误时出现语句。如果有人能帮助请让我知道。

+0

什么是“OK”和“打印”在您的代码 – Geeky

+0

@Geeky确定和打印是按钮。 –

回答

1
alertInfo: function(message, title, height, width, print) { 
    var buttons = { 
     "Ok": function() { 
      $(this).dialog("close"); 
     } 
    }; 
    if (print) buttons.print = function() { 
     $(this).dialog().printArea(); 
    }; 
    $("<div></div>").dialog({ 
     buttons: buttons, 
     close: function(event, ui) { 
      $(this).remove(); 
     }, 
     resizable: false, 
     title: title, 
     modal: true, 
     width: height, 
     height: width, 
     overflow: 'auto', 
     position: { 
      my: "center", 
      at: "center", 
      of: window 
     } 
    }).html(message); 
} 

这是假设的打印参数是一个布尔

$.alertInfo(infoTable,'User Info',800,600, true); // to show print button 

$.alertInfo(infoTable,'User Info',800,600, false); // to not show print button 
1

你有效地传递对象到buttons属性:

{ 
    "Ok": function() { 
     $(this).dialog("close"); 
    }, 
    "Print": function() { 
     $(this).dialog().printArea(); 
    } 
} 

所以你可以做的是动态创建对象基于您的条件,然后将动态创建的对象设置为属性。它可能看起来像这样:

// create the object 
var myButtons = { 
    "Ok": function() { 
     $(this).dialog("close"); 
    } 
}; 

// conditionally add a "print" button 
if (someCondition) { 
    myButtons.Print = function() { 
     $(this).dialog().printArea(); 
    } 
} 

// use the object 
$("<div></div>").dialog({ 
    buttons: myButtons, 
    close: function (event, ui) { $(this).remove(); }, 
    // etc. 
});