Google可视化:如何使用表调用并绘制顺序查询? [英] Google Visualization : How to call and draw sequential query with table?

查看:56
本文介绍了Google可视化:如何使用表调用并绘制顺序查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用2个不同的sql查询数据并排绘制一张表。我使用以下格式发送。但是,它将第一个查询数据绘制到第二个表格容器中,而不是第一个表格容器。

  var sqlQuery =sql?tq =选择Section,SubSection,Id,Question,Answer,Others,其中+ 
SubSection ='1.1'& sqlQueryID = questions_bank;
var query1 = new google.visualization.Query(sqlQuery);
TABLE_LOCATION ='tableProductDeploymentContainer';
query1.send(drawQuestions);
var sqlQuery =sql?tq = select其中+
SubSection ='1.2'& sqlQueryID = questions_bank;部分,子部分,Id,问题,答案,其他。
var query2 = new google.visualization.Query(sqlQuery);
TABLE_LOCATION ='tableProductDeploymentContainer';
query2.send(drawQuestions);
休息;

函数drawQuestions(queryResponse){
if(queryResponse.isError()){
alert('Error in query:'+ queryResponseData.getMessage()+''+ queryResponseData .getDetailedMessage());
return;
}
var questionBankResponse = queryResponse.getDataTable();
if(questionBankResponse === null){
alert('Empty rows in query:'+ questionBankResponse.getNumberOfRows());
return;
}
var questionDataTable = new google.visualization.DataTable();
questionDataTable.addColumn('string','');
questionDataTable.addColumn('string','');
questionDataTable.addColumn('string','');
var questionDataTableRow = new Array();
var rowCounter;
for(rowCounter = 0; rowCounter< questionBankResponse.getNumberOfRows(); rowCounter ++){
var count = 0 * 1;
var chbQuestion;
var questionId = questionBankResponse.getValue(rowCounter,2);
var questionName = questionBankResponse.getValue(rowCounter,3);
var answerValue = questionBankResponse.getValue(rowCounter,4);
var answerOthers = questionBankResponse.getValue(rowCounter,5);
if(answerOthers!== null)
answerOthers = answerOthers.toString();
if(answerValue === null)
answerValue = 0;
if(answerValue.toString()===1)
chbQuestion =< input type = \checkbox \+id = \+ questionId + \+checked />;
else
chbQuestion =< input type = \checkbox \+id = \+ questionId +\+/>;
questionDataTableRow [count ++] = chbQuestion;
questionDataTableRow [count ++] = questionName;
questionDataTableRow [count ++] = answerOthers;
questionDataTable.addRow(questionDataTableRow);
}
var tableObject = new google.visualization.Table(document.getElementById(TABLE_LOCATION));
tableObject.draw(questionDataTable,{allowHtml:true,'cssClassNames':cssClasses,width:'100%',sort:'disable'});
}

我相信,在设置TABLE_LOCATION全局变量时会出现一些错误。有没有什么办法可以动态地传递表格容器,而不需要在全球范围内维护。



感谢您的帮助。

Query.send 的回调是异步调用的,因此
不能保证一个在另一个之前完成



正如你指出的那样,在回调中发送表id,而不是使用全局范围...



见下面的代码片段...

  var sqlQuery =sql?tq = select Section,SubSection,Id,Question,Answer,其他地方+ 
SubSection ='1.1'& sqlQueryID = questions_bank;
var query1 = new google.visualization.Query(sqlQuery);
$ b query1.send(function(queryResponse){
drawQuestions(queryResponse,'tableProductDeploymentContainer');
});

var sqlQuery2 =sql?tq = select Section,SubSection,Id,Question,Answer,Others其中+
SubSection ='1.2'& sqlQueryID = questions_bank;
var query2 = new google.visualization.Query(sqlQuery2);
$ b query2.send(function(queryResponse){
drawQuestions(queryResponse,'tableProductDeploymentContainer2');
});

函数drawQuestions(queryResponse,TABLE_LOCATION){
if(queryResponse.isError()){
alert('Error in query:'+ queryResponseData.getMessage()+'' + queryResponseData.getDetailedMessage());
return;
}
var questionBankResponse = queryResponse.getDataTable();
if(questionBankResponse === null){
alert('Empty rows in query:'+ questionBankResponse.getNumberOfRows());
return;
}
var questionDataTable = new google.visualization.DataTable();
questionDataTable.addColumn('string','');
questionDataTable.addColumn('string','');
questionDataTable.addColumn('string','');
var questionDataTableRow = new Array();
var rowCounter;
for(rowCounter = 0; rowCounter< questionBankResponse.getNumberOfRows(); rowCounter ++){
var count = 0 * 1;
var chbQuestion;
var questionId = questionBankResponse.getValue(rowCounter,2);
var questionName = questionBankResponse.getValue(rowCounter,3);
var answerValue = questionBankResponse.getValue(rowCounter,4);
var answerOthers = questionBankResponse.getValue(rowCounter,5);
if(answerOthers!== null)
answerOthers = answerOthers.toString();
if(answerValue === null)
answerValue = 0;
if(answerValue.toString()===1)
chbQuestion =< input type = \checkbox \+id = \+ questionId + \+checked />;
else
chbQuestion =< input type = \checkbox \+id = \+ questionId +\+/>;
questionDataTableRow [count ++] = chbQuestion;
questionDataTableRow [count ++] = questionName;
questionDataTableRow [count ++] = answerOthers;
questionDataTable.addRow(questionDataTableRow);
}
var tableObject = new google.visualization.Table(document.getElementById(TABLE_LOCATION));
tableObject.draw(questionDataTable,{allowHtml:true,'cssClassNames':cssClasses,width:'100%',sort:'disable'});
}


I have to draw a side by side table with 2 different sql query data. I am sending in the given below format. However, it was drawn first query data into second table container instead with first table container.

 var sqlQuery = "sql?tq=select Section, SubSection, Id, Question, Answer, Others where " +
                                        "SubSection = '1.1' &sqlQueryID=questions_bank";
                        var query1 = new google.visualization.Query(sqlQuery);
                        TABLE_LOCATION = 'tableProductDeploymentContainer';
                        query1.send(drawQuestions);
                        var sqlQuery = "sql?tq=select Section, SubSection, Id, Question, Answer, Others where " +
                                       "SubSection = '1.2' &sqlQueryID=questions_bank";
                        var query2 = new google.visualization.Query(sqlQuery);
                        TABLE_LOCATION = 'tableProductDeploymentContainer';
                        query2.send(drawQuestions);
                        break;

function drawQuestions(queryResponse) {
            if (queryResponse.isError()) {
                alert('Error in query: ' + queryResponseData.getMessage() + ' ' + queryResponseData.getDetailedMessage());
                return;
            }
            var questionBankResponse = queryResponse.getDataTable();
            if (questionBankResponse === null) {
                alert('Empty rows in query: ' + questionBankResponse.getNumberOfRows());
                return;
            }
            var questionDataTable = new google.visualization.DataTable();
            questionDataTable.addColumn('string', '');
            questionDataTable.addColumn('string', '');
            questionDataTable.addColumn('string', '');
            var questionDataTableRow = new Array();
            var rowCounter;
            for (rowCounter = 0; rowCounter < questionBankResponse.getNumberOfRows() ; rowCounter++) {
                var count = 0 * 1;
                var chbQuestion;
                var questionId = questionBankResponse.getValue(rowCounter, 2);
                var questionName = questionBankResponse.getValue(rowCounter, 3);
                var answerValue = questionBankResponse.getValue(rowCounter, 4);
                var answerOthers = questionBankResponse.getValue(rowCounter, 5);
                if (answerOthers !== null)
                    answerOthers = answerOthers.toString();
                if (answerValue === null)
                    answerValue = 0;
                if (answerValue.toString() === "1")
                    chbQuestion = "<input type=\"checkbox\"" + " id=\"" + questionId + "\"" + " checked />";
                else
                    chbQuestion = "<input type=\"checkbox\"" + " id=\"" + questionId + "\"" + " />";
                questionDataTableRow[count++] = chbQuestion;
                questionDataTableRow[count++] = questionName;
                questionDataTableRow[count++] = answerOthers;
                questionDataTable.addRow(questionDataTableRow);
            }
            var tableObject = new google.visualization.Table(document.getElementById(TABLE_LOCATION));
            tableObject.draw(questionDataTable, { allowHtml: true, 'cssClassNames': cssClasses, width: '100%', sort: 'disable' });
        }

I believe, there is some error in setting the TABLE_LOCATION global variable. Is there any way to pass the table container dynamically without maintaining in the global level.

Thanks for your help.

解决方案

since the callback for Query.send is called asynchronously,
cannot guarantee one finishes before the other

as you point out, send the table id within the callback, rather than using global scope...

see following snippet...

var sqlQuery = "sql?tq=select Section, SubSection, Id, Question, Answer, Others where " +
               "SubSection = '1.1' &sqlQueryID=questions_bank";
var query1 = new google.visualization.Query(sqlQuery);

query1.send(function (queryResponse) {
  drawQuestions(queryResponse, 'tableProductDeploymentContainer');
});

var sqlQuery2 = "sql?tq=select Section, SubSection, Id, Question, Answer, Others where " +
               "SubSection = '1.2' &sqlQueryID=questions_bank";
var query2 = new google.visualization.Query(sqlQuery2);

query2.send(function (queryResponse) {
  drawQuestions(queryResponse, 'tableProductDeploymentContainer2');
});

function drawQuestions(queryResponse, TABLE_LOCATION) {
    if (queryResponse.isError()) {
        alert('Error in query: ' + queryResponseData.getMessage() + ' ' + queryResponseData.getDetailedMessage());
        return;
    }
    var questionBankResponse = queryResponse.getDataTable();
    if (questionBankResponse === null) {
        alert('Empty rows in query: ' + questionBankResponse.getNumberOfRows());
        return;
    }
    var questionDataTable = new google.visualization.DataTable();
    questionDataTable.addColumn('string', '');
    questionDataTable.addColumn('string', '');
    questionDataTable.addColumn('string', '');
    var questionDataTableRow = new Array();
    var rowCounter;
    for (rowCounter = 0; rowCounter < questionBankResponse.getNumberOfRows() ; rowCounter++) {
        var count = 0 * 1;
        var chbQuestion;
        var questionId = questionBankResponse.getValue(rowCounter, 2);
        var questionName = questionBankResponse.getValue(rowCounter, 3);
        var answerValue = questionBankResponse.getValue(rowCounter, 4);
        var answerOthers = questionBankResponse.getValue(rowCounter, 5);
        if (answerOthers !== null)
            answerOthers = answerOthers.toString();
        if (answerValue === null)
            answerValue = 0;
        if (answerValue.toString() === "1")
            chbQuestion = "<input type=\"checkbox\"" + " id=\"" + questionId + "\"" + " checked />";
        else
            chbQuestion = "<input type=\"checkbox\"" + " id=\"" + questionId + "\"" + " />";
        questionDataTableRow[count++] = chbQuestion;
        questionDataTableRow[count++] = questionName;
        questionDataTableRow[count++] = answerOthers;
        questionDataTable.addRow(questionDataTableRow);
    }
    var tableObject = new google.visualization.Table(document.getElementById(TABLE_LOCATION));
    tableObject.draw(questionDataTable, { allowHtml: true, 'cssClassNames': cssClasses, width: '100%', sort: 'disable' });
}

这篇关于Google可视化:如何使用表调用并绘制顺序查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆