【问题标题】:Pagination of nested objects in angular jsAngular js中嵌套对象的分页
【发布时间】:2017-09-18 22:49:35
【问题描述】:

我从 angular 开始,我有一个我想分页的嵌套对象。要分页的项目是给定对象中的一些“属性”。 queuelist 对象嵌套在数组中的数组中。任何帮助,将不胜感激。 非分页数据的 plunker 链接是:

https://plnkr.co/edit/zgo0msd6y5ba6DJ6qGlc?p=preview

app.js:

var app = angular.module("myApp",[])
                 .controller("mycontroller",['$scope',function($scope){

  $scope.queuelist = [
  {
    "name": "ONE",
    "QueueList": [
      {
        "id": 1,
        "attributes": {
          "a": 1,
          "b": "2017-07-25T12:57:06Z",
          "c": 1500967626000,
          "d": "asdasd",
          "e": "aasdasdasd",
          "f": 0
        },
        "$$hashKey": "object:64"
      },
      {
        "id": 2,
        "attributes": {
          "a": 1,
          "b": "2017-07-25T12:57:06Z",
          "c": 1500967626000,
          "d": "asdasd",
          "e": "aasdasdasd",
          "f": 0
        },
        "$$hashKey": "object:65"
      },
      {
        "id": 3,
        "attributes": {
          "a": 1,
          "b": "2017-07-25T12:57:06Z",
          "c": 1500967626000,
          "d": "asdasd",
          "e": "aasdasdasd",
          "f": 0
        },
        "$$hashKey": "object:66"
      }
    ],
    "$$hashKey": "object:59"
  },
  {
    "name": "TWO",
    "QueueList": [
      {
        "id": 4,
        "attributes": {
          "a": 1,
          "b": "2017-07-25T12:57:06Z",
          "c": 1500967626000,
          "d": "asdasd",
          "e": "aasdasdasd",
          "f": 0
        },
        "$$hashKey": "object:72"
      },
      {
        "id": 5,
        "attributes": {
          "a": 1,
          "b": "2017-07-25T12:57:06Z",
          "c": 1500967626000,
          "d": "asdasd",
          "e": "aasdasdasd",
          "f": 0
        },
        "$$hashKey": "object:73"
      },
      {
        "id": 6,
        "attributes": {
          "a": 1,
          "b": "2017-07-25T12:57:06Z",
          "c": 1500967626000,
          "d": "asdasd",
          "e": "aasdasdasd",
          "f": 0
        },
        "$$hashKey": "object:74"
      },
      {
        "id": 7,
        "attributes": {
          "a": 1,
          "b": "2017-07-25T12:57:06Z",
          "c": 1500967626000,
          "d": "asdasd",
          "e": "aasdasdasd",
          "f": 0
        },
        "$$hashKey": "object:75"
      }
    ],
    "$$hashKey": "object:60"
  }
];


  $scope.objects = [];


   /*                  
  for(i=0;i<$scope.data.length;i++){
      $scope.data2.push($scope.data[i].QueueList);
  };
   */

    for(i=0;i<$scope.queuelist.length;i++){
      for(j=0;j<$scope.queuelist[i].QueueList.length;j++){
          $scope.objects.push($scope.queuelist[i].QueueList[j].attributes);
      };
    };              
   }])

和 index.html:

<!DOCTYPE html>
<html ng-app="myApp">
    <head>
        <script type= "text/javascript" src= "https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.2/angular.js"></script>
        <script type= "text/javascript" src= "app.js"></script>
        <style>
            table, td, th {    
            border: 1px solid #ddd;
            text-align: left;
            }

            table {
            border-collapse: collapse;
            width: 100%;
            }

            th, td {
            padding: 15px;
            }
        </style>
    </head>
    <body>
        <div ng-controller="mycontroller">

            <div ng-repeat="queueJob in queuelist">
                {{queueJob.name}}
                <table>
                    <thead>
                        <tr>
                            <th><b>a</b></th>
                            <th><b>b</b></th>
                            <th><b>c</b></th>
                            <th><b>e</b></th>
                            <th><b>f</b></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="queue in queueJob.QueueList">
                            <td>{{queue.attributes.a}}</td>
                            <td>{{queue.attributes.b}}</td>
                            <td>{{queue.attributes.c}}</td>
                            <td>{{queue.attributes.e}}</td>
                            <td>{{queue.attributes.f}}</td>
                        </tr>
                        <br/><br/>
                    </tbody>
                </table>
                <br/><br/>
                <br/><br/>
            </div>

        </div>
    </body>
</html>

【问题讨论】:

  • 我需要为每个表格单独分页。谢谢
  • 样本数据中的所有attributes 对象都完全相同。在它们之间导航下一个/上一个将呈现相同的显示......或者它是您想要导航的attributes 对象中的值(a、b、c、d、@ 987654332@, f)?显示预期显示的表格/屏幕截图将是最有帮助的。
  • 嗨,马修,我懒得编单独的数据了。所以我只是编辑了每个属性中的“值”。在每种情况下,“钥匙”都是相同的。查看具有编辑值的新 plunker。
  • 签出ng-table.com/# ng-table 指令,轻松实现分页和更多内容
  • 类似CodePen Demo

标签: angularjs pagination


【解决方案1】:

您可以使用以下组合来做到这一点:

  • 自定义过滤器(确定要跳过多少条记录),以及...
  • 内置的limitTo 过滤器(将记录数限制为所需的页面大小)

1) 首先创建自定义过滤器。

app.filter("startFrom", function thisFilter() {
  return function(input, index) {
    return input.slice(parseInt(index));
  };
});

过滤器接收一个index,然后在Array.prototype.slice() 方法中使用它。 slice() 方法在给定索引处对数组进行切片,并返回一个包含所有剩余对象的新数组。过滤器返回新数组。

2) 在ng-repeat 指令中使用自定义过滤器和内置limitTo 过滤器。

<tr ng-repeat="queue in queueJob.QueueList | startFrom: queueJob.pageIndex | limitTo: 1">

这里我们使用新创建的startFrom 自定义过滤器,将queueJob.pageIndex 属性作为过滤器的index 参数传递给它。我们将startFrom 过滤器的结果传递给limitTo 过滤器,从而将记录数减少到1。

注意:我们必须在queueJob 迭代变量上使用pageIndex 属性,因为这个ng-repeat 包含在另一个ng-repeat 中,因此$scope.pageIndex 变量将是发生冲突并随后被覆盖。

3) 创建下一个和上一个按钮

<tr>
  <td colspan="5">
    <button class="btn" 
      ng-click="onPrevClicked(queueJob)"
      ng-disabled="isFirst(queueJob)">
      <span class="fa fa-chevron-left"></span>
      Prev
    </button>
    <button class="btn" 
      ng-click="onNextClicked(queueJob)"
      ng-disabled="isLast(queueJob)">
      Next
      <span class="fa fa-chevron-right"></span>
    </button>
    Page {{ queueJob.pageIndex + 1 }}
  </td>
</tr>

这里我们使用ng-click 指令来调用控制器函数,这些函数递增/递减queueJob 对象的pageIndex 属性。如果用户在第一条/最后一条记录上,我们还使用ng-disabled 指令来防止导航下一条/上一条。

4) 在控制器中创建可绑定函数

$scope.onPrevClicked = onPrevClicked;
$scope.onNextClicked = onNextClicked;
$scope.isFirst = isFirst;
$scope.isLast = isLast;

function onPrevClicked(obj) {
  if (!isFirst(obj)) obj.pageIndex--;
}

function onNextClicked(obj) {
  if (!isLast(obj)) obj.pageIndex++;
}

function isFirst(obj) {
  return obj.pageIndex === 0;
}

function isLast(obj) {
  return obj.pageIndex + 1 === obj.QueueList.length;
}

5) 预先初始化pageIndex 属性

$scope.queuelist.forEach(function(obj) {
  obj.pageIndex = 0;
});

这将pageIndex 初始化为一个可以递增并随后递减的数字。

演示

CodePen: Using a custom filter to do pagination

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2013-12-06
    相关资源
    最近更新 更多