我想使用justgage来显示来自mysql数据库的数据,因此我的所有对象都必须动态创建。 我的请求正确检索了我想要的所有数据,因此getData.php文件中包含的内容效果很好。
当我想循环这些数据时,它不起作用,我不能用所有数据检索每一行,它给我一些像[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;
}
更新: 现在,我可以从数据库中检索我的数据并动态生成我的所有物品。但我不确定这是好方法。我需要根据数据库中的新数据刷新所有量表。还有另一种方法吗?
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;
}
答案 0 :(得分:1)
您正在尝试alert
将参数强制转换为字符串的项目。您的商品对象没有自定义toString()
方法,因此字符串值的默认值为[object Object]
。
如果要检查项目的外观,请尝试使用devtools / console。而不是alert(item)
,请使用console.log(item)
或console.dir(item)
;然后在Chrome和Firefox中打开你的控制台( ctrl + shift + i )。
你想做什么。您需要阅读setTimeout
。但是说实话,在服务器上查询数据并不是一个很好的设计。