基于Gridview列修改jQuery progressbar的颜色

时间:2013-08-08 10:49:05

标签: javascript jquery gridview progress-bar

我正在尝试根据gridview中每行中的boloean值修改进度条的颜色(布尔值)如果值为True,则颜色为绿色;如果值为False,则颜色为红色。但由于某种原因,设置颜色的条件不起作用!

提前致谢:

Asp GridView代码:

<Columns>
<asp:TemplateField>
    <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
    </ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="progress" HeaderText="Progress" SortExpression="progress" />
<asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
<asp:TemplateField>
<ItemTemplate>
   <div class="pbcontainer">
      <div class="progressbar"><span><%# Eval("progress") %>%</span></div>
      <div class="value" style="visibility:hidden; height:0; width:0;"> <%#         Eval("progress") %> </div>
      <div class="status" style="visibility:hidden; height:0; width:0;"> <%# Eval("status") %> </div>
  </div>
</ItemTemplate>
</asp:TemplateField>
</Columns>

脚本:

</style>

<link href="CSS/jquery-ui.css" rel="stylesheet" />
<script src="Scripts/jquery-1.9.1.js"></script>
<script src="Scripts/jquery-ui-1.10.3%20.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<script>
    $(function () {
        $('.pbcontainer').each(function () {
            var val = parseInt($(".value", this).text());
            var status = $(".status", this).text();

            var progressColor = "Orange";
            var progressBackColor = "lightYellow";

            console.log(status);
            if (status == "False") {
                progressColor = "Red";
                //console.log("Condition met color red status Flase");
            }
            else if (status == "True") {
                progressColor = "Green";
                //console.log(progressColor);
                //console.log("Condition not met color Green status True");
            }

            $('.progressbar', this).progressbar({ value: val });
            $('.progressbar', this).css({ 'background': progressBackColor });
            $('.progressbar > div', this).css({ 'background': progressColor });
        });
    });
</script>

2 个答案:

答案 0 :(得分:1)

由于状态周围有额外的空白区域,因此条件不正确。

您需要修改代码以修剪此空白区域

 var status = $.trim( $(".status", this).text() );

查看此演示 http://jsfiddle.net/aXTcV/1/

答案 1 :(得分:0)

试试这个:

<script type="text/javascript">
    $(function () {
        $('.pbcontainer').each(function () {
            var this_Progress = $(this);
            var val = parseInt($.trim(this_Progress.find('.value').text()));
            var status = $.trim(this_Progress.find('.status').text())

            var progressColor = "Orange";
            var progressBackColor = "lightYellow";

            console.log(status);
            if (status == "False") {
                progressColor = "Red";
                //console.log("Condition met color red status Flase");
            }
            else if (status == "True") {
                progressColor = "Green";
                //console.log(progressColor);
                //console.log("Condition not met color Green status True");
            }
            this_Progress.find('.progressbar').progressbar({ value: val });
            this_Progress.find('.ui-progressbar').css({ 'background': progressBackColor });
            this_Progress.find('.ui-progressbar-value').css({ 'background': progressColor });

        });
    });
</script>