gpt4 book ai didi

javascript - 在angular ng-grid中如何指定子数组的计数

转载 作者:行者123 更新时间:2023-11-29 19:43:09 25 4
gpt4 key购买 nike

如果字段包含多个项目的数组,我需要指定 x 个单位的计数。我如何在 ng-grid 字段显示名称中执行此操作?

Json 示例:$scope.data = [{id: {1, 3}, 纳米:无名氏} , {id: {2,4,5}, nm: Jane Doe}]

当前结果:

Id              Name
-------------- -------------------------------
1,3 John Doe
2,4,5 Jane Doe

期望的结果:

Id              Name
-------------- -------------------------------
2 Ids John Doe
3 Ids Jane Doe

当前的 Angular 代码:

                $scope.gridOptions = {
data: 'data',
selectedItems: $scope.mySelections,
columnDefs: [
{field: 'id', displayName: 'Id', width: '***'},
{field: 'nm', displayName: 'Name', width: '****'}],
multiSelect: false
};

最佳答案

只需像这样创建一个单元格模板:

<div class="ngCellText" ng-class="col.colIndex()"><span ng-cell-text>{{row.getProperty("id").length}} Ids</span></div>

然后像这样应用它:

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {


$scope.myData = [{id: [1, 12], name: 'John Doe'},
{id: [3, 7, 9, 6, 2], name: 'John Doe'},
{id: [10, 52, 78], name: 'John Doe'},
{id: [101, 88, 72, 99], name: 'John Doe'}];


$scope.columnDefs = [
{
displayName:'Id',
cellTemplate: 'cellTemplate.html'
},
{
field:'name', displayName:'Name'
}
]

$scope.gridOptions = {
data: 'myData',
columnDefs: $scope.columnDefs
};
});

示例: Plunker

关于javascript - 在angular ng-grid中如何指定子数组的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21708657/

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