我有一個酒店預訂項目,前端使用angular2創建,後端使用laravel創建,它是一個API系統。我有一個在angular2中列出的表格。爲此,我從laravel獲取了預訂數據並將其綁定在了角度表內,但我不知道分頁是如何通過api工作的。最初我只提取並綁定了15個數據。點擊下一頁時如何獲得更多數據。這裏是laravel角的代碼。api系統的分頁工作原理(前端是angular2,後端是laravel5.4)?
Laravel
public function index()
{
$bookings = Booking::where('is_delete', 0)
->paginate(15);
return response()->json(['bookingDetails' => $bookings], 200);
}
Angular2
booking.service.ts
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import { Bookings } from './booking';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import 'rxjs/Rx';
import { Observable } from "rxjs";
@Injectable()
export class BookingService {
private headers = new Headers({'Content-Type': 'application/json'});
private _url: string = 'http://cabinapi.app/api/bookings/';
/*private _url: string = 'apidata/testData.json';*/
constructor(private http: Http) { }
getBooking(): Observable<any> {
return this.http.get(this._url)
.map((response: Response) => response.json().bookingDetails.data as Bookings[])
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
}
booking.component.ts
import { Component, OnInit } from '@angular/core';
import { BookingService } from './booking.service';
import { Bookings } from './booking';
@Component({
selector: 'booking',
templateUrl: './booking.component.html'
})
export class BookingComponent implements OnInit {
bookings: Bookings[];
constructor(private employeeService: BookingService) {}
getBooking(): void {
/*this.employeeService.getEmployee().then(employees => this.employees = employees);*/
this.employeeService.getBooking().subscribe(bookings => this.bookings = bookings);
}
ngOnInit(): void {
this.getBooking();
setTimeout(function() {
$(function() {
$("#dataTable").DataTable();
});
}, 1000);
}
}
感謝您的回覆。是的,你的迴應就像你提到的那樣。但我懷疑它是如何在我的問題中提到的我的代碼在前端工作。 –