Running a javascript function based on statechange

妖精的绣舞 提交于 2020-01-07 04:08:17

问题


Following the answer in this stackoverflow question, I am trying to run the following code. But the myfunction takes only one google visualization event. So Is the following code is valid? Or how to handle multiple statechange google visualization events in a single function?

var categoryPicker1, categoryPicker2;
function drawVisualization() {
  // etc.
  categoryPicker1 = // etc...

  categoryPicker2 = // etc...

  // Register to hear state changes.
  google.visualization.events.addListener(categoryPicker1, 'statechange', myfunction);
  google.visualization.events.addListener(categoryPicker2, 'statechange', myfunction);

  // etc.
}

function myfunction() {
  var whereClauses = [];
if (categorypicker1) {
    whereClauses.push("something1 = '" + document.getElementsByClassName('goog-inline-block goog-menu-button-caption')[0].innerHTML + "'")
}
if (categorypicker2) {
    whereClauses.push("something2 = '" + document.getElementsByClassName('goog-inline-block goog-menu-button-caption')[1].innerHTML + "'")
}
whereClause = whereClauses.join(" AND ");

// do something....
}

回答1:


Not really clear from your question, but I assume you're building the SQL query to your database from the selected items in the CategoryPicker. Despite being an EXTREMELY bad/dangerous thing to do (building SQL client side, and sending it to a server), this should be possible by just grabbing the selectedItems from your CategoryPicker, and joining them with " AND ". Like:

values = categoryPicker1.getState().selectedValues;
values = values.concat(categoryPicker2.getState().selectedValues);
var args = values.map(function(_) { return "'" + _ + "'"; });
console.log(args.join(" AND "));

I wouldn't do this if I were you. I would pass the arguments up to the server, and remap them there (after appropriately filtering them, etc). Again this is very dangerous.



来源:https://stackoverflow.com/questions/17780678/running-a-javascript-function-based-on-statechange

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!