0

我正在 Visual Studio 中使用 Kendo 网格控件构建 Angular 2 应用程序。我已经编写了分页功能的代码,但它似乎不起作用。有人可以帮忙吗?

风险列表.component.ts

import { Component, OnInit } from '@angular/core';
import { Risk } from './risk';
import { RiskService } from './risk.service';
import { GridModule, GridDataResult, PageChangeEvent, SortDescriptor, orderBy} from '@progress/kendo-angular-grid';

@Component({
    moduleId: module.id,
    selector: 'rm-risks',
    templateUrl: '/app/risk-list.component.html',
    providers: [RiskService]
})

export class RiskListComponent implements OnInit {
    private gridView: GridDataResult;
    private sort: SortDescriptor[] = [];
    private data: any[];
    private pageSize: number = 10;
    private skip: number = 0;
    title = 'Risk List';
    risks: Risk[];


    constructor(private _riskService: RiskService) {
        this.data = [];
        this.getRisks();
        this.loadRisks();
     }

    protected pageChange(event: PageChangeEvent): void {
        this.skip = event.skip;
        this.loadRisks();
    }

    protected sortChange(sort: SortDescriptor[]): void {
        this.sort = sort;
        this.loadRisks();
    }


    private loadRisks(): void {
        this.gridView = {
            data: this.data.slice(this.skip, this.skip + this.pageSize), 
           // data: orderBy(this.risks, this.sort),
            total: this.data.length
        };
    }

    getRisks(): void {
        this._riskService.getRisks().then(risks => this.risks = risks);

    }

    ngOnInit(): void {
        this.getRisks();
    }
};

风险列表.component.html

<kendo-grid [data]="risks"
            [skip]="skip"
            [pageSize]="pageSize"
            [scrollable]="'virtual'"
            [rowHeight]="36"
            [height]="300"
            (pageChange)="pageChange($event)"
            [pageable]="true"   
            [sortable]="{ mode: 'multiple' }"
            [sort]="sort"
            (sortChange)="sortChange($event)">
    <kendo-grid-column field="reference" title="Reference" width="120">
    </kendo-grid-column>
    <kendo-grid-column field="insuredName" title="Insured Name" width="120">
    </kendo-grid-column>
    <kendo-grid-column field="inceptionDate" title="Inception Date" width="120">
    </kendo-grid-column>
    <kendo-grid-column field="riskType" title="Risk Type" width="120">
    </kendo-grid-column>
    <kendo-grid-column field="status" title="Indication" width="120">
    </kendo-grid-column>
    <kendo-grid-column field="grossPremium" title="Gross Premium" width="120">
    </kendo-grid-column>
    <kendo-grid-column field="allocatedTo" title="Allocated To" width="120">
    </kendo-grid-column>
    <kendo-grid-column field="allocatedCompany" title="Allocated Company" width="120">
    </kendo-grid-column>
    <kendo-grid-column field="Discontinued" width="100">
        <template kendoCellTemplate let-dataItem>
            <input type="checkbox" [checked]="dataItem.Discontinued" disabled />
        </template>
    </kendo-grid-column>
</kendo-grid>

风险服务.ts

import { Injectable } from '@angular/core';
import { Risk } from './risk';
import { Risks } from './mock-risk';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/from';


@Injectable()
export class RiskService {

    getRisks(): Promise<Risk[]> {
        return Promise.resolve(Risks);
    }

}
4

2 回答 2

1

您不能在 KendoGrid 中同时进行虚拟滚动分页。

启用[scrollable]="'virtual'"后,它将隐藏分页。

如果您正在寻找分页,然后删除[scrollable]="'virtual'"

你可以参考这个

于 2016-10-23T10:26:18.273 回答
0

我认为您没有将风险数据分配给 this.data 数组,并且您正在尝试使用 this.data.slice(this.skip, this.skip + this.pageSize) 进行切片,这将是空值

试试下面的代码

 export class RiskListComponent implements OnInit {
private gridView: GridDataResult;
private sort: SortDescriptor[] = [];
private data: any[];
private pageSize: number = 10;
private skip: number = 0;
title = 'Risk List';
risks: Risk[];
risks1: Risk[];
view;

constructor(private _riskService: RiskService) {
    this.data = [];
    this.getRisks();
    this.loadRisks();
 }

protected pageChange(event: PageChangeEvent): void {
    this.skip = event.skip;
    this.loadRisks();
}

protected sortChange(sort: SortDescriptor[]): void {
    this.sort = sort;
    this.loadRisks();
}


private loadRisks(): void {
    this.view= {
        data: this.risks1.slice(this.skip, this.skip + this.pageSize), 
       // data: orderBy(this.risks, this.sort),
        total: this.risks1.length
    };
}

getRisks(): void {
    this._riskService.getRisks().then(risks => this.risks1 = risks);

}

ngOnInit(): void {
    this.getRisks();
}

};

在 Risk-listcomponent.html 而不是风险中,将视图分配给 [数据]

<kendo-grid [data]="view"
        [skip]="skip"
  ....
于 2016-10-23T12:32:19.153 回答