简体   繁体   English

Knockout.js:如何在另一个可观察数组的基础上更新属性

[英]Knockout.js: How to update property on the basis of another observable array

How i can change value of one column on the basis of another column value change.In the following code i want to update % column using marks columns value change. 我如何在另一列值更改的基础上更改一列的值。在以下代码中,我想使用标记列值更改来更新%列。

HTML: HTML:

<table data-bind="foreach: users()">
  <thead class="flip-content table-head">
    <tr>

      <th align="center">Name</th>
      <th align="center">Marks</th>
      <th align="center">% out of 100</th>
    </tr>
  </thead>
  <tr >
    <td data-bind="text: $data.name()"> </td>
    <td>
      <input data-bind="value: $data.marks()" />
    </td>
    <td data-bind='text:  $data.percent()'></td>

  </tr>
</table>

SCRIPT: 脚本:

var UserModel = function () {
  this.users = ko.observableArray([
    {
      id: 1,
      name: ko.observable('Bob'),
      marks: ko.observable(0),
      percent: ko.computed(function () {
        //percentage formula
      }, this)
    },
    {
      id: 2,
      name: ko.observable('Jane'),
      marks: ko.observable(0),
      percent: ko.computed(function () {
        //percentage formula
      }, this)
    }
  ]);
  this.selectedUser = ko.observable(this.users()[0]);
}
var userModel = new UserModel();
ko.applyBindings(userModel);

How i can do this. 我该怎么做。

The easiest and most common solution is to use "class" instances rather than plain objects. 最简单,最常见的解决方案是使用“类”实例而不是普通对象。 These allow you to easily select the right marks value because inside the constructor, this refers to the current user. 这些使您可以轻松选择正确的marks值,因为在构造函数内部, this指向当前用户。

 var User = function(id, name, marks) { this.id = id; this.name = name; this.marks = ko.observable(marks); this.percent = ko.pureComputed(function() { return this.marks() + "%"; }, this); } var UserModel = function () { this.users = ko.observableArray([ new User(1, "Bob", 10), new User(2, "Alice", 30), ]); this.selectedUser = ko.observable(this.users()[0]); } var userModel = new UserModel(); ko.applyBindings(userModel); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <table data-bind="foreach: users()"> <thead class="flip-content table-head"> <tr> <th align="center">Name</th> <th align="center">Marks</th> <th align="center">% out of 100</th> </tr> </thead> <tr > <td data-bind="text: name"> </td> <td> <input data-bind="value: marks" /> </td> <td data-bind='text: percent'></td> </tr> </table> 

If you absolutely need to use plain objects, you can define percent 's logic on your parent model and pass a reference to the current item in the view to create a computed on the fly. 如果您绝对需要使用普通对象,则可以在父模型上定义percent的逻辑,并将引用传递给视图中的当前项目以动态创建计算对象。 (I wouldn't recommend this) (我不会推荐这个)

 var UserModel = function () { this.users = ko.observableArray([ { id: 1, name: ko.observable('Bob'), marks: ko.observable(0) }, { id: 2, name: ko.observable('Jane'), marks: ko.observable(0) } ]); this.selectedUser = ko.observable(this.users()[0]) }; UserModel.prototype.percentageFor = function(user) { return user.marks() + "%"; }; var userModel = new UserModel(); ko.applyBindings(userModel); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <table data-bind="foreach: users()"> <thead class="flip-content table-head"> <tr> <th align="center">Name</th> <th align="center">Marks</th> <th align="center">% out of 100</th> </tr> </thead> <tr > <td data-bind="text: name"> </td> <td> <input data-bind="value: marks" /> </td> <td data-bind='text: $parent.percentageFor($data)'></td> </tr> </table> 

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

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