dongtan6695 2015-10-21 16:13
浏览 569
已采纳

如何使用Blade传递PHP变量来遍历JavaScript代码

I am using chart.js to show some data, and everything was working fine when the code was this:

JavaScript/Blade

 var pieDataAssignments = [

                @for($i = 0;$i<count($name);$i++)

            {
                value: {!! count(array_filter($ass_c,create_function('$a','return $a=="'.$name[$i].'";')))!!},
                color: 'pink',
                highlight: "#eaa5a2",
                label: 'Subject'
            },

                @endfor

        ];

Then I thought it was working, which it was, and I thought to start customising the Pie Chart.

Firstly I tried inserting the value for each variable individually, e.g.

color: {!! $colour[$i] !!},
etc...

That just prevented the pie chart from appearing, which led me to ponder my failure..

I then tried to insert everything into the first Blade call, like so:

Blade

{!!'

{
    value:'.count(array_filter($ass_c,create_function('$a','return $a=="'.$name[$i].'";'))).',
    color: "'.$colour[$i].'",
    highlight: "#eaa5a2",
    label: "hi"
},

'!!}

But still, it did not show up.

How can I go about looping JavaScript code and passing php variables at the same time? I don't know what else to try and I can't find anything online apart from telling me to do exactly what I tried.

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • 啊啊啊小孔 2015-10-21 16:24
    关注

    I fixed the issue, the code I had was recreating the array inside it's own instance. The code that now works is the following:

    var pieDataAssignments = [
    
            @for($i = 0;$i<count($name);$i++)
    
            {!!'
    
            {
                value:'.count(array_filter($ass_c,create_function('$a','return $a=="'.$name[$i].'";'))).',
                color: "'.$colour[$i].'",
                highlight: "#eaa5a2",
                label: "'.$name[$i].'"
            },
    
            '!!}
    
    
    
            @endfor
    
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?