简体   繁体   English

淘汰赛可观察到的数组更新不更新UI

[英]knockout observable array update not updating the UI

When I update an observable element of Knockout the UI is not getting update 当我更新Knockout的observable元素时,UI不会更新
HTML HTML

<tbody data-bind="foreach: students, visible: !students().isDeleted">
     <tr>
         <td data-bind="text: RollNo"></td>
         <td data-bind="text: Name"></td>
         <td data-bind="text: Phone"></td>
         <td data-bind="text: Email"></td>
         <td>
             <a href="#" data-bind="click: $root.editStudent">Edit</a>
             <a href="#" data-bind="click: $root.deleteStudent">Delete</a>
         </td>
     </tr>
</tbody>

Javascript 使用Javascript

function StudentModel(student){
    this.RollNo = ko.observable(student.RollNo);
    this.Name = ko.observable(student.Name);
    this.Phone = ko.observable(student.Phone);
    this.Email = ko.observable(student.Email);
    this.isDeleted = ko.observable(student.isDeleted);
    this.isEdited = ko.observable(student.isEdited);
}

function StudentViewModel() {
    //Array of students
    this.students = ko.observableArray();

    //Data retrived from the server
    var listStudent= JSON.parse(@Html.Raw(ViewBag.StudentsList));;
    var mappedStudents = $.map(listStudent, function(student) { return new StudentModel(student) });

    //Map it to show the data
    this.students(mappedStudents);

    //Delete student
    this.deleteStudent= function(student){
        var stu = this.students()[this.students.indexOf(student)];
        stu.isDeleted(true);
    }.bind(this);  

When I click on Delete the UI is not updated... When I try stu.isDeleted=true; 当我单击Delete ,UI不会更新...当我尝试stu.isDeleted=true; still it does not works... Any help would be appreciated... 仍然不起作用...任何帮助将不胜感激...

Fiddle 小提琴

The problem is in the databinding. 问题出在数据绑定中。

visible: !students().isDeleted

This looks up the isDeleted property in the observable array. 这将在可观察数组中查找isDeleted属性。 Which doesn't exist, so it is false and will always show all the elements. 哪个不存在,所以它是错误的,并且将始终显示所有元素。

If you want to hide the students the visible binding should be on the <tr> . 如果要隐藏学生,则可见的绑定应该在<tr>

If you want to remove the student from the observable array you can just remove it. 如果要从可观察数组中删除学生,则可以将其删除。

http://jsfiddle.net/8fALs/2/ http://jsfiddle.net/8fALs/2/

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

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