简体   繁体   English

如何制作具有计算宽度的进度条

[英]How to make a progress bar which has a calculated width

I have an ASP.NET MVC 5 application and I would like to add a progress bar to every event in a list of events, and I would like the progress bar to have a width calculated from two properties of an Event class which is in a foreach loop. 我有一个ASP.NET MVC 5应用程序,我想向事件列表中的每个事件添加进度条,并且我希望进度条具有根据Event类的两个属性计算出的宽度。 foreach循环。 Should I add jQuery to the view to calculate the variable and then add it to the CSS style? 我应该在视图中添加jQuery以计算变量,然后将其添加到CSS样式中吗?

Here is the code of the view's relevant section: 这是视图相关部分的代码:

@model IEnumerable<Betgo.Models.Event>

<ul class="list-group">
@foreach (var events in Model)
{
    <li class="list-group-item">
        //other elements of the list item

        <div class="progress">
            <div class="progress-bar" role="progressbar" aria-valuenow="70"
                 aria-valuemin="0" aria-valuemax="100" style="width:40%">
                70%
            </div>
        </div>

        <hr />
    </li>
}

Thank you in advance! 先感谢您!

EDIT: 编辑:

public class Event
{
    //properties
    public double OddsA { get; set; }
    public double OddsB { get; set; }
}

width = OddsA/(OddsA + OddsB)*100;
@model IEnumerable<Betgo.Models.Event>

<ul class="list-group">
@foreach (var events in Model)
{
    var width = events.OddsA/(events.OddsA + events.OddsB)*100; 
    <li class="list-group-item">
        //other elements of the list item

        <div class="progress">
            <div class="progress-bar" role="progressbar" aria-valuenow="70"
                 aria-valuemin="0" aria-valuemax="100" style="width:@(width)%">
                70%
            </div>
        </div>

        <hr />
    </li>
}

you can use razor to calculate the width since you are rendering all bars on load, doing it like this 您可以使用剃刀来计算宽度,因为您要在加载时渲染所有钢筋,就像这样

@foreach (var event in Model)
{
<li class="list-group-item">
    //other elements of the list item

    <div class="progress">
        <div class="progress-bar" role="progressbar" aria-valuenow="70"
             aria-valuemin="0" aria-valuemax="100"
             style="width:@(event.OddsA/(event.OddsA + event.OddsB)*100)%">
            @(event.OddsA/(event.OddsA + event.OddsB)*100)%
        </div>
    </div>

    <hr />
</li>
}

Calculating the value at the beginning of the loop and using it in the style attribute of the progress bar did the work! 在循环开始时计算值,然后在进度条的style属性中使用它就可以了! Thank you for helping! 感谢您的帮助!

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

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