How to use printing data , PDF and XLS file of data using DataTable
Posted By : Tushar Paliwal | 25-Jul-2014
In my project I need functionality of printing data , PDF and XLS file of that data from front end. After spending a lot time over internet, I found code in java which provides such kind of functionality but It seems that code very much cumbersome which was difficult to manage.
Then I investigated more over internet and found a solution in JQuery which provided me same functionality using few lines of code. I used dataTable which provide printing data , PDF and XLS file using JQuery and this functionality in dataTable is known as oTableTool.
Here I will show you how It works by using grails project in Spring Tool Suits(STS).
Step 1:
Create a grails project using
File>New>Grails Project
Step 2:
Put CSS file in Project_Directory/Project_Name/web-app/css directory.
-
dataTables.tableTools.css
-
jquery.dataTables.css
Step 3:
Put JS file in Project_Directory/Project_Name/web-app/js directory.
-
dataTables.tableTools.js
-
jquery.dataTables.js
Step 4:
Put JS file in Project_Directory/Project_Name/web-app/swf directory.
-
copy_csv_xls.swf
-
copy_csv_xls_pdf.swf
Step 5:
Create a controller which will accept request and render appropriate view .Now create a table in html to view the data.
$('#example').dataTable({ "dom" : 'CT<"clear">lfrtip', "oTableTools" : { "sSwfPath" : "../swf/copy_csv_xls_pdf.swf", "aButtons" : [ "copy", "print", { "sExtends" : "collection", "sButtonText" : "Save", "aButtons" : [ "csv", "xls", "pdf" ] } ] } });
<"clear">
In above code I used T in dom which is used to show oTableTool functionality.
example : Table Id
sSwfPath : Path of .swf file.
aButtons : Buttons for copy, print, pdf and xls functionality.
Feel free to ask any query relavent to this code.
Thank You
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Tushar Paliwal
Tushar is a developer with experience in Groovy and Grails , Spring and enterprise Java Technologies.