gpt4 book ai didi

javascript - AngularJS Ui-grid DataTables,如何避免多次重复

转载 作者:行者123 更新时间:2023-11-30 16:28:26 28 4
gpt4 key购买 nike

问题来了

我用 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 “persons”(peopleList 中的人)
  2. ng-repeat on "contacts"(people.contacts 中的联系人)

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

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

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

这是我希望尽可能使用的 ng-grid 解决方案(不工作!):http://jsfiddle.net/guprs47p/

最佳答案

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

您可以使用 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在绑定(bind)数据的时候会有实际的值。所以你知道实际值是一个数组,可以使用 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>

关于javascript - AngularJS Ui-grid DataTables,如何避免多次重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755264/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com