javascript 更新 Google Visualization DataTable 中的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13365618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating Data in Google Visualization DataTable
提问by Josh Johnson
How do you update data in a google visualization datatable? Example:
你如何更新谷歌可视化数据表中的数据?例子:
var data = new google.visualization.DataTable();
data.addColumn('string', 'Name');
data.addColumn('string', 'Occupation');
data.addRow(['Bob', 'Shoe Wearer']);
data.addRow(['Henry', 'Transformer']);
data.addRow(['Betty', 'Seltzer Connoisseur']);
// Time passes and Bob changes jobs:
data.addRow(['Bob', 'Beach Comber']);
Of course, that adds a new row and now I have two Bobs. How can I update Bob's occupation?
当然,这增加了一个新行,现在我有两个 Bob。如何更新 Bob 的职业?
回答by HansMaulwurf
As Bob is the first row you inserted, his occupation resides in row with index 0 and column with index 1:
由于 Bob 是您插入的第一行,因此他的职业位于索引为 0 的行和索引为 1 的列中:
data.setValue(0, 1, 'Beach Comber');
In case you don't know the row index of a person who's occupation is to be updated, I suggest iterating or filtering to find all Bob rows (or the oneBob row in your case). Iteration is the 'brute force' way and goes like this
如果您不知道要更新职业的人的行索引,我建议迭代或过滤以查找所有 Bob 行(或您的情况下的一个Bob 行)。迭代是“蛮力”方式,就像这样
for (var y = 0, maxrows = data.getNumberOfRows(); y < maxrows; y++) {
if (data.getValue(y, 0) == 'Bob') {
data.setValue(y, 1, 'Beach Comber');
}
}
Filtering is more elegant:
过滤更优雅:
var foundRows = data.getFilteredRows([{column: 0, value: 'Bob'}]);
for (var y = 0, maxrows = foundRows.length; y < maxrows; y++) {
data.setValue(foundRows[y], 1, 'Beach Comber');
}
The reference API-Doc can be found here: https://developers.google.com/chart/interactive/docs/reference#DataTableand holds a bunch a good examples.
可以在此处找到参考 API-Doc:https: //developers.google.com/chart/interactive/docs/reference#DataTable并提供了很多很好的示例。