Wednesday, June 5, 2024
65
rated 0 times [  71] [ 6]  / answers: 1 / hits: 15907  / 12 Years ago, tue, november 13, 2012, 12:00:00

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?


More From » google-visualization

 Answers
5

As Bob is the first row you inserted, his occupation resides in row with index 0 and column with index 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 one Bob row in your case). Iteration is the 'brute force' way and goes like this



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#DataTable and holds a bunch a good examples.


[#82008] Monday, November 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
faithemilys

Total Points: 418
Total Questions: 100
Total Answers: 114

Location: North Korea
Member since Fri, Nov 4, 2022
2 Years ago
faithemilys questions
Wed, Mar 31, 21, 00:00, 3 Years ago
Thu, Oct 15, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Sun, Feb 9, 20, 00:00, 4 Years ago
Fri, Dec 6, 19, 00:00, 5 Years ago
;