简体   繁体   English

如何在UI部分中更新敲除js observablearray的编辑项?

[英]How to update the edited item of knockout js observablearray in UI part?

How to update the knockout js observablearray in UI part..Initially Im editing a item and in update function i'm trying to update a particular item so im sending the current item object to service and getting the updated vales as response. 如何更新UI部分中的淘汰赛js observablearray。最初,我正在编辑项目,并在更新功能中,我试图更新特定项目,以便我将当前项目对象发送给服务并获取更新后的价位作为响应。 Inside the response I need to update the observablearray as well the UI too.. I have tried some thing like this and its not working for me... please check in http://jsfiddle.net/up8rB/20/ 在响应中,我还需要更新UI以及UI。.我已经尝试过类似的操作,但它对我不起作用...请在http://jsfiddle.net/up8rB/20/中检查

<div data-bind="ifnot: Role()">           
 <label for="description">Description</label>
 <input type="text" data-bind="value: Description" class="form-control" placeholder="Enter the description..." />
 <button data-bind="click: $root.create"> Create</button>
</div>
<div data-bind="if: Role">
 <label for="description">Description</label>
 <input type="text" data-bind="value: Role().Description" class="form-control" placeholder="Enter the description..." />
 <button data-bind="click: $root.update"> Save</button>
</div>
<table id="example1" class="table table-bordered table-striped" data-bind="visible: Roles().length > 0">
<thead>
<tr>
<th>RoleID</th>
<th>Description</th>                               
<th>Actions</th>
</tr>
</thead>
<tbody data-bind="foreach: Roles">
<tr>
<td data-bind="text: $data.RoleID"></td>
<td data-bind="text: $data.Description"></td>
<td>
<button data-bind="click: $root.edit" >Edit</button>
<button data-bind="click: $root.delete" >Delete</button>
</td>
</tr>
</tbody>
</table>
function RoleViewModel() {
    var self = this;
    self.RoleID = ko.observable("0");
    self.Description = ko.observable().extend({
        required: { message: 'Please supply description ..' }
    });

var Role = {
    RoleID: self.RoleID,
    Description: self.Description
};
self.Role = ko.observable();
self.Roles = ko.observableArray();
$.ajax({
        url: 'Service.svc/RoleCollection',
        cache: false,
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        data: {},
        success: function (data) {
            self.Roles(data); //Put the response in ObservableArray
        }
    });
self.edit = function (Role) {
    self.Role(Role);
}
self.update = function () {
        var Role = self.Role();
        $.ajax({
            url: '..service.svc/UpdateRole',
            cache: false,
            type: 'PUT',
            contentType: 'application/json; charset=utf-8',
            data: ko.toJSON(Role),
            success: function (data) {
                for (var i = 0; i < self.Roles().length; i++) {
                    alert(self.Roles()[i].RoleID());
                    if (self.Roles()[i].RoleID() === data.RoleID) {
                        self.Role(self.Roles()[i]);
                        break;
                    }
                }
            }
        })
}
var viewModel = new RoleViewModel();
ko.applyBindings(viewModel);

Your issue is that your are assigning items to your observable array that aren't themselves observable , so your UI is not updating when they change. 您的问题是您正在将自己本身observable项目分配给observable array ,因此,当它们更改时, UI不会更新。

I did some renaming, as I found it confusing with so many things named 'Role' , but the first change was your array setup: 我做了一些重命名,因为发现它与许多名为'Role'东西混淆了,但是第一个更改是您的数组设置:

var roles = [{
    RoleID: ko.observable(1),
    Description: ko.observable('First item')
}, {
    RoleID: ko.observable(2),
    Description: ko.observable('Second item')
}];

I also modified the update function: 我还修改了更新功能:

self.update = function () {
    var _Role = self.Role();
    for (var i = 0; i < self.Roles().length; i++) {
        if (self.Roles()[i].RoleID === _Role.RoleID) {
            self.Roles()[i] = _Role();
            break;
        }
    }
}

See the Updated Fiddle 参见更新的小提琴

Update: 更新:

Your json response should populate the observableArray correclty, but you may have an issue when setting it back after the update, try setting the properties rather than overwriting the element: 您的json响应应填充observableArray正确性,但是在更新后将其重新设置时,您可能会遇到问题,请尝试设置属性而不是覆盖元素:

self.update = function () {
    var _Role = self.Role();
    for (var i = 0; i < self.Roles().length; i++) {
        if (self.Roles()[i].RoleID === _Role.RoleID) {
            self.Roles()[i].RoleID = _Role().RoleID;
            self.Roles()[i].Description = _Role().Description;
            break;
        }
    }
}

See the Updated Fiddle 参见更新的小提琴

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

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