【问题标题】:Google Chart Table - how to skip hyperlink formatting if the cell is emptyGoogle Chart Table - 如果单元格为空,如何跳过超链接格式
【发布时间】:2020-01-09 02:46:23
【问题描述】:

我正在使用 google.visualization.PatternFormat 将列中的 url 格式化为 Google Chart Table 中的超链接。当前的方法将整列格式化为超链接。如果该列中的单元格为空,我想跳过格式。

  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%'
    });

由于该列是可选的,我想将单元格留空,而不是有一个无处可去的超链接。

【问题讨论】:

    标签: javascript google-visualization


    【解决方案1】:

    您基本上是在尝试将 FormatPattern 放入条件中(例如 if 语句)。

    我做了一些研究,看起来(至少 Ray 能够在 this aging question 中确定),您可能需要遍历行以有条件地应用格式,因为 PatternFormat 文档没有似乎没有为此提供一种机制。

    幸运的是,DataTable Class 文档为我们提供了一些可能对您的用例有所帮助的函数,我希望您的代码可能会使用 getFilteredRowssetFormattedValue,例如:

    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);
    }
    

    此代码将在您的模式应用于所有行后运行,目的是从目标单元格值为空字符串的任何行中删除新应用的格式(因为我希望 Google 返回一个空字符串作为任何空单元格的值;如果这些单元格的返回值实际上是其他值,例如 null,我们显然希望将该值提供给 getFilteredRows。)

    请注意,此代码未经测试(实际上只是初步猜测),但希望能让您更接近解决方案。

    【讨论】:

    • 这是正确的方法,如果最初加载了null,则该值将返回null。但是,行索引不一定等于从getFilteredRows 返回的数组的索引——需要使用setFormattedValue(emptyRowInds[rowInd]——而不是——setFormattedValue(rowInd
    • 谢谢@WhiteHat。相应地进行了编辑。
    • 谢谢。有效。我修改为与 null var emptyRowInds = data.getFilteredRows([{column: 7, value: null}]); 进行比较
    • 太棒了。如果您愿意,请将答案标记为已接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    相关资源
    最近更新 更多