我正在使用这个库: http: //l-lin.github.io/angular-datatables
一切都很好——我以 Angular 的方式渲染了表格——直到数据大到足以影响性能(只有 1,000 多行)。
解决方案是按照此处的建议使用 ajax 或 promise 进行渲染:angular-datatables the deferRender not working
所以在过去的三天里,我被困在如何将下面的代码从角度方式转换为承诺
角度方式
视图.html
<table datatable="ng" dt-options="dtOptions" class="table table-striped table-bordered">
<thead>
<tr>
<th>No</th>
<th>Issue</th>
<th>Meeting No.</th>
<th>Department/Unit</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="(key, value) in issueList | groupBy : 'IssueId' | reverse track by $index">
<td ng-class="{read : value[0].AnswerStatus == 1}">{{$index+1}}</td>
<td>{{value[0].Issue}}</td>
<td>{{value[0].MeetingNo}}</td>
<td>
<table class="table table-striped table-bordered" cellspacing="0" width="100%">
<tbody>
<tr ng-repeat="x in value">
<td width="80%">{{x.Department}}/{{x.Unit}}</td>
<td> <a class="btn btn-default waves-effect m-b-5" ng-click="sendDetail(x.IssueId,x.UnitId)"><i class="fa fa-folder-open"></i> Show </a></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
视图.js
$scope.dtOptions =
DTOptionsBuilder.newOptions()
.withOption('stateSave', true)
.withOption('stateDuration', -1)
.withOption('deferRender', true);
$http({
method: 'GET',
url: 'http://issue.json'})
.then(function(response) {
$scope.issueList = response.data;
});
问题.json
[{
"IssueId": "1",
"MeetingNo": "1",
"Issue": "Issue title 1",
"Content": "Content 1",
"AnswerStatus": null,
"UnitId": 1,
"Unit": "Unit 1",
"DepartmentId": 1,
"Department": "Department 1"
}, {
"IssueId": "2",
"MeetingNo": "1",
"Issue": "Issue title 2",
"Content": "Content 2",
"AnswerStatus": null,
"UnitId": 5,
"Unit": "Unit 5",
"DepartmentId": 1,
"Department": "Department 1"
}, {
"IssueId": "2",
"MeetingNo": "1",
"Issue": "Issue title 2",
"Content": "Content 2",
"AnswerStatus": 1,
"UnitId": 6,
"Unit": "Unit 6",
"DepartmentId": 1,
"Department": "Department 1"
}]
下面的代码是我的中途改造
承诺方式
视图.html
<table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="table table-striped table-bordered">
</table>
视图.js
$scope.dtOptions =
DTOptionsBuilder.fromFnPromise(function() {
var defer = $q.defer();
$http({
method: 'GET',
url: 'http://issue.json'
}).then(function(result) {
defer.resolve(result.data);
});
return defer.promise;
})
.withOption('stateSave', true)
.withOption('stateDuration', -1)
.withOption('deferRender', true); //the very reason to use promise for performance booth
$scope.dtColumns = [
DTColumnBuilder.newColumn(null).withTitle('No')
.renderWith(function(data, type, full, meta) {
return (meta.row+1);
}),
DTColumnBuilder.newColumn('Issue').withTitle('Issue'),
DTColumnBuilder.newColumn('MeetingNo').withTitle('Meeting No.'),
DTColumnBuilder.newColumn('Department').withTitle('Department/Unit'),
];
如您所见,第一列缺少所需的样式,AnswerStatus == 1最后一列缺少组合值Department + '/' + Unit和导航到另一个页面的按钮。
另请注意,角度方式正在使用 groupBy 这进一步使我的问题复杂化:(
请帮忙。谢谢你的时间。
更新:
实际上,我也在考虑替代解决方案,但仍然面临障碍。我正在考虑用这个重构json:
Array.prototype.groupBy = function(prop) {
return this.reduce(function(groups, item) {
const val = item[prop]
groups[val] = groups[val] || []
groups[val].push(item)
return groups
}, {})
}
$scope.dtOptions =
DTOptionsBuilder
.fromFnPromise(function() {
var defer = $q.defer();
$http({
method: 'GET',
url: 'http://issue.json'
}).then(function(result) {
var d = result.data.groupBy('IssueId');
var arr = Object.values(d);
console.log(arr);
defer.resolve(arr);
});
return defer.promise;
})
.withOption('stateSave', true)
.withOption('stateDuration', -1)
.withOption('deferRender', true);
这会产生一个新的编号数组数组,如下所示(console.log(arr)):
[
[{
"IssueId": "1",
"MeetingNo": "1",
"Issue": "Issue title 1",
"Content": "Content 1",
"AnswerStatus": null,
"UnitId": 1,
"Unit": "Unit 1",
"DepartmentId": 1,
"Department": "Department 1"
}],
[{
"IssueId": "2",
"MeetingNo": "1",
"Issue": "Issue title 2",
"Content": "Content 2",
"AnswerStatus": null,
"UnitId": 5,
"Unit": "Unit 5",
"DepartmentId": 1,
"Department": "Department 1"
}, {
"IssueId": "2",
"MeetingNo": "1",
"Issue": "Issue title 2",
"Content": "Content 2",
"AnswerStatus": 1,
"UnitId": 6,
"Unit": "Unit 6",
"DepartmentId": 1,
"Department": "Department 1"
}]
]
但我不知道如何处理新数组。