简体   繁体   English

使用Knockout.js进度栏值未绑定到数据绑定值属性

[英]Progress bar value not bound to data-bind value attribute using Knockout.js

I have a progress bar, and I added a condition that, if the value is below 100, the color should be red; 我有一个进度条,并且添加了一个条件,如果该值小于100,则颜色应为红色; and if it is 100, the color should change to green. 如果是100,则颜色应变为绿色。 But when I debug the code I see the progress value return 0 even if some value is assigned to value attribute. 但是,当我调试代码时,即使将某些值分配给value属性,我也会看到进度值返回0。

Below is the progress bar code, and I am assigning value 100 within the data-bind attribute. 以下是进度条码,我在data-bind属性中分配了值100。

 <div class="card-header float-right" style="width:50%; padding-left:100px;"> <progress id="myProgress" data-bind="attr: { value: 100, }" min="0" max="100"></progress> </div> 

Below is the javascript code; 下面是javascript代码; when I debug and check progress value, it displays 0. 当我调试并检查进度值时,它显示为0。

 $(document).ready(function () { var progressvalue = $("#myProgress").val(); var progressText = $("#myProgress").text(); if (progressvalue == 100) { $('#myProgress').addClass("progress-100"); $('#myProgress').removeClass("progress-below"); } else { $('#myProgress').addClass("progress-below"); $('#myProgress').removeClass("progress-100"); } }); 

You do not need the data-bind attribute from what I can see in your current code (though you may have a reason outside of what you've shown). 我不需要在当前代码中看到的data-bind属性(尽管您可能有超出所显示内容的原因)。 Edit: For Knockout.js, you will of course need the data-bind attribute. 编辑:对于Knockout.js,您当然需要data-bind属性。

All you need to do is set a value for your <progress> element, and then make sure you're updating it as whatever process it represents is progressing . 您所需要做的就是为<progress>元素设置一个value ,然后确保在它表示正在进行的任何过程中都对其进行更新。

<progress id="myProgress" value="10" max="100"></progress>

Edit: For Knockout.js , you need to tie your data-bind attributes to a ViewModel field, and that ViewModel field would need to be associated with an Observable. 编辑:对于Knockout.js ,您需要将data-bind属性data-bindViewModel字段,并且该ViewModel字段需要与Observable关联。

You could do something like this (a toy example that just increments the progress value by 10 each second, but shows how you could update your ViewModel, as well as trigger a class change after reaching a threshold value): 您可以执行以下操作(一个玩具示例,该示例仅将进度值每秒增加10,但显示了如何更新ViewModel以及在达到阈值后触发类更改):

<div class="card-header float-right" style="width:50% ; padding-left:100px">
<progress
      id="myProgress"
      data-bind="value: progressValue, class: progressClass"
      max="100"
></progress>
</div>

<script>
      const INITIAL_PROGRESS = 1;
      const PROGRESS_THRESHOLD = 90;
      const BELOW_THRESHOLD_CLASS = "progress-below";
      const ABOVE_THRESHOLD_CLASS = "progress-100";

      let progressViewModel = {
        progressValue: ko.observable(INITIAL_PROGRESS),
        progressClass: ko.observable(BELOW_THRESHOLD_CLASS)
      };

      ko.applyBindings(
        progressViewModel,
        document.getElementById("myProgress")
      );

      ko.when(
        function() {
          return progressViewModel.progressValue() > PROGRESS_THRESHOLD;
        },
        function(result) {
          progressViewModel.progressClass(ABOVE_THRESHOLD_CLASS);
        }
      );

      for (let i = 1; i < 11; i++) {
        window.setTimeout(function() {
          progressViewModel.progressValue(i * 10);
        }, i * 1000);
      }

</script>

Then you have to figure out how you want your Observable value(s) updated. 然后,您必须弄清楚如何更新可观测值。

If you're using KnockoutJS, the actual progress is probably an observable property on your viewmodel. 如果您使用的是KnockoutJS,则实际进度可能是viewmodel上的一个可观察属性。 Let's say it is actually called progress , then you would bind to it like this: 假设它实际上称为progress ,那么您将像这样绑定它:

<div class="card-header float-right" style="width:50% ; padding-left:100px">
    <progress id="myProgress" data-bind="attr: { value: progress }" min="0" max="100"></progress>
</div>

For styling, you should use the css or class bindings to assign a class to the progress bar, based on the progress. 对于样式,应使用cssclass绑定根据进度将类分配给进度栏。 Here's a Fiddle with a little example, although I've used the Bootstrap progress element and not the actual native progress element since it's a nightmare to style: https://jsfiddle.net/thebluenile/20zsqn5w/ 这是一个小提琴,上面有一个小例子,尽管我使用了Bootstrap进度元素而不是实际的本机进度元素,因为这是样式的噩梦: https : //jsfiddle.net/thebluenile/20zsqn5w/

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

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