partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

GridExporter.js (2498B)


      1 /**
      2  * Exports a grid with all visible fields and rows
      3  */
      4 Ext.define("PartKeepr.Exporter.GridExporter", {
      5     constructor: function (gridPanel, format, extension)
      6     {
      7         this.gridPanel = gridPanel;
      8         this.format = format;
      9         this.extension = extension;
     10     },
     11     exportGrid: function ()
     12     {
     13         var columns = this.gridPanel.getColumns();
     14 
     15         var store = this.gridPanel.getStore();
     16         var records = store.getData();
     17         var record, i, j, value, column, fieldValue;
     18         var rows = [], rowValues = [];
     19 
     20         for (i = 0; i < columns.length; i++) {
     21             if (!columns[i].isHidden()) {
     22                 rowValues.push(Ext.util.Format.stripTags(columns[i].text));
     23             }
     24         }
     25 
     26         rows.push(rowValues);
     27 
     28         for (i = 0; i < records.length; i++) {
     29             rowValues = [];
     30             record = records.getAt(i);
     31 
     32             for (j = 0; j < columns.length; j++) {
     33                 column = columns[j];
     34 
     35                 fieldValue = record.get(column.dataIndex);
     36 
     37                 if (column.renderer && column.renderer.call) {
     38                     value = column.renderer.call(
     39                         column.usingDefaultRenderer ? column : column.scope || this.gridPanel,
     40                         fieldValue,
     41                         null,
     42                         record,
     43                         i,
     44                         j,
     45                         store,
     46                         this.gridPanel.getView());
     47 
     48                 } else {
     49                     value = fieldValue;
     50                 }
     51 
     52                 if (!column.isHidden()) {
     53                     rowValues.push(Ext.util.Format.stripTags(value));
     54                 }
     55             }
     56 
     57             rows.push(rowValues);
     58         }
     59 
     60 
     61         var options = {
     62             headers: {}
     63         };
     64 
     65         options.headers["Accept"] = this.format;
     66         options.jsonData = rows;
     67         options.method = "POST";
     68         //this.down("#formatSelector").getValue().get("mimetype");
     69         options.url = PartKeepr.getBasePath() + "/api/export";
     70         options.callback = Ext.bind(this.onExportSuccessful, this);
     71         Ext.Ajax.request(options);
     72     },
     73     /**
     74      * Callback for when the export is complete. Creates a client-side blob object and forces
     75      * download of it.
     76      */
     77     onExportSuccessful: function (options, success, response)
     78     {
     79         var blob = new Blob([response.responseText], {type: this.format});
     80         saveAs(blob, "export." + this.extension);
     81     }
     82 });