【问题标题】:Add Progress Bar's Class based on GridView column value: jQuery根据 GridView 列值添加进度条的类:jQuery
【发布时间】:2015-08-13 07:13:00
【问题描述】:

我有一个网格,其中有一个名为“C”的列,它指定了 C:\ 驱动器。在该列中,我在引导进度条中显示已用空间。

我已经完成了与gridview 的记录绑定。在这里,我想根据已用空间更改进度条类。假设,如果已用空间超过 90%,我想显示进度条危险,否则我想显示进度条信息。

The code, I have tried and the result I am getting is below:

<asp:TemplateField>
    <ItemTemplate>
         <div class="progress">
            <div class="progress-bar progress-bar-info progress-bar-striped active" id="progress-bar" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">
                 <%# Eval("C") %>
             </div>
         </div>
     </ItemTemplate>                                
 </asp:TemplateField>

 jQuery:

    <script type="text/javascript">

    $(function () {
        $(".progress").each(function () {
            var $this = $(this),
                value = parseInt($this.find('.progress-bar').text());
            $this.progressbar({
                value: value
            });
            // Here, "value" gives the used space value.
        });
    });

</script>

 Result:

如何根据值更改进度条类也对齐进度条文本对齐中心?请提出建议。

【问题讨论】:

    标签: jquery asp.net twitter-bootstrap gridview progress-bar


    【解决方案1】:

    只需通过检查值将progress-bar-danger 类添加到.each 循环中即可。

    $(function () {
      $(".progress").each(function (index, elem) {
        var $this = $(this),
            value = parseInt($this.find('.progress-bar').text());
            /*
            $this.progressbar({
                value: value
            });
            */
        if ( value > 90 ) {
          $(elem).find('.progress-bar').addClass('progress-bar-danger');
        }
        // Here, "value" gives the used space value.
      });
    });
    .container {
      margin-top: 40px;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    
    
    <div class="container">
      <div class="row">
        <div class="col-xs-4">
          <div class="progress">
            <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 60%;">
              60%
            </div>
          </div>
          <br />
          <div class="progress">
            <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 91%;">
              91%
            </div>
          </div>                
        </div>
      </div>
    </div>

    【讨论】:

    • 我在 gridview 中使用,它可能有更多行。但是这个并没有给我结果。
    • 抱歉,但我不明白为什么 gridview 会阻止您选择所有进度容器,因为这是您已经在做的事情。你能解释一下这是什么问题吗?
    • 在这里,我正在使用数据库中的数据表加载 gridview。数据表可能有超过 5 行。对于每一行,我需要根据“C”列的值显示进度条。我需要根据条件循环和更改进度条。
    猜你喜欢
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2021-05-31
    相关资源
    最近更新 更多