【发布时间】:2019-05-08 21:07:05
【问题描述】:
我正在使用 Angular 数据表 (https://l-lin.github.io/angular-datatables/#/welcome) 创建一个 Angular 6 应用程序。这是我的组件代码:
import { Component, OnInit, ViewChild } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { DataTableDirective } from 'angular-datatables';
@Component({
selector: 'app-mainmenu',
templateUrl: './mainmenu.component.html',
styleUrls: ['./mainmenu.component.css']
})
export class MainmenuComponent implements OnInit {
@ViewChild(DataTableDirective)
datatableElement: DataTableDirective;
dtOptions: DataTables.Settings = {};
learningPaths: LearningPath[];
constructor(private http: HttpClient) { }
ngOnInit(): void {
const that = this;
this.dtOptions = {
pagingType: 'full_numbers',
pageLength: 10,
serverSide: true,
processing: true,
ajax: (dataTablesParameters: any, callback) => {
that.http
.post<DataTablesResponse>(
'http://localhost:4154/api/LP?p=1'
,
dataTablesParameters, {}
).subscribe(resp => {
that.learningPaths = resp.data;
callback({
recordsTotal: resp.recordsTotal,
recordsFiltered: resp.recordsFiltered,
data: []
});
});
},
columns: [{ data: 'icon', orderable: false }, { data: 'name' }, { data: 'description' }],
order: [[ 1, "asc" ]]
};
}
}
我希望能够将当前页面索引传递给服务器端 api。谁能指出我正确的方向?我可以像这样显示当前页面索引:
{{ (datatableElement.dtInstance | async)?.table().page.info().page }}
但我不知道如何在进行 ajax 调用之前访问页面信息。
【问题讨论】:
-
如果包含标记会更好
-
另外你需要使用分页器
-
这可能会有所帮助,但这是在 AngularJS 中:stackoverflow.com/questions/39161117/…
-
当您单击页面按钮(示例第 2 页)时,请尝试在方法中传递
$event并告诉我们您在其中得到了什么。 -
为什么需要当前页面?
dataTablesParameters中有两个参数,即start和length。如果您想要特定页面的项目(即单击页码),请在后端过滤查询集,例如queryset[start:start + length]将完成这项工作。
标签: angular angular6 angular-datatables