简体   繁体   English

如何使foreach循环成javascript?

[英]How to make a foreach loop into javascript?

I'm trying to make a Razor loop in Javascript. 我正在尝试使用Javascript进行Razor循环。 I've tried this code, but this wouldn't work. 我已经试过了这段代码,但这行不通。 Nothing is displayed into the web-page. 网页上没有任何显示。

Code: 码:

$(function () {
        $('#container').highcharts({
            title: {
                text: 'Monthly Average Temperature',
                x: -20 //center
            },
            subtitle: {
                text: 'Source: WorldClimate.com',
                x: -20
            },
            xAxis: {
                categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
            },
            yAxis: {
                title: {
                    text: 'Temperature (°C)'
                },
                plotLines: [{
                    value: 0,
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: '°C'
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'middle',
                borderWidth: 0
            },
            series: [
@{int i=0, compte = Model.statistique.Count;}
                @foreach (var item in Model.statistique) {
                    <text>
                        {
                            name: '@(item.nomProcessus)',
                            data: [@(item.lundi), @(item.mardi), @(item.mercredi), @(item.jeudi), @(item.vendredi)]
                        }
                    </text>
                    if (i < compte) {
                        @:,
                    }
                    else
                    {
                        @:]
                    }
                    i++;
                }
});
});

Original code: 原始代码:

jsFiddle jsFiddle

How do I to make the foreach razor loop works into it, please? 请问我如何使foreach剃须刀环进入其中?

Any brilliant idea? 有什么好主意吗?

Try this 尝试这个

   series: [
            @foreach(var item in Model)
            {
                @:{ name: '@item.nomProcessus', data: [@(item.lundi), @(item.mardi), @(item.mercredi), @(item.jeudi), @(item.vendredi)] },
            }
]

Note: You don't need to worry about that last coma. 注意:您不必担心最后的昏迷。 It is acceptable. 它是可以接受的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM