2013-10-16 50 views
1

我目前有一个预览我们的表单,看起来完全如何我们希望它查找列表项正在查看,但问题是当我添加我的打印按钮作为CEWP在FOrm显示它执行完全相同的功能,使用控制P并打印整个页面。看代码。CEWP打印按钮与打印SharePoint表单的代码?

  <div style="text-align: center"><input onclick="window.print();return false;" type="button" value=" Print this page "/>&#160;</div>" 

我想添加到这个拥有它只能打印表格,并在窗口中没有其他内容了侧当前视图和实际填充8.5×11

任何想法?

+0

也许媒体查询例如http://www.joshuawinn.com/css-print-media-query/ – Plato

回答

0

this InfoPath print button的启发,一种解决方案是获取CEWP的内容并创建一个仅包含这些内容的新窗口。

var patt = /**your CEWP ID here***/g; 
var alldivs = document.getElementsByTagName('div'); 
var printpageHTML = ''; 
for(var i=0; i<alldivs.length; i++){ 
    if(patt.test(alldivs[i].id)){ 
    printpageHTML = '<HTML><HEAD>\n' + 
     document.getElementsByTagName('HEAD')[0].innerHTML + 
     '</HEAD>\n<BODY>\n' + 
     alldivs[i].innerHTML.replace('inline-block','block') + 
     '\n</BODY></HTML>'; 
    break; 
    } 
} 
var printWindow = window.open('','printWindow'); 
printWindow.document.open(); 
printWindow.document.write(printpageHTML); 
printWindow.document.close(); 
printWindow.print(); 
printWindow.close(); 

修复:删除HTML字符的转义。

+0

我正在打开新窗口,但它是空白的。我相信我正在使用正确的CEWP ID,但它似乎并没有引起任何问题。我应该从哪里抽取CEWP ID? – user2887014

+0

在Internet Explorer中,使用F12开发人员工具。通过点击选择元素非常方便,可以找到渲染Web部件的代码。我的一个内容编辑器Web部件名为'WebPartWPQ2'。 –

+0

这工作很好!正是我想要的。干杯! – user2887014

0

简单的打印功能:

<input id="printpagebutton" type="button" value="Print this page" onclick="printpage()"/> 

<script type="text/javascript"> 
function printpage() { 
    //Get the print button and put it into a variable 
    var printButton = document.getElementById("printpagebutton"); 
    //Set the print button visibility to 'hidden' 
    printButton.style.visibility = 'hidden'; 
    //Print the page content 
    window.print() 
    //Set the print button to 'visible' again 
    //[Delete this line if you want it to stay hidden after printing] 
    printButton.style.visibility = 'visible'; 
    } 
</script>