Re: StringFilter + Selection event

87 views
Skip to first unread message

asgallant

unread,
Jan 11, 2013, 2:59:40 PM1/11/13
to google-visua...@googlegroups.com
There isn't anything to select in a StringFilter control, perhaps what you want is the "statechange" event, which gets fired every time the user interacts with the control?

On Friday, January 11, 2013 8:44:27 AM UTC-5, david.t...@gmail.com wrote:
How can I add a select event to the StringFilter? (https://code.google.com/apis/ajax/playground/?type=visualization#stringfilter_control)

david.t...@gmail.com

unread,
Jan 12, 2013, 8:57:04 AM1/12/13
to google-visua...@googlegroups.com
Why not?
If for example I want to have a table of people (with name, id, etc.) and when clicking on someone I want something specific to happen (or at least be able to read the id, first name, etc)

asgallant

unread,
Jan 12, 2013, 11:43:55 AM1/12/13
to google-visua...@googlegroups.com
Oh, you want to implement a select event in your chart/table, not the control.  Here's example code you can plug into the playground code for the StringFilter control:

google.visualization.events.addListener(table'select'function ({
  var sel table.getChart().getSelection();
  var tmpData table.getDataTable();
  var msgArray ['You have selected:'];
  for (var 0sel.lengthi++{
    msgArray.push(tmpData.getValue(sel[i].row0));
  }
  alert(msgArray.join('\n'));
});

Put that in the code after you create the table and before you create the dashboard.

david.t...@gmail.com

unread,
Jan 13, 2013, 7:05:03 AM1/13/13
to google-visua...@googlegroups.com
Great, it works!
Thank you!

asgallant

unread,
Jan 13, 2013, 11:29:27 AM1/13/13
to google-visua...@googlegroups.com
You're welcome.

david.t...@gmail.com

unread,
Jan 20, 2013, 7:21:02 AM1/20/13
to google-visua...@googlegroups.com
There's a little bug there that I can't find. The exact code you posted..

When you click on someone I get an alert, if I click on it on the 2nd time (on the same line)  it's undefined, on the 3rd time (again on the same line) it's back again and so on..

Any idea why?

asgallant

unread,
Jan 20, 2013, 12:20:47 PM1/20/13
to google-visua...@googlegroups.com
If you click on a selected element, it deselects that element, but the "select" event is still fired.  You can test to see if anything is selected by checking the length of the array returned by table.getChart().getSelection():

google.visualization.events.addListener(table'select'function ({
  var sel table.getChart().getSelection();
  if (sel.length 0{

    var tmpData table.getDataTable();
    var msgArray ['You have selected:'];
    for (var 0sel.lengthi++{
      msgArray.push(tmpData.getValue(sel[i].row0));
    }
    alert(msgArray.join('\n'));
  }
});

david.t...@gmail.com

unread,
Jan 26, 2013, 7:02:06 AM1/26/13
to google-visua...@googlegroups.com
Can I deselect an element manually in code without requiring another click?
Because currently, the solution you offered requires 2 clicks on the same element when the first (deselect) does nothing and the 2nd one is the select one.

asgallant

unread,
Jan 26, 2013, 9:25:31 AM1/26/13
to google-visua...@googlegroups.com
Yes, you can deselect an element in code.  The #setSelection method takes an array of objects with "row" and/or "column" properties that identify the element(s) selected.  Remove the element you want to deselect from the array and pass the array back to #setSelection.  To deselect all elements, you can call #setSelection without passing any parameters.
Reply all
Reply to author
Forward
0 new messages