JQuery for Everyone: Print (Any) Web Part(s) Plugin
As I build up to the new, enhanced Preview Pane code, I want to highlight some of the more modular features it will have.
Find Something, Do Something With It
I improved the Print Any Web Part code and, once again, made it “plugin-style.” Now you can write your own links by using a selector, like $(”#WebPartWPQ1″), and add the .print() method. You can also add css instructions like so: print(css:”.ms-toolbar{display:none;}”).
Mash It Up!
If you mash this up with my other plugin, SharePoint Client-Side Object Model, you can find web parts no matter where they are on the page or which order they load in: $(”#”+wp.Tasks2.id).add(”#”+wp.Calendar.id).print().
<script type="text/javascript"> if(typeof jQuery=="undefined"){ var jQPath="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/"; document.write("<script src='",jQPath,"jquery.min.js' type='text/javascript'><\/script>"); } </script> <script type="text/javascript"> (function(){ jQuery.fn.print = function(options){ var o = $.extend({css:"",},options); var html = "<HTML>\n<HEAD>\n<style type='text/css'>\n"+ options.css+"</style>"+ $("head").html()+"\n</HEAD>\n<BODY>\n"; this.each(function(i,e){ html += "<DIV style='font-family:verdana,arial,helvetica,"+ "sans-serif;font-size:8pt;margin:20px;'>\n"+ $(e).html()+"</DIV>\n"; }); html += "</BODY>\n</HTML>"; var printWP = window.open("","printWebPart"); printWP.document.open(); printWP.document.write(html); printWP.document.close(); printWP.print(); return this; }; })(); </script>
- JQuery for Everyone: Accordion Left Nav
- JQuery for Everyone: Print (Any) Web Part
- JQuery for Everyone: HTML Calculated Column
- JQuery for Everyone: Dressing-up Links Pt1
- JQuery for Everyone: Dressing-up Links Pt2
- JQuery for Everyone: Dressing-up Links Pt3
- JQuery for Everyone: Cleaning Windows Pt1
- JQuery for Everyone: Cleaning Windows Pt2
- JQuery for Everyone: Fixing the Gantt View
- JQuery for Everyone: Dynamically Sizing Excel Web Parts
- JQuery for Everyone: Manually Resizing Web Parts
- JQuery for Everyone: Total Calculated Columns
- JQuery for Everyone: Total of Time Differences
- JQuery for Everyone: Fixing Configured Web Part Height
- JQuery for Everyone: Expand/Collapse All Groups
- JQuery for Everyone: Preview Pane for Multiple Lists
- JQuery for Everyone: Preview Pane for Calendar View
- JQuery for Everyone: Degrading Dynamic Script Loader
- JQuery for Everyone: Force Checkout
- JQuery for Everyone: Replacing [Today]
- JQuery for Everyone: Whether They Want It Or Not
- JQuery for Everyone: Linking the Attachment Icon
- JQuery for Everyone: Aspect-Oriented Programming with jQuery
- JQuery for Everyone: AOP in Action - loadTip Gone Wild
- JQuery for Everyone: Wiki Outbound Links
- JQuery for Everyone: Collapse Text in List View
- JQuery for Everyone: AOP in Action - Clone List Header
- JQuery for Everyone: $.grep and calcHTML Revisited
- JQuery for Everyone: Evolution of the Preview
- JQuery for Everyone: Create a Client-Side Object Model
- JQuery for Everyone: Print (Any) Web Part(s) Plugin
- JQuery for Everyone: Minimal AOP and Elegant Modularity
- JQuery for Everyone: Cookies and Plugins
- JQuery for Everyone: Live Events vs. AOP
- JQuery for Everyone: Live Preview Pane
- JQuery for Everyone: Pre-populate Form Fields
- JQuery for Everyone: Get XML List Data with OWSSVR.DLL (RPC)
- Use Firebug in IE
- JQuery for Everyone: Extending OWS API for Calculated Columns
- JQuery for Everyone: Accordion Left-nav with Cookies Speed Test
- JQuery for Everyone: Email a List of People with OWS
- JQuery for Everyone: Faster than Document.Ready
- jQuery for Everyone: Collapse or Prepopulate Form Fields
- jQuery for Everyone: Hourly Summary Web Part
- jQuery for Everyone: "Read More..." On a Blog Site
- jQuery for Everyone: Slick Speed Test
- jQuery for Everyone: The SharePoint Game Changer
- JQuery For Everyone: Live LoadTip
I do not understand what I would do with this code or where to put it.?. Sorry if it is obvious. Looks useful.
I agree with Mike. Would be nice with a simple example…
I added the code to my test site and it works. I have a few problems.
1. How do I get the calendar to print on one page?
2. How do I get it to work with other web parts? Here are some examples: (Hidden) Cross-Site Menu, (Hidden) LoadTip Live and other ‘great stuff’ I got from EndUsersSharePoint.
I add it to the site and only the print code works.
Hope you can help.
@Frank,
1. I’m guessing that the calendar will print to a single page if you can adjust your print settings. If not, you’re allowed to pass custom CSS instructions in the call to $.print(). See the example in this article.
2. Check the versions of jQuery you’re using. If you’re mashing up web parts, make sure you use the same version everywhere.
As for making specific code work together, I can only speak for my own creations and I never tested this with LoadTip Live.
Hi Paul:
Would you give us some help on how to install and utilize this plug-in?
Thanks-
Charlie Epes
If I have 1000+ items, will it print page by page or you can all the pages?
Here is a simpler version of this code.
It not jquery, but to be honest theres hardly a need for jquery on small functions like these..
function print() {
var a = window.open('', '', 'scrollbars=yes,width=850,height=650');
a.document.open("text/html");
a.document.write('');
a.document.write(document.getElementById('print').innerHTML);
a.document.write('');
a.document.close();
a.print();
}
Thanks Sam. I’m in agreement that some of the things I’m seeing can be done with a simple javascript function as opposed to making a call to jQuery. Specifically, some of the interface enhancements available could just as easily be one with plain CSS in a CEWP. — Mark
@Sam,
Thanks for your suggestion. However, your script does not preserve the CSS/style used on the area being printed. You also neglected to mention that your version does not work in SharePoint because there is no element with id=print. I’ll also add, that part of this script’s utility is that it does not clog the global namespace with a generic name like yours, it extends jQuery instead–which followers of this article are already using for many other solutions.
I’ll admit, I wrote this a long time ago, and probably would write better version today, but when I offered this, I tested it in multiple browsers in SharePoint–your code is just a copy-paste example from somewhere else.