Gabe Wonders
Gabe Wonders

Reputation: 35

Cannot remove items from observable array using Knockoutjs

Using knockoutjs I am trying to remove a user from my observable array named users. Perhaps its not working as expected due to the fact that my viewModel is within a document.ready function?

Here is a background on the code below: My application pulls JSON data from three different Salesforce orgs and places it in three different tables (one for each org). When the page is loaded the application immediately creates a view model containing an observable array. There is also a class constructor called User that makes approvalDate observable. The tables are sorted ascending by approval date as shown in the foreach loop in the tag.

For the Remove button i use

data-bind="click: $root.removeUser"

I use root prefix to cause KO to look for a removeUser handler on my top-level viewmodel instead of on the User instance being bound.

Any suggestions would be appreciated. Thanks!

View Model:

function User(id, name, profile, userRole, lastLoginDate, approvalDate, salesforceOrg) {
    this.id = id;
    this.name = name;
    this.profile = profile;
    this.userRole = userRole;
    this.lastLoginDate = lastLoginDate;
    this.approvalDate = ko.observable(approvalDate);
    this.SalesforceOrg__c = salesforceOrg;
  }

  $(function () {
    var viewModel = function (updateUarUrl, userRecords) {
      var self = this;

  self.updateUarUrl = updateUarUrl 

  self.users = ko.observableArray([]);

  for (i = 0; i < userRecords.length; i++) {
    var newUser = new User(
      userRecords[i].Id, userRecords[i].Name, userRecords[i].Profile, userRecords[i].UserRole,
      userRecords[i].LastLoginDate, userRecords[i].ApprovalDate__c, userRecords[i].SalesforceOrg__c);
    self.users.push(newUser);
  }

  self.removeUser = function (user) {
    self.users.remove(user);
  }

  self.checkDate = function (date) {
    var lastLoginDate = new Date(date);
    var today = new Date();
    var difference = (today - lastLoginDate) / 86400000; // to change miliseconds into days divide by this number

    if (difference > 89) {
        return true;
    }
    return false;
  };


};

  $.getJSON("@Url.Action("GetUser")", function(allData) {
    // Bind the ViewModel to the View using Knockout
    // ko.applyBindings(myViewModel, document.getElementById('someElementId')) restricts the activation to the element with ID someElementId and its descendants
    ko.applyBindings(new viewModel('@Url.Action("PostGscUarLog")', allData.gscUsers), document.getElementById("gsc"));
    ko.applyBindings(new viewModel('@Url.Action("PostDsUarLog")', allData.dsUsers), document.getElementById("ds"));
    ko.applyBindings(new viewModel('@Url.Action("PostCompassUarLog")', allData.compassUsers), document.getElementById("compass"));
  });
});

View:

@foreach (var table in tables) 
{
  <h2>@table.Desc Users</h2>
  <table class="table" id="@table.Id">
    <thead>
      <tr>
        <th>Name</th>
        <th>Profile</th>
        <th>Role</th>
        <th>Last Login Date</th>
        <th>Approval Date</th>
        <th>Business Reason</th>
      </tr>
    </thead>
    <tbody data-bind="foreach: users.sort(function (l, r) { return new Date(l.approvalDate()) - new Date(r.approvalDate()) })">
      <tr>
        <td data-bind="text: name"></td>
        <td data-bind="if: profile"><span data-bind="text: profile.Name"></span></td>
        <td data-bind="if: userRole"><span data-bind="text: userRole.Name"></span></td>
        <td data-bind="if: lastLoginDate"><span data-bind="text: moment(lastLoginDate).format('LL')"></span></td>
        <td data-bind="if: approvalDate()"><span data-bind="text: moment(approvalDate()).format('LL')"></span></td>
        <td><input data-bind="attr: { id: name }, enable: $parent.checkDate(lastLoginDate) == true" type="text" /></td>
        <td><button type="button" data-bind="click: $root.removeUser">Remove</button></td>
      </tr>
    </tbody>
  </table>
}

Debug screenshot: https://www.dropbox.com/s/bl0p2wfqhpp1orb/1-16-2015%201-11-09%20PM.gif?dl=0

Upvotes: 0

Views: 182

Answers (1)

I just wonder why don't you use users().sort(....) in stead of users.sort(...) in your binding. I've just test in jsfiddle, I couldn't bind data like your way.

<ul data-bind="foreach: Items().sort()">

Upvotes: 1

Related Questions