【问题标题】:AngularJS Ui-grid DataTables, how to avoid multi ng-repeatAngularJS Ui-grid DataTables,如何避免多次重复
【发布时间】:2015-11-17 11:01:30
【问题描述】:

问题来了,

我用 angularJS 列出了一些人。 人们是看起来像这样的 JSON 对象:

{ "id":"0" , "name":"My name" , "firstname":"My first Name" , "contacts":[{"id":"0" , "type":"phone" , "value":"0574869345"},{"id":"1" , "type":"email" , "myEmail@mail.com"}] }

请注意,此人可能有多个联系信息(电子邮件、电话等...)

我实际上能够将人员列表(数组)显示到一个“简单”表中:

  1. ng-repeat on "persons"(peopleList 中的人员)
  2. ng-repeat on "contacts"(在 people.contacts 中的联系人)

这听起来像是最合乎逻辑且最简单的解决方案。但在其他一些情况下,使用不太复杂的数据,我正在使用“ng-grid”,它现在是“ui-grid”来显示数据表。

而且,在此之前,我曾经使用过 jquery.dataTables,但似乎它不适合 angular,因为它与 js var 直接相关,并且在 $scope 更改时没有“更新”......你可能明白了,用户将能够“更改”事件中的人员列表。所以网格/表格内容必须链接到范围。

这是我正在尝试实现的当前“仅角度”解决方案的一个非常缩短/简化的示例:http://jsfiddle.net/k2p3xxhx/

如果可能的话,我想使用 ng-grid 解决方案(不工作!):http://jsfiddle.net/guprs47p/

【问题讨论】:

    标签: javascript angularjs json object


    【解决方案1】:

    Angular UI 网格允许您绑定自定义模板。 您可以使用字段的单元格模板属性来实现所需的功能。

    您可以使用 cellTemplate,它只是用于呈现数据的自定义 HTML。

    <div ng-repeat='field in COL_FIELD'>
      <div>{{field.id}} - {{field.type}}- {{field.value}}</div>
     </div>
    

    现在,既然您有了模板,您就可以使用它的 cellTemplate 属性分配给您的字段(它只是 UI Grid 中的一列),因此您的字段对象如下所示,

      {
      field: "contacts",
      displayName: "Contacts",
      cellTemplate: "<div class='ui-grid-cell-contents'><div ng-repeat='field in COL_FIELD'>{{field.id}} - {{field.type}}- {{field.value}}</div></div>"
    }
    

    可以看到,有一个COL_FIELD会在上面迭代数据,这个COL_FIELD在绑定数据的时候会有实际值。所以你知道实际值是一个可以使用ng-repeat 迭代的数组。

    因此,当 ui 网格呈现时,它将遍历您的数据并显示值。

    下面我使用了与您相同的示例来根据您的需要创建示例。

    您可以在 UI Grid 的 official site 上找到有关自定义模板的更多信息

    希望对你有帮助!

    var app = angular.module("myApp", ['ui.grid']);
    
    function ctrl($scope) {
      $scope.myData = {
        data: "listing",
        columnDefs: [{
          field: "id",
          displayName: "ID"
        }, {
          field: "name",
          displayName: "Name"
        }, {
          field: "firstname",
          displayName: "First Name"
        }, {
          field: "contacts",
          displayName: "Contacts",
          cellTemplate: "<div class='ui-grid-cell-contents'><div ng-repeat='field in COL_FIELD'>{{field.id}} - {{field.type}}- {{field.value}}</div></div>"
        }, ],
        rowHeight: 100
      }
      $scope.listing = [{
        "id": "0",
        "name": "My name",
        "firstname": "My first Name",
        "contacts": [{
          "id": "0",
          "type": "phone",
          "value": "0574869345"
        }, {
          "id": "1",
          "type": "email",
          "value": "myEmail@mail.com"
        }]
      }, {
        "id": "1",
        "name": "My name One",
        "firstname": "My first Name One",
        "contacts": [{
          "id": "2",
          "type": "phone",
          "value": "0574444444"
        }, {
          "id": "3",
          "type": "email",
          "value": "myEmailOne@mail.com"
        }]
      }];
      $scope.count = 0;
      $scope.switch = function() {
        if ($scope.count % 2 == 0) {
          $scope.listing = [{
            "id": "0",
            "name": "My name",
            "firstname": "My first Name",
            "contacts": [{
              "id": "0",
              "type": "phone",
              "value": "0574869345"
            }, {
              "id": "1",
              "type": "email",
              "value": "myEmail@mail.com"
            }]
          }, {
            "id": "1",
            "name": "My name One",
            "firstname": "My first Name One",
            "contacts": [{
              "id": "2",
              "type": "phone",
              "value": "0574444444"
            }, {
              "id": "3",
              "type": "email",
              "value": "myEmailOne@mail.com"
            }]
          }];
        } else {
          $scope.listing = [{
            "id": "2",
            "name": "My name Two",
            "firstname": "My first Name Two",
            "contacts": [{
              "id": "4",
              "type": "phone",
              "value": "0888888888"
            }, {
              "id": "5",
              "type": "email",
              "value": "myEmailTwo@mail.com"
            }]
          }, {
            "id": "3",
            "name": "My name Three",
            "firstname": "My first Name Three",
            "contacts": [{
              "id": "6",
              "type": "phone",
              "value": "022222222"
            }, {
              "id": "7",
              "type": "email",
              "value": "myEmailThree@mail.com"
            }]
          }];
        }
        $scope.count++;
        console.log('switched!');
      };
    }
    .uiGridTable: {
      height: 600px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    <script src="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.js"></script>
    <link href="https://cdn.rawgit.com/angular-ui/bower-ui-grid/master/ui-grid.min.css" rel="stylesheet" />
    
    <div ng-app="myApp" ng-controller="ctrl">
      <button type="button" ng-click="switch();">Switch</button>
      <div ui-grid="myData" class="uiGridTable"></div>
    </div>

    【讨论】:

    • 这真的很有帮助!感谢您的回答。但是是否有可能让它像一个“表响应”表(来自引导程序)并添加数据表中的原生提示,如“快速搜索”输入以及将显示设置为 10 个条目或 20 个或更多/更少的可能性...与页面管理?
    • UI 网格提供列过滤和全局过滤。它还提供默认排序,如果这不符合您的要求,它可以编写您自己的自定义算法。是的,UI 网格支持客户端和服务器端的分页以及分页(显示下拉列表的行数)。您可以在官方网站上查看功能。
    猜你喜欢
    • 1970-01-01
    • 2011-11-18
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2014-04-15
    相关资源
    最近更新 更多