簡體   English   中英

如何將php數組塊傳遞給jquery函數?

[英]How can pass php array chunk to jquery function?

我正在嘗試將php數組傳遞給我的jQuery函數,我已經完成並正常工作,並且遇到格式問題。 以下是詳細信息。

默認功能

drawHeroArea : function () {

    !verboseBuild || console.log('proton.graphsStats.drawHeroArea()');
    if($('#hero-area').length)

    proton.graphsStats.graph.Area = Morris.Area({

        element: 'hero-area',

        data: [

          {period: '2010Apr Q1', iphone: 2666, ipad: null, itouch: 2647},

          {period: '2010 Q2', iphone: 2778, ipad: 2294, itouch: 2441},

          {period: '2010 Q3', iphone: 4912, ipad: 1969, itouch: 2501},

          {period: '2010 Q4', iphone: 3767, ipad: 3597, itouch: 5689},

          {period: '2011 Q1', iphone: 6810, ipad: 1914, itouch: 2293},

          {period: '2011 Q2', iphone: 5670, ipad: 4293, itouch: 1881},

          {period: '2011 Q3', iphone: 4820, ipad: 3795, itouch: 1588},

          {period: '2011 Q4', iphone: 15073, ipad: 5967, itouch: 5175},

          {period: '2012 Q1', iphone: 10687, ipad: 4460, itouch: 2028},

          {period: '2012 Q2', iphone: 8432, ipad: 5713, itouch: 1791}

        ],

        xkey: 'period',

        ykeys: ['iphone', 'ipad', 'itouch'],

        labels: ['iPhone', 'iPad', 'iPod Touch'],

        pointSize: 2,

        hideHover: 'auto'

    });

},

我想在此函數中傳遞數據

 $LoopD['mydata'] = array();
 foreach($tData as $mon=>$val){ 

        $LoopD[]  =  array(

        'period' => $mon, 
        'iphone' => $val['amber'], 
        'ipad' =>  $val['red'],
        'itouch' => $val['green'],
            );

}
$jsonL  = json_encode($LoopD);  

使用Javascript:

var jsonL       = $jsonL;

$(document).ready(function(e) {
            drawHeroArea(jsonL);

                });

我正在將數據傳遞給功能

drawHeroArea : function (jsonL) {

        !verboseBuild || console.log('proton.graphsStats.drawHeroArea()');
        if($('#hero-area').length)

        proton.graphsStats.graph.Area = Morris.Area({

            element: 'hero-area',

            data: [
            jsonL
],

            xkey: 'period',

            ykeys: ['iphone', 'ipad', 'itouch'],

            labels: ['iPhone', 'iPad', 'iPod Touch'],

            pointSize: 2,

            hideHover: 'auto'

        });

    }

到目前為止,我已經嘗試過了,但是無法找到結果,我想在我的jquery函數中使用數據民用格式。

{period: '2010 Q2', iphone: 2778, ipad: 2294, itouch: 2441}

jsonL已經是一個數組,因此在您的drawHeroArea而不是這樣:

data: [
    jsonL
],

用這個:

data: jsonL

另外,請確保您替換以下內容:

$LoopD['mydata'] = array();

有了這個:

$LoopD = array();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM