title: ng-grid export to CSV plugin -- how to download all data even if paged
body: <p>Hi I am working with angular's ng-grid to display about 1000 rows of data. Users are allowed to view the data in the actual table 10, 20, or 100 rows at a time..but regardless of how much data they are viewing at a time, I want them to be able to download a CSV of all 1000 rows.</p>
  
  <p>Is there a way to do this with the current plugin? I've been struggling for a while now since the plug in always goes to the actual ng-grid for the data to create a CSV from.</p>
  
  <p>ng-grid plug in:</p>
  
  <pre><code>ngGridCsvExportPlugin = function(opts) {
  var self = this;
  self.grid = null;
  self.scope = null;
  self.init = function(scope, grid, services) {
      self.grid = grid;
      self.scope = scope;
  
      function showDs() {
          var keys = [];
  
          for (var f in grid.config.columnDefs) { if (grid.config.columnDefs[f].field) { keys.push(grid.config.columnDefs[f].field); }}
          var csvData = '';
          function csvStringify(str) {
              if (str == null) return '';  // we want to catch anything null-ish, hence just == not ===
              if (typeof(str) === 'number') return '' + str;
              if (typeof(str) === 'boolean') return (str ? 'TRUE' : 'FALSE') ;
              if (typeof(str) === 'string') return str.replace(/"/g,'""');
              return JSON.stringify(str).replace(/"/g,'""');
          }
          function swapLastCommaForNewline(str) {
              var newStr = str.substr(0,str.length - 1);
              return newStr + "\n";
          }
          for (var k in keys) {
              csvData += '"' + csvStringify(keys[k]) + '",';
          }
          csvData = swapLastCommaForNewline(csvData);
          var gridData = grid.data;
          for (var gridRow in gridData) {
              for ( k in keys) {
                  var curCellRaw;
                  if (opts != null &amp;&amp; opts.columnOverrides != null &amp;&amp; opts.columnOverrides[keys[k]] != null) {
                    curCellRaw = opts.columnOverrides[keys[k]](gridData[gridRow][keys[k]]);
                  } else {
                    curCellRaw = gridData[gridRow][keys[k]];
                  }
                  csvData += '"' + csvStringify(curCellRaw) + '",';
              }
              csvData = swapLastCommaForNewline(csvData);
          }
          var fp = grid.$root.find(".ngFooterPanel");
          var csvDataLinkPrevious = grid.$root.find('.ngFooterPanel .csv-data-link-span');
          if (csvDataLinkPrevious != null) {csvDataLinkPrevious.remove() ; }
          var link = "data:text/csv;charset=UTF-8," + encodeURIComponent(csvData);
          var csvDataLinkHtml = "&lt;span class=\"csv-data-link-span\"&gt;";
          csvDataLinkHtml += "&lt;br&gt;&lt;a class=\"btn hidden btn-primary exportTable2CSV\" href=" + link + " download=\"Export.csv\"&gt;CSV Export&lt;/a&gt;&lt;/br&gt;&lt;/span&gt;" ;
          fp.append(csvDataLinkHtml);
          scope.$emit("exportTable2CSVLinkReady", link);
      }
  
      setTimeout(showDs, 0);
      scope.catHashKeys = function() {
  
          showDs();
  
        hash = '';
        for (idx in scope.renderedRows) { hash += scope.renderedRows[idx].$$hashKey;  }
        return hash;
      };
      scope.$watch('catHashKeys()', showDs);
  };
  };
  </code></pre>