离子收集 - 重复日期分隔

m1c*_*rdy 3 angularjs ionic-framework ionic

我得到了一个包含文本和图像的大约200个项目的非常大的列表.ng-repeat是缓慢渲染这种顺利的方法.它尝试了这个解决方案.工作得很好.但不是收集重复.

我的网络服务返回:

在此输入图像描述

有特定日期的活动.事件应按日期分组.所以为了使用集合重复,如果你不能使用angular.filter groupBy,如何插入分隔符?

Lef*_*tyX 5

我可以为您提供部分解决方案,只有在数据集按分隔符中显示的字段排序时才能使用.

首先,我们需要在数组中创建一个假元素,以便我们可以区分其他元素中的分隔符.

假设我们有一组从网络服务中提取的帖子:

.controller('mainController', function($scope, dataService) {
    $scope.posts = [];
    var divider = '';
});
Run Code Online (Sandbox Code Playgroud)

当我们加载帖子时,私有字段分隔符将被使用.

我们将loadMore滚动列表时加载额外数据的方法:

$scope.loadMore = function(argument) {
    page++;
    dataService.GetPosts(page, pageSize)
      .then(function(result) {
        if (result.data.length > 0) {
            angular.forEach(result.data, function(value, key) {
              value.divider = false;
              if (value.postId !== divider)
              {
                divider = value.postId;
                $scope.posts.push({divider: true, dividerText: value.postId});
              }
              $scope.posts.push(value);
            });
        }
        else {
            $scope.theEnd = true;
        }
    })
    .finally(function() {
        $scope.$broadcast("scroll.infiniteScrollComplete");
    });
};
Run Code Online (Sandbox Code Playgroud)

当我们从web api获取数据(并且解析了promise)时,我们遍历集合并检查该字段是否与分隔符不同.如果这是一个新的分隔符,我们存储信息并向集合中添加一个新元素:

angular.forEach(result.data, function(value, key) {
    value.divider = false;
    if (value.postId !== divider)
    {
        divider = value.postId;
        $scope.posts.push({divider: true, dividerText: value.postId});
    }
    $scope.posts.push(value);
});
Run Code Online (Sandbox Code Playgroud)

如你所见,我添加了一个元素:

$scope.posts.push({divider: true, dividerText: value.postId});
Run Code Online (Sandbox Code Playgroud)

我已经使用了一个dividerText将在稍后显示的字段.

现在我们需要创建自己的指令divider-collection-repeat,该指令应附加到集合重复:

<ion-item collection-repeat="post in posts" item-height="75" divider-collection-repeat>
Run Code Online (Sandbox Code Playgroud)

我想你正在使用infinite-scroll,所以这里是整个HTML:

  <ion-content ng-controller="mainController">
    <ion-list>
      <ion-item collection-repeat="post in posts" item-height="75" divider-collection-repeat>
          {{post.name}}
    </ion-item>
  </ion-list>
  <ion-infinite-scroll ng-if="!theEnd" on-infinite="loadMore()" distance="50%"></ion-infinite-scroll>
  </ion-content>
Run Code Online (Sandbox Code Playgroud)

这是指令:

.directive('dividerCollectionRepeat', function($parse) {

    return {
        priority: 1001,
        compile: compile
    };

    function compile (element, attr) {
      var height = attr.itemHeight || '75';
      var itemExpr = attr.collectionRepeat.split(' ').shift();
      attr.$set('itemHeight', itemExpr + '.divider ? 40 : (' + height + ')');
      attr.$set('ng-class', itemExpr + '.divider ? "item-divider" : ""');
      var children = element.children().attr('ng-hide', itemExpr + '.divider');
      element.prepend(
        '<div ng-show="' + itemExpr + '.divider" class="my-divider" ' +
        'ng-bind="' + itemExpr + '.dividerText" style="height:100%;">' +
        '</div>'
      );

      return function postLink(scope, element, attr) {
         scope.$watch(itemExpr + '.divider', function(divider) {
            element.toggleClass('item-divider', !!divider);
         });
      };  

    }

});
Run Code Online (Sandbox Code Playgroud)

该指令使用您在collection-repeat中定义的表达式将元素(html)预先添加到列表中.

在我的示例中,我使用了collection-repeat="post in posts"这一行:

var itemExpr = attr.collectionRepeat.split(' ').shift();
Run Code Online (Sandbox Code Playgroud)

获取项目的名称; 在我的情况下,它将是post.

我们使用的height原因可能是我们可能需要为分隔符设置不同的高度.

这一点是所有魔法发生的地方:

  element.prepend(
    '<div ng-show="' + itemExpr + '.divider" class="my-divider" ' +
      'ng-bind="' + itemExpr + '.dividerText" style="height:100%;">' +
    '</div>'
  );
Run Code Online (Sandbox Code Playgroud)

它使用ng-show字段'post.divider'(ng-show="' + itemExpr + '.divider")并绑定我们的文本字段ng-bind="' + itemExpr + '.dividerText"

我还添加了一个自定义类my-divider,以防我们需要稍微更改分隔符的布局.

最终的结果是在这里或在这个plunker.

您可能已经注意到我没有使用日期字段,因为我已经有一个样本,遗憾的是,我没有任何日期.我想应该很容易适应你的情况.

该指令基于我在github上找到的样本.您可以在此处找到该指令的代码.