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

如何使用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.

我使用chart.js来显示一些数据,当代码是这样的时候一切正常: p>

JavaScript / Blade p>

  var pieDataAssignments = [
 
 @for($ i = 0; $ i&lt; count($ name)  ; $ i ++)
 
 {
 value:{!!  count(array_filter($ ass_c,create_function('$ a','return $ a ==''。$ name [$ i]。'“;')))!!},
 color:'pink',\  n突出显示:“#eaa5a2”,
标签:'主题'
},
 
 @endfor 
 
]; 
  code>  pre> 
 
 

然后我 我认为它正在工作,我想开始定制饼图。 p>

首先我尝试单独插入每个变量的值,例如 p>

  color:{!!  $ color [$ i] !!},
etc ... 
  code>  pre> 
 
 

这只是阻止饼图出现,这让我思考了我的失败。 p>

然后我尝试将所有内容插入到第一个Blade调用中,如下所示: p>

Blade p>

  {!!'
 
 {
 value:'。count(array_filter($ ass_c,create_function('$ a','return $ a ==''。$ name [$ i]。'“;  ')))。',
 color:“'。$ color [$ i]。'”,
高亮显示:“#eaa5a2”,
标签:“hi”
},
 
'!  !} 
  code>  pre> 
 
 

但是,它仍然没有显示出来。 p>

如何循环JavaScript代码并同时传递php变量? 我不知道还有什么可以尝试的,除了告诉我完全按照我的尝试做什么外,我在网上找不到任何东西。 p>

提前谢谢。 p> DIV>

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

];