无法使用Knockoutjs从可观察数组中删除项目

时间:2015-01-16 19:36:28

标签: javascript jquery knockout.js

使用knockoutjs我试图从名为users的可观察数组中删除用户。由于我的viewModel位于document.ready函数中,它可能没有按预期工作?

以下代码背景如下: 我的应用程序从三个不同的Salesforce组织中提取JSON数据,并将其放在三个不同的表中(每个组织一个)。加载页面时,应用程序立即创建包含可观察数组的视图模型。还有一个名为User的类构造函数,它使approvalDate可观察。 这些表按批准日期递增排序,如标记中的foreach循环所示。

对于“删除”按钮,我使用

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

我使用root前缀使KO在我的顶级viewmodel上查找removeUser处理程序,而不是在绑定的User实例上查找。

任何建议将不胜感激。谢谢!

查看型号:

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"));
  });
});

查看:

@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>
}

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

1 个答案:

答案 0 :(得分:1)

我只是想知道你为什么不在绑定中使用users()。sort(....)而不是users.sort(...)。我只是在jsfiddle进行测试,我无法按照您的方式绑定数据。

<ul data-bind="foreach: Items().sort()">
相关问题