2

I am using google.visualization.PatternFormat to format the url in the column into a hyperlink in Google Chart Table. The current approach will format the whole column into a hyperlink. I would like to skip the format if the cell in that column is empty.

  function handleSampleDataQueryResponse(response) {

    var data = response.getDataTable();

    var format_demo = new google.visualization.PatternFormat(
    '<a href="{0}">Link</a>'); 
    format_demo.format(data, [7]);

    var chartTbl = new google.visualization.Table(document.getElementById('dataTable'));
    chartTbl.draw(view, {showRowNumber: true, allowHtml: true, page: 'enable', pageSize: 5, width: '100%', height: '100%'
    });

As the column is optional, I would like to leave the cell blank rather than have a hyperlink that will go nowhere.

grenovic
  • 21
  • 1

1 Answers1

1

You're basically trying to put a FormatPattern inside a condition (like an if statement).

I did a little research and it looks like (at least as far as Ray was able to determine in this aging question), you may need to loop through your rows to apply a format conditionally because the PatternFormat documentation doesn't seem to provide a mechanism for this.

Fortunately, the DataTable Class documentation gives us several functions that could be helpful for your use case, and I expect your code might, for example, use getFilteredRows and setFormattedValue and look something like:

var emptyRowInds = getFilteredRows([{column: 7, value: ''}]);

var columnInd = 7;
var formattedVal = null;
  // Alternatively, you could hard-code `formattedVal = '';`

for (var rowInd = 0;  rowInd < emptyRowInds.length; rowInd++){
  setFormattedValue(emptyRowInds[rowInd], columnInd, formattedVal);
}

This code would run after your pattern has been applied to all rows, with the goal of removing the newly applied format from any rows where our target cell's value is an empty string (because I expect Google to return an empty string as the value of any empty cell; if the returned value for such cells is actually something else, such as null, we would obviously want to supply that value to getFilteredRows instead.)

Note that this code is untested (and is really just a first guess) but should hopefully get you much closer to a solution.

Cat
  • 4,141
  • 2
  • 10
  • 18
  • this is the right approach, the value will return `null`, if `null` was loaded initially. however, the row index will not necessarily equal the index of the array returned from `getFilteredRows` -- need to use `setFormattedValue(emptyRowInds[rowInd]` -- instead of -- `setFormattedValue(rowInd` – WhiteHat Sep 06 '19 at 11:18
  • Thanks @WhiteHat. Edited accordingly. – Cat Sep 06 '19 at 11:34
  • Thanks. It worked. I modified to compare with null var emptyRowInds = data.getFilteredRows([{column: 7, value: null}]); – grenovic Sep 08 '19 at 03:26
  • Awesome. Mark the answer as accepted if you feel like it. – Cat Sep 09 '19 at 03:50