0

I'm trying to pass a variable to a google visualisation method but it keeps bombing out with an error if I use a variable but works fine if I type the string. What am I missing here?

This works:

var filterdata = new google.visualization.DataView(data)
filterdata.setRows(
  filterdata.getFilteredRows([{column: 0, value: 'someval'}])
);

This doesn't work:

filteredrows = "[{column: 0, value: 'someval'}]";

var filterdata = new google.visualization.DataView(data)
filterdata.setRows(
  filterdata.getFilteredRows(filteredrows)
);

The error I'm getting is:

"Uncaught Error: columnFilters must be a non-empty array"

EDIT:

I'm trying to build an array / string to pass all the dashboard control states as filter conditions for getfilteredrows(). Below is the code I'm using to build the array. I've also tried to create a string but either way it's not accepted...

control_states = [ctrl1.getState(),ctrl2.getState(),ctrl3.getState()];
var filteredrows = {};
var cnt = 0;
for (var i = 0; i < control_states.length; i++) {
  var picker_state = control_states[i]
  for (var j = 0; j < picker_state.selectedValues.length; j++) {
    filteredrows[cnt] = "column: " + i + ", value: '" + picker_state.selectedValues[j] + "'";
    cnt += 1;
  };
};

3 Answers 3

1

One is JSON, the other is an actual array.

Just write the first line as this

var filteredrows = [{column: 0, value: 'someval'}];

If your filteredrows is actually a string and you can't change that, use JSON.parse on it first

var filterdata = new google.visualization.DataView(data)
filterdata.setRows(
  filterdata.getFilteredRows(JSON.parse(filteredrows))
);
Sign up to request clarification or add additional context in comments.

1 Comment

I tried JSON parse but it still didn't work. I'll edit question to clarify what I'm doing
1

I guess that the method getFilteredRows accepts a valid array and not a string. I'll suggest to use JSON.parse before to send the string. I.e. something like:

filterdata.getFilteredRows(JSON.parse(filteredrows));

2 Comments

I edited question to give more info as I tried this and it didn't work
Aha ... I see. Then you should definitely use JSON.parse to convert your string to valid JavaScript object. After that you can pass it to the API's method.
0

Here is how I solved the problem, thanks to naomik and Krasimir for pointing me in the right direction!

control_states = [ctrl1.getState(),ctrl2.getState(),ctrl3.getState()];

var filteredrows = [ ];
var cnt = 0;
for (var i = 0; i < control_states.length; i++) {
  var picker_state = control_states[i]
  for (var j = 0; j < picker_state.selectedValues.length; j++) {
    filteredrows[cnt] = [ ];
    filteredrows[cnt]["column"] = i;
    filteredrows[cnt]["value"] = picker_state.selectedValues[j];
    cnt += 1;
  };
};

var filterdata = new google.visualization.DataView(data)
filterdata.setRows(
  filterdata.getFilteredRows(filteredrows)
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.