运行基于statechange的JavaScript函数 [英] Running a javascript function based on statechange
问题描述
按照 stackoverflow问题中的答案,我正在尝试运行以下代码。但是,我的功能只需要一个Google可视化事件。以下代码是否有效?或者如何在单个函数中处理多个statechange google可视化事件?
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....
}
推荐答案
从您的问题中不太清楚,但我认为您正在从CategoryPicker中选定的项目构建SQL查询到您的数据库。尽管是一个非常好的/危险的事情(构建SQL客户端,并将其发送到服务器),但是只要从CategoryPicker中抓取selectedItems并将其与AND相加即可。喜欢:
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.
这篇关于运行基于statechange的JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!