Forum

November 2nd, 2014
A A A
Avatar

Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

The forums are currently locked and only available for read only access
sp_Feed Topic RSS sp_TopicIcon
For those looking to print a jqGrid grid.
07/09/2012
22:28
Avatar
nelsonm
Michigan, USA
Member
Members
Forum Posts: 106
Member Since:
20/08/2011
sp_UserOfflineSmall Offline

Hi all,

For those who want to be able to print a jqGrid grid, i'd like to offer a nice solution. Even though you only have to specify the main grid id, any sub grids nested within the main grid print out nicely as well. All of this is done on the client side in javascript.

The function adds a print button to the end of the standard jqGrid buttons on the left side of the navigation bar and binds the button to the print function on click. The function uses jQuery and jqGrid methods as well as the third party "printElement" plugin to accomplish the task.

0. download and install Erik Zaadi's "printElement" plugin at: http://projects.erikzaadi.com/.....038;hellip;..ntElement/ .

1. create a div container somewhere.

<div id='prt-container' class='hide'>
</div>

2. create a "print-grid.css" file that contains entries to hide the container and display grid lines on print.

.hide {display:none;}
table {border-collapse:collapse;}
th, td {border:1px solid black; !important}

3. add the "setPrintGrid" function call to the end of your grid definition. The function parameters are (grid_id, pager_id, button_title)

// example jqGrid
$('#tab4-grid').jqGrid({
.
pager: '#tab4-pager',
toppager: true, // comment out if you don't want the the top nav bar.
.
});

// setup grid print capability. Add print button to navigation bar and bind to click.
setPrintGrid('tab4-grid','tab4-pager','Customer Grid');

4. add the setPrintGrid function definition to some javascript file with a global namespace.

// setup grid print capability. Add print button to navigation bar and bind to click.
function setPrintGrid(gid,pid,pgTitle){
  // print button title.
  var btnTitle = 'Print Grid';

  // setup print button in the grid top navigation bar.
  $('#'+gid).jqGrid('navSeparatorAdd','#'+gid+'_toppager_left', {sepclass :'ui-separator'});
  $('#'+gid).jqGrid('navButtonAdd','#'+gid+'_toppager_left', {caption: '', title: btnTitle, position: 'last', buttonicon: 'ui-icon-print', onClickButton: function() {PrintGrid();} });

  // setup print button in the grid bottom navigation bar.
  $('#'+gid).jqGrid('navSeparatorAdd','#'+pid, {sepclass : "ui-separator"});
  $('#'+gid).jqGrid('navButtonAdd','#'+pid, {caption: '', title: btnTitle, position: 'last', buttonicon: 'ui-icon-print', onClickButton: function() { PrintGrid();} });

  function PrintGrid(){
   // empty the print div container.
   $('#prt-container').empty();

   // copy and append grid view to print div container.
   $('#gview_'+gid).clone().appendTo('#prt-container').css({'page-break-after':'auto'});

   // remove navigation divs.
   $('#prt-container div').remove('.ui-jqgrid-toppager,.ui-jqgrid-titlebar,.ui-jqgrid-pager');

   // print the contents of the print container.
   $('#prt-container').printElement({pageTitle:pgTitle, overrideElementCSS:[{ href:'css/print-grid.css',media:'print'}]});
  }
}

I hope it helps someone.

10/09/2012
18:47
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

I do not have time to test this,

but Thank youfor sharing your solution.

Kind Regards

Tony

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

10/09/2012
19:55
Avatar
nelsonm
Michigan, USA
Member
Members
Forum Posts: 106
Member Since:
20/08/2011
sp_UserOfflineSmall Offline

No problem…

I'm sure someone can probably streamline the process a bit and make it a little more flexable.

On a side note…

I realized that the "print-grid.css" file should not contain the ".hide" css entry.  The ".hide" css entry should be put in a css file that is defined in the head of the index page.  Also, the "print-grid.css" file should NOT be defined on the index or any other page.  It should just exist in the css folder of the site and be called from the "printElement" plugin.

Defining "print-grid.css" in the head of the index or any other page would interfere with other tables.

Sorry about that.

10/09/2012
23:21
Avatar
nelsonm
Michigan, USA
Member
Members
Forum Posts: 106
Member Since:
20/08/2011
sp_UserOfflineSmall Offline

Also, preceding the "table, th and td" css entries in the "print-grid.css" file in the original post with "#prt-container" should eliminate any problems with the css entries interfering with other tables.

12/09/2012
19:49
Avatar
nelsonm
Michigan, USA
Member
Members
Forum Posts: 106
Member Since:
20/08/2011
sp_UserOfflineSmall Offline

Here is the jqgrid screen.

Image Enlarger

Here is an image of a work order grid printout with subgrids.

printout of gridImage Enlarger

24/09/2012
23:25
Avatar
mah
Brazil
Member
Members
Forum Posts: 4
Member Since:
24/09/2012
sp_UserOfflineSmall Offline

nelsonm, tyvm for the code! A really nice solution for the printing problem.

But Im having 2 issues: 

1. Everytime I reload the grid, a new print button is set in the navgrid.

2. I have a footer row with sums and when I print the grid the footer stays in between the grid rows and not in the end of the last page. I even tried to remove the footer from the prt-container but no sucess.

Any workarounds? Confused

25/09/2012
00:40
Avatar
nelsonm
Michigan, USA
Member
Members
Forum Posts: 106
Member Since:
20/08/2011
sp_UserOfflineSmall Offline

mah,

Issue #1 does not occur in my tests.  make sure the "setPrintGrid" function call is placed within the document ready function that contains the grid definition.  I place my "setPrintGrid" call at the bottom of the docutment ready function.

Also, i have updated the code a bit so you don't have to define a print container or add the ".hide" css in some css file.  the new code creates these for you.  all you have to do is create the "print-grid" css file.  the new code is displayed in the next post.

$(function() {

// example jqGrid
$('#tab4-grid').jqGrid({
.
pager: '#tab4-pager',
toppager: true, // comment out if you don't want the the top nav bar.
.
});

// setup grid print capability. Add print button to navigation bar and bind to click.
setPrintGrid('tab4-grid','tab4-pager','Customer Grid');

});

issue #2 - you'll have to display a screen shot of the your grid before i can help with that.

25/09/2012
00:49
Avatar
nelsonm
Michigan, USA
Member
Members
Forum Posts: 106
Member Since:
20/08/2011
sp_UserOfflineSmall Offline

hi all,

I modified the "setPrintGrid" function a bit.  You no longer have to manually define the ".hide" css or the print container div.  The "setPrintGrid" function now does that for you.  All you have to do is create the "print-grid.css" file.

I have posted the new code in a new post so this one does not go on forever.

thanks.

01/10/2012
16:40
Avatar
mah
Brazil
Member
Members
Forum Posts: 4
Member Since:
24/09/2012
sp_UserOfflineSmall Offline

Hi!

Thanks for the response!

#1 – I was calling setPrintGrid before loading the navgrid. Now I put the call after the navgrid(obviously lol) and it worked! :)

#2 -Im posting the images. 1st image is the grid on the screen. It has groups and totals for each group. And then in the bottom is the global total that is fixed in the grid as a footer.

2nd its the print of the grid with the fixed footer overlaying the other columns . Smile

[Image Can Not Be Found]

[Image Can Not Be Found]

02/10/2012
21:34
Avatar
mah
Brazil
Member
Members
Forum Posts: 4
Member Since:
24/09/2012
sp_UserOfflineSmall Offline

Hello!

So still about #2, I got the footer to stay in the top of the printed page(as img below) by inserting

$('#prt-container .ui-jqgrid-sdiv').after($('#prt-container .ui-jqgrid-bdiv'));

Right before removing the navigation divs in the function PrintGrid.

[Image Can Not Be Found]

But I still cant manage to put the footer at the end of the last line of the grid in the printing page.

Everything I tried, the footer overlays the other rows in the 1st page of the printing page as I showed before. Cry

03/10/2012
02:21
Avatar
nelsonm
Michigan, USA
Member
Members
Forum Posts: 106
Member Since:
20/08/2011
sp_UserOfflineSmall Offline

mah,

I can't explain why this is happening to you since i don't know how you have put together your grid.  In any case, you should not have to modify any of the setPrintGrid function code.  Why don't you zip and email me the file that contains your grid definition and setPrintGrid function call.  I can't promise you anything, but i will take a quick look at it.

04/10/2012
20:21
Avatar
mah
Brazil
Member
Members
Forum Posts: 4
Member Since:
24/09/2012
sp_UserOfflineSmall Offline

I manage to solve the issue with the fixed footer today. Aparently was a matter of css. Im posting my solution maybe it will help others if they have similar issues. I was using jqueryui for the css of the jqgrid.

I put this code inside PrintGrid(), right before the code removing the navigation divs:

            //Fixed footer in the last page

            $('#prt-container .ui-jqgrid-bdiv').css({ 'height': 'auto' });
            $('#prt-container .ui-jqgrid-sdiv').before($('#prt-container .ui-jqgrid-bdiv'));

            // remove navigation divs.

Besides that, I tested a little more your function and put some header in the 1st page of the printed page.

//Insert logo as a header of the 1st page

$('#prt-container').append('<img src="../../img/Logo1.jpg" display="table">');

    // copy and append grid view to print div container.

14/02/2013
13:16
Avatar
JiheL
France
Member
Members
Forum Posts: 19
Member Since:
02/12/2009
sp_UserOfflineSmall Offline

Hi Nelsomn

First of all many thanks for your work which will help me in app building !

Please can you tell me why I obtain the message 'a.browser is undefined' from printElement.js when I click on Print button in Firefox 18.0.2 ?

Again, great work !

Cheers

JiheL

10/07/2013
10:22
Avatar
gushendra86
Jakarta
Member
Members
Forum Posts: 3
Member Since:
10/07/2013
sp_UserOfflineSmall Offline

Hello, this plugins help me very much, Thanks to you first 🙂

I have a problem when i click the print button. It appear in the bottom of jqgrid

Before :

beforeImage Enlarger

After i click the button it appear :

Image Enlarger

Can anyone help me how not display the print after jqgrid ? Many Thanks.

10/07/2013
12:53
Avatar
tony
Sofia, Bulgaria
Moderator
Members

Moderators
Forum Posts: 7721
Member Since:
30/10/2007
sp_UserOfflineSmall Offline

Hello,

It depends what you do.

Can you please show your code when the print icon is clicked?

Regards

For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.

12/07/2013
11:53
Avatar
gushendra86
Jakarta
Member
Members
Forum Posts: 3
Member Since:
10/07/2013
sp_UserOfflineSmall Offline

hai, it's done. I just add

$('#prt-container').empty();

after

$('#prt-container').printElement({pageTitle:pgTitle, overrideElementCSS:[{ href:'css/print-grid.css',media:'print'}]});

Anyway, 1 more problem is when i click print, it does not print a div with background color ? Any solution ?

This is my code :

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.printElement.js"></script>
<script type="text/javascript">
function PrintGrid(){
    // empty the print div container.
    $('#prt-container').empty();
    
    // copy and append grid view to print div container.
    
    $('#form').clone().appendTo('#prt-container').css({'page-break-after':'auto'});
    
    // print the contents of the print container.
    $('#prt-container').printElement({overrideElementCSS:[{ href:'index.css',media:'print'}]});
    $('#prt-container').empty();
}
</script>
<div id='prt-container' class='hide'>
</div>

<div id="form">
    <a href="asd">asdasda</a>
    <div id="clr">h</div>
</div>
<div>
test
</div>
<a href="#" onclick="PrintGrid();">print</a>

and this is the css :

#clr {
background:red; width:200px; height:20px; padding:50px; color:red;
}

Many Thank you.

12/07/2013
21:16
Avatar
macrabbit
New Member
Members
Forum Posts: 1
Member Since:
12/07/2013
sp_UserOfflineSmall Offline

Hi, I got the same issue with you . I found that the "<div id="prt-container" ></div>" was not hidden . But if i add the css as like 

"<div id="prt-container"  style="display: none;"></div>"  , will be solved the issuse. But the grid contain could not show on the print page .

Can you help me too?

Thanks

15/07/2013
03:57
Avatar
gushendra86
Jakarta
Member
Members
Forum Posts: 3
Member Since:
10/07/2013
sp_UserOfflineSmall Offline

Hai macrabbit, my code is just like this 🙂

// setup grid print capability. Add print button to navigation bar and bind to click.
function setPrintGrid(gid,pid,pgTitle){
  // print button title.
  var btnTitle = 'Print Grid';

  // setup print button in the grid top navigation bar.
  $('#'+gid).jqGrid('navSeparatorAdd','#'+gid+'_toppager_left', {sepclass :'ui-separator'});
  $('#'+gid).jqGrid('navButtonAdd','#'+gid+'_toppager_left', {caption: '', title: btnTitle, position: 'last', buttonicon: 'ui-icon-print', onClickButton: function() {PrintGrid();} });

  // setup print button in the grid bottom navigation bar.
  $('#'+gid).jqGrid('navSeparatorAdd','#'+pid, {sepclass : "ui-separator"});
  $('#'+gid).jqGrid('navButtonAdd','#'+pid, {caption: '', title: btnTitle, position: 'last', buttonicon: 'ui-icon-print', onClickButton: function() { PrintGrid();} });

  function PrintGrid(){
   // empty the print div container.
   $('#prt-container').empty();

   // copy and append grid view to print div container.
   $('#prt-container').append('<img src="../img_logo/33logo.png" display="table">');
   $('#gview_'+gid).clone().appendTo('#prt-container').css({'page-break-after':'auto'});

   // remove navigation divs.
   $('#prt-container div').remove('.ui-jqgrid-toppager,.ui-jqgrid-titlebar,.ui-userdata,#jqgh_grid_act,#jqgh_grid_cb,.s-ico,.ui-jqgrid-pager');
   
   $('#prt-container div .cbox').remove();
   $('#prt-container div input').remove();

   // print the contents of the print container.
   $('#prt-container').printElement({pageTitle:pgTitle, overrideElementCSS:[{ href:'css/print-grid.css',media:'print'}]});
   $('#prt-container').empty();
  }
}
// setup grid print capability. Add print button to navigation bar and bind to click.
setPrintGrid('grid','paging','Admin Page');

Hope you success too Smile

Forum Timezone: Europe/Sofia

Most Users Ever Online: 715

Currently Online:
52 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

OlegK: 1255

markw65: 179

kobruleht: 144

phicarre: 132

YamilBracho: 124

Renso: 118

Member Stats:

Guest Posters: 447

Members: 11373

Moderators: 2

Admins: 1

Forum Stats:

Groups: 1

Forums: 8

Topics: 10592

Posts: 31289

Newest Members:

, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.com

Moderators: tony: 7721, Rumen[Trirand]: 81

Administrators: admin: 66

Comments are closed.
Privacy Policy   Terms and Conditions   Contact Information