douyun7718 2014-06-26 04:21
浏览 49

如何在Javascript函数中嵌入JS的AJAX输出

It's becoming a nightmare for me solving this problem. I am developing an app using phonegap. I have used highcharts in it for Graphs. The graph is coming properly but not the tooltip in the graph. Please check the code below, I need this code to be able to show the tooltip with proper data. There has to be a loop of the following code and it has to be for every user or series in the graph. So I have ran a for loop in PHP and saved it in a variable and got the whole output of the loop using AJAX in a JSON format.

When I use the variable in the Javascript code in which I have saved the AJAX output, it just doesn't do anything but if I alert than it alerts the exact code which I want to make tooltip work. I have tried eval() also but no benefit. Any help or guidance will be much appreciated.

Loop Code

        $toolTip = '';

        for($i=0;$i<$teamControllerW->total_user;$i++){
            $toolTip .= "var chart = $('#team_containerWF').highcharts();";
            $toolTip .= "var figures".$i." = [".join($teamControllerW->new_yAxesData1[$i], ",") ."]; ";
            $toolTip .= "var fatVals".$i." = [". join($teamControllerW->fatVal[$i], ',') ."]; ";
            $toolTip .= "var weightVals".$i." = [". join($teamControllerW->weightVal[$i], ',') ."]; ";
            $toolTip .= "var result_playing_dates = [". join($teamControllerW->new_playing_dates_data[$i], ',') ."]; ";
            $toolTip .= "
            $.each(figures".$i.", function (j, figure".$i.") {
                 var curDanger = (figure".$i."[0] - danger[0][0]) * delta + danger[0][1];
                 var play_date = false;
                 for (var k = 0; k < result_playing_dates.length ; k ++ ) {
                    if (figure".$i."[0] == result_playing_dates[k]) {
                        play_date = true;
                        break;
                    }
                 }
                 ";
                 $toolTip .= "if (figure".$i."[1] > curDanger) {
                    if (play_date) {
                        if (chart.series[".$i."].data[j] && chart.series[".$i."].data[j].graphic) {
                            chart.series[".$i."].data[j].graphic.attr({ fill: '#a31515' });
                             chart.series[".$i."].data[j].update({
                             fatVal : fatVals".$i."[j],
                             weightVal : weightVals".$i."[j],
                             marker:{
                                fillColor: '#a31515',
                                radius: 6,
                                states: {
                                      hover: {
                                         fillColor: '#a31515',
                                         lineColor: '#a31515',
                                         radius: 10
                                      }
                                   }
                                }
                             });
                        }
                     }
                     else {
                        if (chart.series[".$i."].data[j] && chart.series[".$i."].data[j].graphic) {
                            chart.series[".$i."].data[j].graphic.attr({ fill: 'red' });
                            chart.series[".$i."].data[j].update({
                            fatVal : fatVals".$i."[j],
                            weightVal : weightVals".$i."[j],
                            marker:{
                               fillColor: 'red',
                               states: {
                                     hover: {
                                        fillColor: 'red',
                                        lineColor: 'red'                                    
                                     }
                                  }
                               }
                            });
                        }
                    }
                }else{
                    if (play_date) {
                        if (chart.series[".$i."].data[j] && chart.series[".$i."].data[j].graphic) {
                            chart.series[".$i."].data[j].graphic.attr({ fill: '#02491a' });
                            chart.series[".$i."].data[j].update({
                            fatVal : fatVals".$i."[j],
                            weightVal : weightVals".$i."[j],
                            marker:{
                                 fillColor: '#02491a',
                                 radius: 6,
                                 states: {
                                       hover: {
                                        fillColor: '#02491a',
                                        lineColor: '#02491a',
                                        radius: 10
                                       }
                                    }
                                 }
                            });
                        }
                     }
                     else {
                        if (chart.series[".$i."].data[j] && chart.series[".$i."].data[j].graphic) {
                            chart.series[".$i."].data[j].graphic.attr({ fill: 'green' });
                            chart.series[".$i."].data[j].update({
                            fatVal : fatVals".$i."[j],
                            weightVal : weightVals".$i."[j],
                            marker:{
                               fillColor: 'green',
                               states: {
                                     hover: {
                                        fillColor: 'green',
                                        lineColor: 'green'                                  
                                     }
                                  }
                               }
                            });
                        }
                    }
                 }
            });
            ";
      }//end for

I'm calling the data like this

    var teamweighinLMgameAction = window.localStorage.getItem("teamweighinLMgameAction");
    AGWeigh = JSON.parse(teamweighinLMgameAction);
    var toolTip = AGWeigh.toolTip;

    alert(toolTip);//On alert it comes fine

    if(data_avail=='yes') { //check if data available
        var danger = myDangerLine,
        deltaX = danger[1][0] - danger[0][0],
        deltaY = danger[1][1] - danger[0][1],
        delta = deltaY / deltaX;

        alert("In: "+toolTip);//the data also alerts fine here

        toolTip; //This is the place where I need the loop output but it nothing comes up

    }
  • 写回答

1条回答 默认 最新

  • duangutian1426 2014-06-26 04:38
    关注

    Ok this sounds like "Loop Code" being result of an AJAX call has passed js code "var chart = $('#team_containerWF').highcharts();..."!! However, this will never get executed by the main page. An AJAX result being passed should ideally be JSON/XML and then only contain name value pairs of data. Such that your main code being viewed would:

    1. validate those name value pairs
    2. execute its own code to render the graph based on those name value pairs

    Therefore, my recommendation is try to structure it that firstly Main page handles the logic for display in Javascript, and AJAX pages in PHP handle the logic for pulling data and not display.

    Also you could provide us with a bit more detail on the structure of your project. Label your code e.g. index.php getChartPlayingDates.php?

    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)