angular-ui/ui-grid

Allow formatting of PDF cells through exporterFieldCallback

Open

#5.227 geöffnet am 14. März 2016

Auf GitHub ansehen
 (10 Kommentare) (1 Reaktion) (0 zugewiesene Personen)JavaScript (2.496 Forks)batch import
help wanted

Repository-Metriken

Stars
 (5.395 Stars)
PR-Merge-Metriken
 (Keine gemergten PRs in 30 T)

Beschreibung

I needed to be able to customize the PDF export a little bit more, so I started looking through the documentation and code to see how to do that. It seems that if you allowed an object return from exporterFieldCallback it would give the end user a lot of flexibility.

In my local copy, I just added an else if check for object type and return the raw field.value.

Here's the full formatFieldAsPdfString function:

        formatFieldAsPdfString: function (field) {
          var returnVal;
          if (field.value == null) { // we want to catch anything null-ish, hence just == not ===
            returnVal = '';
          } else if (typeof(field.value) === 'number') {
            returnVal = field.value.toString();
          } else if (typeof(field.value) === 'boolean') {
            returnVal = (field.value ? 'TRUE' : 'FALSE') ;
          } else if (typeof(field.value) === 'string') {
              returnVal = field.value.replace(/"/g, '""');
          } else if (typeof (field.value) === 'object') {
              returnVal = field.value;
          } else {
              returnVal = JSON.stringify(field.value).replace(/^"/,'').replace(/"$/,'');
          }

          if (field.alignment && typeof(field.alignment) === 'string' ){
            returnVal = { text: returnVal, alignment: field.alignment };
          }

          return returnVal;
        }

This now allows me to add code like this:

gridOptions.exporterFieldCallback = function (grid, row, col, value) {
if (col.name === 'Status') {
  if (value === 'Active')
     value = { text: value, color: 'green' };
  else
     value = { text: value, color: 'red' };
}
return value;
}

Thank you, Josh

Contributor Guide