当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - building where clause based on google visualization category picker control

问题描述:

According to this link, we can execute a function based on Google visualization category picker control. Now I am trying to build same type of where clause based on three controls as given below.

var whereClause1;

var control1, control2, control3;

function drawVisualization() {

// etc.

control1 = // etc...

control2 = // etc...

control3 = // etc...

// Register to hear state changes.

google.visualization.events.addListener(control1, 'statechange', foo1);

google.visualization.events.addListener(control2, 'statechange', foo2);

google.visualization.events.addListener(control3, 'statechange', foo3);

// etc.

}

function foo1() {

var whereClauses = [];

if (control1.getState().selectedValues == '') {

whereClauses.push("Something")

}

else if (control1) {

whereClauses.push("Something1 = '" +control1.getState().selectedValues+ "'")

}

whereClause1 = whereClauses;

dothis();

}

function foo2() {

var whereClauses = [];

if (control2.getState().selectedValues == '') {

whereClauses.push("Something1 = '" +control1.getState().selectedValues+ "'")

}

else if (control2) {

whereClauses.push("Something2 = '" +control2.getState().selectedValues+ "'")

}

whereClause1 = whereClauses;

dothis();

}

function foo3() {

var whereClauses = [];

if (control3.getState().selectedValues == '') {

whereClauses.push("Something2 = '" +control2.getState().selectedValues+ "'")

}

else if (control3) {

whereClauses.push("Something3 = '" +control3.getState().selectedValues+ "'")

}

whereClause1 = whereClauses;

dothis();

}

function dothis() {

whereClause += " AND " + whereClause1;

}

How can I merge the three functions in to one single function foo using some loop?

EDIT : Find my sample fiddle here.

In the sample, when i choose 'A' in column 1, the where clause should take value column0 = 'Home' AND column1 = 'A' and when I choose 'bar' in column 2, the where clause should take value column0 = 'Home' AND column1 = 'A' AND column2 = 'bar' and if I choose 'bar' and '200' in column2 and column3 respectively, the where clause should take value column0 = 'Home' AND column2 = 'bar' AND column3 = '200' and so on.

Also it take reverse order too. That is, when I choose 'Choose a value' in column 2, the where clause should take value that is set in column 1 only. I am tried with the following:

function foo() {

var whereClauses = [];

if (control1.getState().selectedValues == '') { whereClauses.push("") }

if (control1) { whereClauses.push("Column1 = '" +control1.getState().selectedValues+ "'") }

if (control2.getState().selectedValues == '') { whereClauses.push("Column1 = '" +control1.getState().selectedValues+ "'") }

if (control2) { whereClauses.push("Column2 = '" +control2.getState().selectedValues+ "'") }

if (control3.getState().selectedValues == '') { whereClauses.push("Column2 = '" +control2.getState().selectedValues+ "'") }

if (control3) { whereClauses.push("Column3 = '" +control3.getState().selectedValues+ "'") }

whereClause1 = whereClauses.join(" AND ");

dothis();

}

But no use.

网友答案:

If I understand your requirements correctly, this function should do what you want:

function getWhereClause () {
    var selectedValues1 = control1.getState().selectedValues;
    var selectedValues2 = control2.getState().selectedValues;
    var selectedValues3 = control3.getState().selectedValues;
    var whereClauses = ["column0 = 'Home'"];

    if (selectedValues1.length) {
        whereClauses.push("Column1 = '" + selectedValues1[0] + "'");
    }
    if (selectedValues2.length) {
        whereClauses.push("Column2 = '" + selectedValues2[0] + "'");
    }
    if (selectedValues3.length) {
        whereClauses.push("Column3 = '" + selectedValues3[0] + "'");
    }
    var whereClause = whereClauses.join(' AND ');
    // do something with whereClause
    alert(whereClause);
}

See working example here: http://jsfiddle.net/asgallant/zrjnh/

分享给朋友:
您可能感兴趣的文章:
随机阅读: