Compress XML, String, Variables in Client Side and Export in HTML5 Using jQuery

In this article you will learn how to compress a XML, String, Variables in the client side and export in HTML5 using JQuery.

Introduction

Normally developers download Excel of any other file format from the server side. It may be the correct way to do the exporting if and only if the data is quite small. As you all know, when you develop a product or website, you may need to convince many people including in that project. You must convince your client because he/she is paying for it. :) Your client will always look at whether your product is worth the money he will spend.

Background

For the past few months I am working on the client side exporting, compression, decompression and so on.

Before going through the client-side mechanisms, you must be aware of what all the problems are in server-side exporting.

  • You need to upload the content to the server first. This will take time when the data is more. When it takes more than 2 minutes, no one will wait for it. They will just cancel the process and close your application. So your reputation is diminished.

  • Once the contents are uploaded, the server will initiate the process of downloading. Again this may take time.

The Process

The process that I will do is so simple.

You may need to look at my previous article that explains the Excel exporting mechanism in the client side.

  1. Export From HTML Table Using jQuery.
  2. Export Hierarchical (Multi-Level) HTML Table With Styles Using jQuery.
Here I am listing what exactly I am going to do with my data.

1. Please find the SampleExcelFileData file. Consider that I have data as in that document. You can see some content but there is not much. I am giving you a demo with that content. I have taken this data from the JQX Grid. For the past few months I have been working in JQX JQwidgets, the implementation I have done is for using it in my JQX grid. If you are new to JQX Grid you can check the following links. 2. Once the data is ready we can go with the compression part. We have the XML string as our data. We are going with the client-side compression mechanism.

For the compression I have used LZW compression, please find more here.

The following is the code for the compression:
  1.  var LZW = {  
  2.     compress: function (uncompressed) {  
  3.         "use strict";  
  4.         // Build the dictionary.  
  5.         var i,  
  6.             dictionary = {},  
  7.             c,  
  8.             wc,  
  9.             w = "",  
  10.             result = [],  
  11.             dictSize = 256;  
  12.         for (i = 0; i < 256; i += 1) {  
  13.             dictionary[String.fromCharCode(i)] = i;  
  14.         }  
  15.   
  16.         for (i = 0; i < uncompressed.length; i += 1) {  
  17.             c = uncompressed.charAt(i);  
  18.             wc = w + c;  
  19.             //Do not use dictionary[wc] because javascript arrays   
  20.             //will return values for array['pop'], array['push'] etc  
  21.             // if (dictionary[wc]) {  
  22.             if (dictionary.hasOwnProperty(wc)) {  
  23.                 w = wc;  
  24.             } else {  
  25.                 result.push(dictionary[w]);  
  26.                 // Add wc to the dictionary.  
  27.                 dictionary[wc] = dictSize++;  
  28.                 w = String(c);  
  29.             }  
  30.         }  
  31.   
  32.         if (w !== "") {  
  33.             result.push(dictionary[w]);  
  34.         }  
  35.         return result;  
  36.     }  
  37. }  
The preceding code does the compression, now we need to check the implementation. Am I right?
  1. $("#excelExport").click(function () {  
  2.    var exportInfo = LZW.compress($("#jqxgrid").jqxGrid('exportdata''xls'));   
  3. });  
In the preceding code you can see that I am compressing the data that I have taken from the JQX Grid.

You can get the grid data as follows.
  1. $("#jqxgrid").jqxGrid('exportdata''xls');  
To learn more about exporting in JQX Grid, please see here: Advanced JQX Grid With All Functionality.

You can always decompress the data as follows.
  1. decompressedVariable = LZW.decompress(exportInfo);  
The following is the code for the decompression:
  1. decompress: function (compressed) {  
  2.     "use strict";  
  3.     // Build the dictionary.  
  4.     var i,  
  5.         dictionary = [],  
  6.         w,  
  7.         result,  
  8.         k,  
  9.         entry = "",  
  10.         dictSize = 256;  
  11.     for (i = 0; i < 256; i += 1) {  
  12.         dictionary[i] = String.fromCharCode(i);  
  13.     }  
  14.   
  15.     w = String.fromCharCode(compressed[0]);  
  16.     result = w;  
  17.     for (i = 1; i < compressed.length; i += 1) {  
  18.         k = compressed[i];  
  19.         if (dictionary[k]) {  
  20.             entry = dictionary[k];  
  21.         } else {  
  22.             if (k === dictSize) {  
  23.                 entry = w + w.charAt(0);  
  24.             } else {  
  25.                 return null;  
  26.             }  
  27.         }  
  28.   
  29.         result += entry;  
  30.   
  31.         // Add w+entry[0] to the dictionary.  
  32.         dictionary[dictSize++] = w + entry.charAt(0);  
  33.   
  34.         w = entry;  
  35.     }  
  36.     return result;  
  37. }  
Output Compression And Decompression:

cs code
code

In the preceding image you can see the length of the content before compression and after compression.

Before compression, the length is 41447. And after compression the length is 5452.

Now we know how to compress and decompress the contents :).

Cool. We have done it.

What else is pending? Yeah you are right, we need to export that content.

Exporting Using BLOB

Excel exporting is an important feature in every application. Yeah we do have an Excel exporting mechanism. :)

For the Excel exporting we are using a new technology called BLOB in HTML5.

To work on it, you need to attach the script:
  1. <script src="FileSaver.min.js"></script>  
This script does the saving of the Excel file. So it is important that we include it though.

So once you have included that file we can move on to the next level.

The Implementation

The following code explains the implementation.
  1. //This will download the excel file without compression (before compression of data)  
  2.   
  3. saveMyFile($('#SubmitForm'), "My Excel File" + ".xls", $("#jqxgrid").jqxGrid('exportdata''xls'), 'text/xls;charset=utf-8');  
  4. //this will download the file with compression (after compression of the data)  
  5. saveMyFile($('#SubmitForm'), "My Excel File" + ".xls", exportInfo, 'text/xls;charset=utf-8');  
You can see that the parameters of the function saveMyFile,
  1. Reference form.
  2. File name.
  3. The string to be exported. In our case, it is our XML string.
  4. The mime type, for example: 'text/xls;charset=utf-8'.

The export function

  1. function saveMyFile(ref, fname, text, mime) {  
  2.    var blob = new Blob([text], { type: mime });  
  3.    saveAs(blob, fname);  
  4.    return false;  
  5. }  
Once you pass the parameters, this function will do the remaining of what needs to be done. Sounds great, right? :)

Your Excel file will be exported in a fraction of a second.

Great work by the Blob function. :)

See the file size difference

Now I hope you have two downloaded files: 
  1. Without compression
  2. With compression

Let us see the size difference now.

excel file
Here, My Excel File.xls is without compression and My Excel File(1).xls is with compression. I hope you see the difference. :)

Now it is time for the complete HTML.

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <title id='Description'>This example illustrates how to customize the filtering conditions available in the columns popup menu.  
  5.     </title>  
  6.     <script src="jquery-1.9.1.js"></script>  
  7.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxcore.js"></script>  
  8.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxdata.js"></script>  
  9.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxbuttons.js"></script>  
  10.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxscrollbar.js"></script>  
  11.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxlistbox.js"></script>  
  12.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxdropdownlist.js"></script>  
  13.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.js"></script>  
  14.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.filter.js"></script>  
  15.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.sort.js"></script>  
  16.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.selection.js"></script>  
  17.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.pager.js"></script>  
  18.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.columnsresize.js"></script>  
  19.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.columnsreorder.js"></script>  
  20.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxgrid.export.js"></script>  
  21.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxdata.export.js"></script>  
  22.     <script type="text/javascript" src="JQXItems/jqwidgets/jqxdatatable.js"></script>  
  23.     <script src="JQXItems/jqwidgets/jqxcheckbox.js"></script>  
  24.     <script src="JQXItems/jqwidgets/jqxmenu.js"></script>  
  25.     <link href="JQXItems/jqwidgets/styles/jqx.base.css" rel="stylesheet" />  
  26.     <script src="generatedata.js"></script>  
  27.     <script src="FileSaver.min.js"></script>  
  28.     <script type="text/javascript">  
  29.         $(document).ready(function () {  
  30.             var LZW = {  
  31.                 compress: function (uncompressed) {  
  32.                     "use strict";  
  33.                     // Build the dictionary.  
  34.                     var i,  
  35.                         dictionary = {},  
  36.                         c,  
  37.                         wc,  
  38.                         w = "",  
  39.                         result = [],  
  40.                         dictSize = 256;  
  41.                     for (i = 0; i < 256; i += 1) {  
  42.                         dictionary[String.fromCharCode(i)] = i;  
  43.                     }  
  44.   
  45.                     for (i = 0; i < uncompressed.length; i += 1) {  
  46.                         c = uncompressed.charAt(i);  
  47.                         wc = w + c;  
  48.                         //Do not use dictionary[wc] because javascript arrays   
  49.                         //will return values for array['pop'], array['push'] etc  
  50.                         // if (dictionary[wc]) {  
  51.                         if (dictionary.hasOwnProperty(wc)) {  
  52.                             w = wc;  
  53.                         } else {  
  54.                             result.push(dictionary[w]);  
  55.                             // Add wc to the dictionary.  
  56.                             dictionary[wc] = dictSize++;  
  57.                             w = String(c);  
  58.                         }  
  59.                     }  
  60.   
  61.                     if (w !== "") {  
  62.                         result.push(dictionary[w]);  
  63.                     }  
  64.                     return result;  
  65.                 },  
  66.                 decompress: function (compressed) {  
  67.                     "use strict";  
  68.                     // Build the dictionary.  
  69.                     var i,  
  70.                         dictionary = [],  
  71.                         w,  
  72.                         result,  
  73.                         k,  
  74.                         entry = "",  
  75.                         dictSize = 256;  
  76.                     for (i = 0; i < 256; i += 1) {  
  77.                         dictionary[i] = String.fromCharCode(i);  
  78.                     }  
  79.   
  80.                     w = String.fromCharCode(compressed[0]);  
  81.                     result = w;  
  82.                     for (i = 1; i < compressed.length; i += 1) {  
  83.                         k = compressed[i];  
  84.                         if (dictionary[k]) {  
  85.                             entry = dictionary[k];  
  86.                         } else {  
  87.                             if (k === dictSize) {  
  88.                                 entry = w + w.charAt(0);  
  89.                             } else {  
  90.                                 return null;  
  91.                             }  
  92.                         }  
  93.   
  94.                         result += entry;  
  95.   
  96.                         // Add w+entry[0] to the dictionary.  
  97.                         dictionary[dictSize++] = w + entry.charAt(0);  
  98.   
  99.                         w = entry;  
  100.                     }  
  101.                     return result;  
  102.                 }  
  103.             }  
  104.             var url = "products.xml";  
  105.             // prepare the data  
  106.             var source =  
  107.             {  
  108.                 datatype: "xml",  
  109.                 datafields: [  
  110.                     { name: 'ProductName', type: 'string' },  
  111.                     { name: 'QuantityPerUnit', type: 'int' },  
  112.                     { name: 'UnitPrice', type: 'float' },  
  113.                     { name: 'UnitsInStock', type: 'float' },  
  114.                     { name: 'Discontinued', type: 'bool' }  
  115.                 ],  
  116.                 root: "Products",  
  117.                 record: "Product",  
  118.                 id: 'ProductID',  
  119.                 url: url  
  120.             };  
  121.             var cellclass = function (row, columnfield, value) {  
  122.                 if (value < 20) {  
  123.                     return 'red';  
  124.                 }  
  125.                 else if (value >= 20 && value < 50) {  
  126.                     return 'yellow';  
  127.                 }  
  128.                 else return 'green';  
  129.             }  
  130.             var dataAdapter = new $.jqx.dataAdapter(source, {  
  131.                 downloadComplete: function (data, status, xhr) { },  
  132.                 loadComplete: function (data) { },  
  133.                 loadError: function (xhr, status, error) { }  
  134.             });  
  135.             // initialize jqxGrid  
  136.             $("#jqxgrid").jqxGrid(  
  137.             {  
  138.                 width: 850,  
  139.                 source: dataAdapter,  
  140.                 pageable: true,  
  141.                 autoheight: true,  
  142.                 sortable: true,  
  143.                 altrows: true,  
  144.                 enabletooltips: true,  
  145.                 columns: [  
  146.                   { text: 'Product Name', datafield: 'ProductName', width: 250 },  
  147.                   { text: 'Quantity per Unit', datafield: 'QuantityPerUnit', cellsalign: 'right', align: 'right', width: 120 },  
  148.                   { text: 'Unit Price', datafield: 'UnitPrice', align: 'right', cellsalign: 'right', cellsformat: 'c2', width: 100 },  
  149.                   { text: 'Units In Stock', datafield: 'UnitsInStock', cellsalign: 'right', cellclassname: cellclass, width: 100 },  
  150.                   { text: 'Discontinued', columntype: 'checkbox', datafield: 'Discontinued' },  
  151.                 ]  
  152.             });  
  153.             $("#excelExport").click(function () {  
  154.                 debugger;  
  155.                 var decompressedVariable;  
  156.                 console.log($("#jqxgrid").jqxGrid('exportdata''xls'));  
  157.                 var exportInfo = LZW.compress($("#jqxgrid").jqxGrid('exportdata''xls'));               
  158.                 //This will download the excel file without compression (before compression of data)  
  159.                 saveMyFile($('#SubmitForm'), "My Excel File" + ".xls", $("#jqxgrid").jqxGrid('exportdata''xls'), 'text/xls;charset=utf-8');  
  160.                 //this will download the file with compression (after compression of the data)  
  161.                 saveMyFile($('#SubmitForm'), "My Excel File" + ".xls", exportInfo, 'text/xls;charset=utf-8');  
  162.                 decompressedVariable = LZW.decompress(exportInfo);  
  163.               
  164.             });  
  165.             function saveMyFile(ref, fname, text, mime) {  
  166.                 var blob = new Blob([text], { type: mime });  
  167.                 saveAs(blob, fname);  
  168.                 return false;  
  169.             }  
  170.         });  
  171.     </script>  
  172. </head>  
  173. <body class='default'>  
  174.     <input type="button" value="Export to Excel" id='excelExport' />  
  175.     <style>  
  176.         .green {  
  177.             color: black\9;  
  178.             background-color: #b6ff00\9;  
  179.         }  
  180.   
  181.         .yellow {  
  182.             color: black\9;  
  183.             background-color: yellow\9;  
  184.         }  
  185.   
  186.         .red {  
  187.             color: black\9;  
  188.             background-color: #e83636\9;  
  189.         }  
  190.   
  191.         .green:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected), .jqx-widget .green:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected) {  
  192.             color: black;  
  193.             background-color: #b6ff00;  
  194.         }  
  195.   
  196.         .yellow:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected), .jqx-widget .yellow:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected) {  
  197.             color: black;  
  198.             background-color: yellow;  
  199.         }  
  200.   
  201.         .red:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected), .jqx-widget .red:not(.jqx-grid-cell-hover):not(.jqx-grid-cell-selected) {  
  202.             color: black;  
  203.             background-color: #e83636;  
  204.         }  
  205.     </style>  
  206.     <div id='jqxWidget' style="font-size: 13px; font-family: Verdana; float: left;">  
  207.         <div id="jqxgrid">  
  208.         </div>  
  209.     </div>  
  210. </body>  
  211. </html>  
Note: I have implemented the grid with a color render implementation. You can omit it and implement a simple grid if you work on the JQX Grid. Please note that you can give any string for the compression and decompression. For my convenience I selected the JQX Grid data.

Conclusion

Please download the attachment and try it. Please do not forget to give your valuable suggestions.

Point of interest

Export, Client-side Export, Compression in the client side, Decompression in the client side, Export using BLOB, Export in jQuery.

That is all for the day, will see you in another article.

Kindest Regards,

Sibeesh