我正在尝试使用pagination
的{{1}}。我面临的问题是通过服务动态设置angular-material2
的值。
我已设法使用静态值创建分页,但我不知道如何动态设置它们。
TS
md-paginator
HTML
import {Component,ChangeDetectorRef, Input,ViewChild } from '@angular/core';
import {Http, Response, RequestOptions, Headers, Request, RequestMethod} from '@angular/http';
import {MdPaginator,PageEvent} from '@angular/material';
import {DataSource} from '@angular/cdk';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/observable/merge';
import 'rxjs/add/operator/map';
@Component({
selector: 'table-http-example',
styleUrls: ['table-http-example.css'],
templateUrl: 'table-http-example.html',
})
export class TableHttpExample {
displayedColumns = ['dbquery', 'title'];
exampleDatabase: ExampleHttpDatabase | null;
dataSource: ExampleDataSource | null;
pageEvent: PageEvent;
@ViewChild(MdPaginator) paginator: MdPaginator;
constructor(private http: Http) {
}
ngOnInit(){
this.loadData();
}
loadData(){
this.exampleDatabase = new ExampleHttpDatabase(this.http);
this.dataSource = new ExampleDataSource(this.exampleDatabase,this.paginator);
}
onPaginateChange(event){
const startIndex = event.pageIndex * event.pageSize;
this.dataSource = new ExampleDataSource(this.exampleDatabase,this.paginator);
}
}
/** An example database that the data source uses to retrieve data for the table. */
export class ExampleHttpDatabase {
private issuesUrl = 'https://emgrweb.ecwcloud.com/eManagerWeb/rest/InterfaceQAClientController/getInterfaceClientScripts'; // URL to web API
getRepoIssues(startIndex,pageSize): Observable<any> {
var headers = new Headers();
headers.append('Content-Type', 'text/plain');
var requestoptions = new RequestOptions({
method: RequestMethod.Post,
url: this.issuesUrl,
headers: headers
body : "start=" +startIndex +"&limit="+ pageSize
})
return this.http.request(new Request(requestoptions)).map(this.extractData);
}
extractData(result: Response): any{
return result.json().data.map(data => {
return data
});
}
onPaginateChange(event){
//alert(JSON.stringify(event));
const startIndex = event.pageIndex * event.pageSize;
// this.drugmap.getDrugDataForClient(startIndex, event.pageSize);
this.dataSource = new BindDataTableSource(this.drugmap,event);
}
constructor(private http: Http) {}
}
export class ExampleDataSource extends DataSource<any> {
constructor(private _exampleDatabase: ExampleHttpDatabase,private _paginator : MdPaginator) {
super();
}
/** Connect function called by the table to retrieve one stream containing the data to render. */
connect(): Observable<any[]> {
console.log( this._paginator);
const startIndex = this._paginator.pageIndex * this._paginator.pageSize;
return this._exampleDatabase.getRepoIssues(startIndex, this._paginator.pageSize);
}
disconnect() {}
}
尝试将<div class="example-container mat-elevation-z8">
<md-table #table [dataSource]="dataSource">
<ng-container cdkColumnDef="dbquery">
<md-header-cell *cdkHeaderCellDef> dbquery </md-header-cell>
<md-cell *cdkCellDef="let row"> {{row.title}} </md-cell>
</ng-container>
<ng-container cdkColumnDef="title">
<md-header-cell *cdkHeaderCellDef> Title </md-header-cell>
<md-cell *cdkCellDef="let row"> {{row.title}} </md-cell>
</ng-container>
<md-header-row *cdkHeaderRowDef="displayedColumns"></md-header-row>
<md-row *cdkRowDef="let row; columns: displayedColumns;"></md-row>
</md-table>
<md-paginator #paginator
[length]="100"
[pageIndex]="0"
[pageSize]="10"
[pageSizeOptions]="[5, 10, 25, 100]"
(page)="pageEvent = $event; onPaginateChange($event)"
>
</md-paginator>
</div>
对象传递给paginator
,但当我ExampleHttpDatabase
出来时,它表示未定义。
答案 0 :(得分:9)
要动态更改值,请使用变量作为<md-paginator>
的输入。当这些变量发生变化时,分页器将拾取它们并将它们渲染到视图中。
<md-paginator #paginator
[length]="myExampleLength"
[pageIndex]="myExamplePageIndex"
[pageSize]="myExamplePageSize"
[pageSizeOptions]="myExamplePageSizeOptions"
(page)="pageEvent = $event; onPaginateChange($event)">
</md-paginator>