javascript - 如何在谷歌仪表板表中为 TR 赋值

标签 javascript jquery google-visualization google-datatable

我正在使用 Google Dashboard 的表格功能,我想将 id 的值分配给创建的每一行的 tr,我有如下数组

   var data = google.visualization.arrayToDataTable([
      ['Name', 'Donuts eaten','id'],
      ['Michael' , 5,'1'],
      ['Elisa', 7,'2'],
      ['Robert', 3,'3'],
      ['John', 2,'4'],
      ['Jessica', 6,'5'],
      ['Aaron', 1,'6'],
      ['Margareth', 8,'7']
    ]);

      var table = new google.visualization.ChartWrapper({
                    'chartType': 'Table',
                    'containerId': 'chart2',
                    dataTable: data,
                    'options': {
                        'width': '500px'
                    }
                });
                table.draw();

我仍然无法弄清楚是否有某种方法可以将值绑定(bind)到 DOM 元素,我们将不胜感激。

最佳答案

这是一个解决方案,使用 JQuery:

google.visualization.events.addListener(table, 'ready', function () {
  $('#chart2 .google-visualization-table-tr-odd, #chart2 .google-visualization-table-tr-even').each(function (e) {
      var idx = $('td:nth-child(2)', this).html();
      $(this).attr('id', 'row' + idx);
    });
  });

并删除 ID 列:

google.visualization.events.addListener(table, 'ready', function () {
  $('.google-visualization-table-tr-head td:nth-child(3)').remove();
  $('#chart2 .google-visualization-table-tr-odd, #chart2 .google-visualization-table-tr-even').each(function (e) {
      var td = $('td:nth-child(2)', this)
      var idx = td.html();
      td.remove();
      $(this).attr('id', 'row' + idx);
    });
  });

考虑到排序:

function tableCleanUp() {
  google.visualization.events.addListener(table.getChart(), 'sort', tableCleanUp2);
  tableCleanUp2();
}

function tableCleanUp2() {
  $('.google-visualization-table-tr-head td:nth-child(3)').remove();
  $('#chart2 .google-visualization-table-tr-odd, #chart2 .google-visualization-table-tr-even').each(function (e) {
      var td = $('td:nth-child(2)', this)
      var idx = td.html();
      td.remove();
      $(this).attr('id', 'row' + idx);
    });
}

google.visualization.events.addListener(table, 'ready', tableCleanUp);

关于javascript - 如何在谷歌仪表板表中为 TR 赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20659635/

相关文章:

charts - 无法向谷歌条形图添加注释

javascript - 上传图片到火力地堡

java - 检测文件是否准备就绪并提供下载服务

javascript - 如何增加 Google 时间线图表的行高?

javascript - 使用 Fancybox 显示每个 div 的特定内容,

jquery - 如何获取与 JQuery 选择器匹配的特定元素的 ID

javascript - Google Charts Sankey - 节点文本样式

javascript - 警报来自 jquery post 的 ID

javascript - 使用 jquery 根据 id 选择表格单元格

javascript - 在 Firefox 上使用 popState 平滑滚动和返回按钮 - 需要点击两次