简体   繁体   English

将通过AJAX调用获取的数据追加到已经存在的列表中以进行延迟加载

[英]Appending data fetched by an AJAX call to an already existing list for lazy-loading

I am pretty new to UI development, so please, bear with me. 我对UI开发非常陌生,所以请耐心等待。 I am trying to implement lazy-loading on a webpage. 我正在尝试在网页上实现延迟加载。 That is, when the user scrolls to the bottom of the page, a page is fetched from the server using an AJAX call. 也就是说,当用户滚动到页面底部时,将使用AJAX调用从服务器获取页面。 What is wish to do is to append the new data to the already existing one. 希望做的是将新数据附加到已经存在的数据上。 Currently, the new data occupies the entire list-view and the scroll bar moves to the top. 当前,新数据占据整个列表视图,滚动条移至顶部。 Here is my code: 这是我的代码:

AJAX call: AJAX通话:

$scope.getMoreData=function(){
      updateAjaxData();
      var url=$scope.getURL();
      A.ajax(url, {
          method: 'post',
          params: AJAX_DATA,
          success: function(data, s, x) {
              $scope.providersList = JSON.parse(data.serviceproviderlist);
              $scope.serviceName = data.category;
              $scope.limit = SEARCH_LIMIT;
              $scope.$apply();
              var div = document.getElementById("spn-service-provider-list");
              var content = document.createTextNode($scope.providersList);
              div.appendChild(content);
          },
          error: function(xhr, statusText, errorThrown) {
              $scope.errorCallback(xhr);
          }
     });
  }

Here is my HTML: 这是我的HTML:

<div id="spn-service-provider-list">
    <div class="spnsearch">
        <i class="spnicon spn-icon-search"></i> 
                <input type="search" ng-model="spsearch" id="spsearch" placeholder="Search Providers" name="spsearch">
    </div>
    <div id="spn-list" style="height:100vh;overflow-y:scroll;" when-scrolled="showMoreItems(filteredproducts.length,limit)">
            <div ng-repeat="providersList in ( filteredproducts = ( providersList | filter:{ providerName: spsearch } | filter:{ citiesServicing: locationsearch }:ignoreNullComparator ))  | orderBy: 'rank' | limitTo: limit " >
            ...................
            ...................

The problem is that when the new page is fetched using the AJAX call, it occupies the entire list-view. 问题在于,当使用AJAX调用获取新页面时,它将占据整个列表视图。 That is, the old list completely vanishes, and the new list occupies its place, with the scroll bar moving to the top. 也就是说,旧列表完全消失,新列表占据其位置,滚动条移到顶部。 Further, what is appending to my list successfully is a couple of [object] 此外,成功添加到我的列表中的是几个[object]

Here is the code suggested, still, no change: 这是建议的代码,仍然没有更改:

success: function(data, s, x) {
              if($scope.providersList === null) {
                $scope.providersList = []; /* init if null */
              }
              /* Update the existing array */
              Array.prototype.push.apply($scope.providersList, JSON.parse(data.serviceproviderlist));
              $scope.serviceName = data.category;
              $scope.limit = SEARCH_LIMIT;
              $scope.$apply();
              viewToBeDisplayed(LIST_VIEW);
          },

Because you erase your existing array. 因为您擦除了现有阵列。
So replace 所以更换

$scope.providersList = JSON.parse(data.serviceproviderlist);

with

if($scope.providersList == null) {
  $scope.providersList = []; /* init if null */
}
/* Update the existing array */
Array.prototype.push.apply($scope.providersList, JSON.parse(data.serviceproviderlist));

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

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