我有一个酒店预订项目,前端使用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);
}
}
感谢您的回复。是的,你的回应就像你提到的那样。但我怀疑它是如何在我的问题中提到的我的代码在前端工作。 –