我正在创建一个带有Angular datatables (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调用之前访问页面信息。
发布于 2019-05-08 21:07:05
你可以通过使用ajax参数来获得页面编号,看看下面的代码,你会得到一个想法。
ajax: (dataTablesParameters: any, callback) => {
    const page = parseInt(dataTablesParameters.start) / parseInt(dataTablesParameters.length) + 1;
    const rowData = {
      no_of_records: dataTablesParameters.length,
      page: page,
      group_id: 1
    };
    that.http
      .post<DataTablesResponse>(
        'http://localhost:3030/role/list',
        rowData, {}
      ).subscribe(resp => {
        console.log(resp);
        that.persons = resp.data;
        callback({
          recordsTotal: 10,
          recordsFiltered: 20,
          data: []
        });
      });
  },发布于 2018-07-30 23:33:02
在dataTablesParameters中传递的值不包括当前页。您可以从传递的参数中使用(start + length) + 1来计算。希望这能有所帮助!
发布于 2018-06-15 21:37:06
我只是错过了这一点。所有属性,如顺序、方向、页面大小等都在主体中随post一起发送(dataTablesParameters变量):
ajax: (dataTablesParameters: any, callback) => {https://stackoverflow.com/questions/50862089
复制相似问题