一页中有多个ajax请求+怎么做+最佳实践 [英] multiple ajax requests from one page + how to do + best practice
问题描述
我目前正在研究Google图表,并且已经完成了一个基本的设置.
I am working on google charts at the minute and I have got a basic one setup.
它目前所做的是连接到DB,并基于1个查询返回一个数据集.我想知道的是,如果我想向数据库绘制更多具有不同查询的图表,该怎么做?或最佳做法是什么?
What it does at present is connects to a DB and returns a dataset based on 1 query. I am wondering is, if I want to draw more charts with different queries to the database how do I do this? Or what is the best practice?
例如,一个查询已经存在一个连接,如何添加另一个查询,然后根据返回的内容绘制图表?
For instance, there is already one connection with one query, how can I add another query, and then draw the charts based on what is returned?
我知道这可能是一个广泛的问题,但是也许有人可以向我展示如何从数据库返回不同的查询/数据集?
I understand that might be a broad question, but maybe someone could show me how I would return a different query/dataset from the DB?
这是我的代码:
$(document).ready(function(){
console.log("hello world")
//alert("result")
$.ajax({
url:"data.php",
dataType : "json",
success : function(result) {
google.charts.load('current', {
'packages':['corechart','bar']
});
google.charts.setOnLoadCallback(function() {
console.log(result[0]["name"])
drawChart(result);
});
}
});
//add a 2nd call - will need a 2nd draw charts to draw the different dataset assuming it will be different
// - will need a 2nd data.php as the query will be different on the dataset
$.ajax({
url:"data.php",
dataType : "json",
success : function(result2) {
google.charts.load('current', {
'packages':['corechart','bar']
});
google.charts.setOnLoadCallback(function() {
console.log(result2[0]["name"])
drawChart(result2);
});
}
});
function drawChart(result) {
var data = new google.visualization.DataTable();
data.addColumn('string','Name');
data.addColumn('number','Quantity');
var dataArray=[];
$.each(result, function(i, obj) {
dataArray.push([ obj.name, parseInt(obj.quantity) ]);
});
data.addRows(dataArray);
var piechart_options = {
title : 'Pie Chart: How Much Products Sold By Last Night',
width : 400,
height : 300
}
var piechart = new google.visualization.PieChart(document
.getElementById('piechart_div'));
piechart.draw(data, piechart_options)
var columnchart_options = {
title : 'Bar Chart: How Much Products Sold By Last Night',
width : 400,
height : 300,
legend : 'none'
}
//var barchart = new google.visualization.BarChart(document
// .getElementById('barchart_div'));
//barchart.draw(data, barchart_options)
var chart = new google.charts.Bar(document.getElementById('columnchart_material'));
chart.draw(data, google.charts.Bar.convertOptions(columnchart_options));
} //have added this column chart but need to wrok out if it is best practice????
});
我从数据库查询中得到一个对象,但是我想知道如何从同一数据库连接返回更多/不同的数据集?例如,如果我想用此查询select * from product where name="Product1" OR name="Product2";
I am getting an object back from my DB query, but I want to know how to return more/different datasets from the same DB connection? For example what if I wanted to draw another chart with the dataset returned from this query select * from product where name="Product1" OR name="Product2";
0: Object { id: "1", name: "Product1", quantity: "2" }
1: Object { id: "2", name: "Product2", quantity: "3" }
2: Object { id: "3", name: "Product3", quantity: "4" }
3: Object { id: "4", name: "Product4", quantity: "2" }
4: Object { id: "5", name: "Product5", quantity: "6" }
5: Object { id: "6", name: "Product6", quantity: "11" }
对于我的PHP代码而言,如下所示:
For what it is worth my php code is as follows:
data.php
<?php
require_once 'database.php';
$stmt = $conn->prepare('select * from product');
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_OBJ);
echo json_encode($results);
?>
database.php
database.php
<?php
$conn = new PDO('mysql:host=192.168.99.100;dbname=demo','root', 'root');
?>
注意:这可能很有趣
推荐答案
每次加载一次Google图表只需加载一次,
并非每次您都需要绘制图表
google charts only needs to be loaded once per page load,
not every time you need to draw a chart
也可以使用google.charts.load
代替-> $(document).ready
它将在执行回调/Promise
also, google.charts.load
can be used in place of --> $(document).ready
it will wait for the page to load before executing the callback / promise
推荐类似于以下代码段的设置...
recommend setup similar to following snippet...
google.charts.load('current', {
packages: ['corechart', 'bar']
}).then(function () {
$.ajax({
url: 'data.php',
dataType: 'json'
}).done(drawChart1);
$.ajax({
url: 'data.php',
dataType: 'json'
}).done(drawChart2);
});
function drawChart1(result) {
...
}
function drawChart2(result) {
...
}
这篇关于一页中有多个ajax请求+怎么做+最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!