我想使用justgage显示数据从mysql数据库,所以我所有的对象必须动态创建。我的请求正确检索所有我想要的数据,所以什么是包含在文件getData.php工作得很好。
当我想循环这个数据时,它不起作用,我不能检索每一行的所有数据,它给我像[object object] [object object] [object object] [object object],…
此外,我不知道如何使用我的动态数据来创建我的"新JustGage",然后,每5000ms刷新一次数据
有可能做我想做的吗?
getData.php
$link=connectBD();
$query="SELECT iod.ReadingValue, iod.Timestamp, io.Name AS ioName, io.OrangeAlarmVal, io.RedAlarmVal, r.Name AS radioName
FROM io_data iod
JOIN io ON io.ID_IO = iod.ID_IO
JOIN radio r ON r.IDRadio = io.IDRadio
WHERE iod.Timestamp = (SELECT MAX(iod2.Timestamp)
FROM io_data iod2
WHERE iod2.ID_IO=iod.ID_IO)";
$table = array();
$table['cols'] = array(
array('label' => 'ReadingValue', 'type' => 'number'),
array('label' => 'Timestamp', 'type' => 'string'),
array('label' => 'ioName', 'type' => 'string'),
array('label' => 'OrangeVal', 'type' => 'number'),
array('label' => 'RedVal', 'type' => 'number'),
array('label' => 'radioName', 'type' => 'string')
);
$result=selectData($link, $query);
$rows = array();
while($r = getRowElement($result)) {
$temp = array();
$temp[] = array('rv' => $r['ReadingValue']);
$temp[] = array('ts' => $r['Timestamp']);
$temp[] = array('ion' => $r['ioName']);
$temp[] = array('op' => $r['OrangeVal']);
$temp[] = array('rp' => $r['RedVal']);
$temp[] = array('rn' => $r['radioName']);
$rows[] = array('c' => $temp);
}
// populate the table with rows of data
$table['rows'] = $rows;
// encode the table as JSON
$jsonTable = json_encode($table);
// set up header; first two prevent IE from caching queries
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
// return the JSON data
echo $jsonTable;
other.js
$(document).ready(function(){
/** Get Data from database **/
var dataIO=getData();
$.each(dataIO, function(i, item) {
alert(item);
});
/*** This part should be generate within the loop with the retrieved fields (data between '**')
var maxGreen=**OrangeVal** - 0.1;
var minOrange=**OrangeVal**;
var maxOrange=**RedVal** - 0.1;
var minRed=**RedVal**;
**ioNameVal**=new JustGage({
id:"**ioNameVal**",
value:**ReadingValue**,
min:-50,
max:0,
title:"ioNameVal",
decimals: 1,
customSectors :[{"lo":-29.4,"hi":maxGreen,"color":"#a9d70b"},
{"lo":minOrange,"hi":maxOrange,"color":"#FF6A00"},
{"lo":minRed,"hi":0,"color":"#ff0000"}],
levelColorsGradient: false
});
// For each JustGage created, I need to setup a automatic reading in the
// database, so when the data change, the JustGage is refreshed
setInterval(function(){
**ioNameVal**.refresh(**ReadingValue**));
}
****/
}
function getData(){
var json = $.ajax({
url: 'getData.php',
dataType: 'json',
async: false
}).responseText;
data = $.parseJSON(json);
return data;
}
:现在,我可以从数据库中检索数据,并动态地生成所有的justgage对象。但我不确定这是好方法。我需要根据数据库中的新数据刷新我所有的仪表。还有别的办法吗?
other.js
/** Get Data from database **/
var dataIO=getData(3);
/*$.each(dataIO.rows, function(i, item) {
alert(item);
});*/
var idIO; var readingValue; var timestamp; var defaultIOType; var ioName; var orangePressure; var redPressure; var radioName;
var maxGreen; var minOrange; var maxOrange; var minRed;
var cpt=1;
$.each(dataIO.rows, function(i, object) {
$.each(object, function(j, val) {
// Pour chaque row
$.each(val, function(k, temp) {
//alert(k + "=" + temp);
switch(k){
case 'id':
idIO=temp;
break;
case 'rv': // reading value
readingValue=temp;
break;
case 'ts': // timestamp
timestamp=temp;
break;
case 'iot': // default IO type
readingdefaultIOType=temp;
break;
case 'ion': // IO Name
ioName=temp;
break;
case 'op': // orange pressure
orangePressure=temp;
if(orangePressure!=null){
maxGreen=orangePressure-0.1;
minOrange=orangePressure;
}
else{
maxGreen=-22.6;
minOrange=-22.5;
}
break;
case 'rp': // red pressure
redPressure=temp;
if(redPressure!=null){
maxOrange=redPressure-0.1;
minRed=redPressure;
}
else{
maxOrange=-20.1;
minRed=-20;
}
break;
case 'rn': // radio name
radioName=temp;
break;
}
});
eval('gauge_'+idIO+'= new JustGage({id:idIO,value:readingValue,min:-29.4,max:0,title:ioName,label:"inHg",decimals: 1,customSectors : [{"lo":-29.4,"hi":maxGreen,"color":"#a9d70b"},{"lo":minOrange,"hi":maxOrange,"color":"#FF6A00"},{"lo":minRed,"hi":0,"color":"#ff0000"}],levelColorsGradient: false})');
/*setInterval(function(){
eval('gauge_'+idIO).refresh(getDataRefresh(idIO));
},3000
);*/
cpt++;
});
});
setInterval(function(){
$.each(dataIO.rows, function(i, object) {
$.each(object, function(j, val) {
// Pour chaque row
$.each(val, function(k, temp) {
//alert(k + "=" + temp);
switch(k){
case 'id':
idIO=temp;
break;
}
});
eval('gauge_'+idIO).refresh(getDataRefresh(idIO));
});
});
},30000
);
function getData(typeToGet){
var json = $.ajax({
url: 'getData.php', // make this url point to the data file
dataType: 'json',
type:"POST",
data:{"typeToGet":typeToGet},
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
data = $.parseJSON(json);
return data;
}
function getDataRefresh(id){
var json = $.ajax({
url: 'getDataRefresh.php', // make this url point to the data file
dataType: 'json',
type:"POST",
data:{"id":id},
async: false
}).responseText;
// Create our data table out of JSON data loaded from server.
data = $.parseJSON(json);
return data;
}
-
您正在尝试将参数类型转换为字符串的
alert
项。你的项目对象没有自定义的toString()
方法,所以默认的字符串值是[object Object]
。
如果你想检查项目看起来像什么,尝试使用devtools/console。用console.log(item)
或console.dir(item)
代替alert(item)
;然后打开你的控制台(ctrl + shift + i在Chrome和Firefox)。
-
你想做的是可能的。你会想要阅读
setTimeout
。但是,老实说,轮询服务器获取数据并不是一个好的设计。