Binding ko computed in ui



请帮忙。我有一个绑定到正文的计算可观察项目。计算的可观察量使用搜索函数进行更新。但我的 UI 仍然显示相同的数据。它不显示当前页面上的项目的更新内容。

 var listofcases = ko.observableArray();
    var itemsOnCurrentPage = ko.computed(function () {
        var startIndex = pageSize * currentPageIndex();
        console.log(listofcases.slice(startIndex, startIndex + pageSize));
        return listofcases.slice(startIndex, startIndex + pageSize);
    }, this);
    function SearchCases(username, role, st, ed, ss) {
        $.getJSON('/breeze/Workflow/ListOfCases?UserId=' + username +
                                                '&Role=' + role +
                                                '&RouteId=Annotate&_st=' + st +
                                                '&_ed=' + ed +
                                                '&_ss=' + ss,
                  function (cases) {
                      if (cases.length != 0) {
                          $.each(cases, function (index, _case) {
                              listofcases.push(new CaseDataViewModel(_case));
                          });
                          itemsOnCurrentPage(listofcases());
                      }
                      else {
                          console.log("dddd");
                          listofcases.push(new CaseDataViewModel(_case));
                      }
                  });
    }

你不应该调用 computed from SearchCases函数。挖空会在更新listofcases时自动重新计算计算。删除以下行:

itemsOnCurrentPage(listofcases());

正如Artem所说,你不应该在任何地方设置计算。删除此行:

itemsOnCurrentPage(listofcases());

您也不需要修改计算,因此不需要将其传递给计算,我怀疑这可能会导致问题。尝试将计算值替换为以下内容:

var itemsOnCurrentPage = ko.computed(function () {
        var startIndex = pageSize * currentPageIndex();
        console.log(listofcases().slice(startIndex, startIndex + pageSize));
        return listofcases().slice(startIndex, startIndex + pageSize);
    });

最新更新