4
Answers

Convert computed output to observableArray using Knockout

Ask a question
Hi,

I am getting all the Applied Users at once and binding them in a Table1. And I am filtering the users based on the approval and binding them to Table2 by using the below code.

function UserViewModel(){
var self=this;

self.UserName = ko.observable("");
self.ContactNo = ko.observable("");
self.Email = ko.observable("");
self.User_ID = ko.observable("");
self.IsApproved = ko.observable("");

var Users = {
UserName: self.UserName,
ContactNo: self.ContactNo,
Email: self.Email,
User_ID: self.User_ID,
IsApproved: self.IsApproved 
};

self.Users = ko.observableArray();

$.ajax({
url: "Home/GetAllUsers",
cache: false,
type: "JSON",
contentType: "application/json; charset=utf-8",
data: {},
success: function(data){
self.ApprovedUsers(data); 
}
});

self.ApprovedUsers = ko.observableArray();
self.ApprovedUsers = ko.computed(function(){
        return ko.utils.arrayFilter(self.User(),function(item){
        return item.IsApproved === 1
 });
});

}

$(document).ready(function(){
var viewModel = new UserViewModel();
ko.applyBindings(viewModel);
});

My Requirement is:
When admin approved the user from Table1 I want to remove that user from Table1 and add him to Table2.

My Problem Is:
I am getting the filtered data in observable format, but for that one I cannot able to write the methods either "PUSH" or "REMOVE".
1. How can I get the ObservableArray from the ko.computed.?  or
2. Filter the ObservableArray data and return the data in observableArray format. How can I do that.?

Can some body plz help me on this.

Thanks in Advance,
Murali Krishna. 

Answers (4)