gpt4 book ai didi

javascript - 将按钮 (HTML) 插入 Google 可视化表格图表

转载 作者:太空狗 更新时间:2023-10-29 13:31:11 25 4
gpt4 key购买 nike

问题

使用 Google 图表工具可以将 HTML 值插入到字段值中,但是,我不确定这是一种好的做法。

我用过的方法如下:

function drawVisualization() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['Name', 'Height', 'Smokes','test'],
['Tong Ning mu', 174, true,'<input type="button" value="test" />'],
['Huang Ang fa', 523, false,'<input type="button" value="test" />'],
['Teng nu', 86, true,'<input type="button" value="test" />']
]);

// Create and draw the visualization.
visualization = new google.visualization.Table(document.getElementById('table'));
visualization.draw(data, {
allowHtml: true
});
}

我的问题

  1. 是否有其他更好的方法来实现这一目标?
  2. 有没有办法从这些按钮中捕获事件,同时还检索接收事件的行的 ID?换句话说,我如何知道点击了哪个按钮以及它代表哪一行?

最佳答案

Are there ways of catching the events from these buttons, whilst also retrieving the id of the row that is receiving the event? In other words, how do I know which button has been clicked and for which row it represents?

例如尝试这种方式

google.charts.load('current', {'packages':['table']});
google.charts.setOnLoadCallback(drawTable);
function getSelectedRowNumber(table) {
var selection = table.getSelection();
var message = '';

for (var i = 0; i < selection.length; i++) {
var item = selection[i];
if (item.row != null && item.column != null) {
message += '{row:' + item.row + ',column:' + item.column + '}';
} else if (item.row != null) {
message += '{row:' + item.row + '}';
} else if (item.column != null) {
message += '{column:' + item.column + '}';
}
}
if (message == '') {
message = 'nothing';
}
alert('You selected ' + message);
}
function drawTable() {
// Create and populate the data table.
var data = google.visualization.arrayToDataTable([
['Name', 'Height', 'Smokes','test'],
['Tong Ning mu', 174, true,'<input type="button" value="test" />'],
['Huang Ang fa', 523, false,'<input type="button" value="test" />'],
['Teng nu', 86, true,'<input type="button" value="test" />']
]);

// Create and draw the visualization.
var table = new google.visualization.Table(document.getElementById('table_div'));
table.draw(data, {
allowHtml: true,
showRowNumber: true,
width: '100%',
height: '100%',
cssClassNames: {tableRow: 'myClass'}
});
// this works for buttons
$(document).on('click','input[type=button]',function() {
getSelectedRowNumber(table);
});
// this works for clicking on row uncomment to test it
/*$(document).on('click','tr.myClass',function() {
getSelectedRowNumber(table);
});*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="table_div"></div>

关于javascript - 将按钮 (HTML) 插入 Google 可视化表格图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13231982/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com