Datatable with pagination server side

Asked

Viewed 1,176 times

0

I searched a lot on Google but I did not have a clear path of how to make a datatable ( angular use 2 ) that contains pagination by the server, or seka, that each page he made an individual request of the same.

All I saw, makes the php request with the back-end, but brings all records in JSON at once, IE, if there are 100,000 lines it takes all of them.

I would like to develop a table that on each page ( of 30 records ) makes a request to get the JSON of the next records. The back-end part is quiet, I only need one way to do this page at the angle, how to request each request. I believe that first I will have to take a total of lines and do the division of pages, then the home page do a req http type: companies/2(page)/30 (records) to return the records of that interval.

I’ve done this with Laravel (php) through his native paging with bootstrap, but now I’m doing the front end at angular 2 and I’ll have to change.

Updating:

as the photo, ex my back-end: back-end

My Service to get http:(it works on the.log() console, but I’m unable to pass the answer to a variable and then send it to view. I’m adventuring now in Angular, mainly typescript, I’m seeing several video-lessons and tals but complicated, my area is PHP.

Service.ts

import { Injectable } from '@angular/core'; import { Http, Response, Headers, Requestoptions } from '@angular/http'; import {Observable} from 'rxjs/Rx'; //import {Company} from '.. /models/companies';

// Import Rxjs required methods import 'rxjs/add/Operator/map'; //import 'rxjs/add/Operator/catch';

@Injectable() export class Empresascrudservice { //private commentsUrl = 'http://localhost:8000/api/enterprise';

constructor(private _http: Http) { }

_endpoint_url: string = 'http://localhost/16-11/Sysferaser5.3/public/data/4';
getEmpresas(page:number) { Return this. _http.get(this._endpoint_url + "?page=" + page) . map(Sponse => Sponse.json()); }

}

companies-index.component.ts:

import { Component, Oninit } from '@angular/core';

import { Empresascrudservice } from '. /services/crud.service'; import { Http, Response, Headers, Requestoptions } from '@angular/http';

@Component(? selector: 'enterprises-index', templateUrl: 'app/registrations/companies/companies-index.component.html' }) export class Empresasindexcomponent Implements Oninit{

constructor(private dataService: EmpresasCrudService) {

this._service = dataService;   }
public _service;
public _data;
public _total;
public _page;

getEmps(page: number){

    this._service.getEmpresas(page)
        .subscribe(
        people => {this._data = people.data, this._total = people.total},
        error => console.error('Error: ' + error),
        () => console.log('Completed!')
    );
    this._page = page;
}

ngOnInit(){
  this.getEmps(1);

}

}

enterprise-index.component.html

<div class="panel panel-default">
<div class="panel-heading">
    <h3 class="panel-title">
        Panel title
    </h3>
</div>
<div class="panel-body">
    Total: {{ _total }} <br>
    Página: {{ _page }}
    <ul>
  <li *ngFor="let item of _data">{{item.empresa_id}}</li>
</ul><br><br>
</div>
<div class="panel-footer">
    Panel footer
</div>

  • 1

    I was able to do but a question arose in the pagination, but for those who want to see the code is in the other question: <https://answall.com/questions/166128/pagina%C3%A7%C3%A3o-angular-2-server-side> I did with the library that the friend indicated

1 answer

2


Last week I was doing some studies, and I created a Rails 5 API JSON project with about 100 lines, and I sent 10 out of 10 pages.

Then I did a project in Angular 2 to read these lines, and take the data and paginate.

If what I understand you want to do is the same I did, this module will facilitate your work

https://github.com/michaelbromley/ng2-pagination

In your Component, create a method to pull the number of lines from the API via Service

  getPage(page: number) {
    this._data = this.postService.getPosts(page)
      .do((res: any) => {
        this._total = res.json().total;
        this._page = page;
      })
      .map((res: any) => res.json().data);
  }

  ngOnInit() {
    this.getPage(1);
  }

My service contains only one method searching the API URL data

  _endpoint_url: string = 'http://192.168.2.3:3030/api/v1/posts';
  getPosts(page) {
    return this._http.get(this._endpoint_url + "?page=" + page)
  }

Remembering that your API should contain a property passing the TOTAL of records it contains. inserir a descrição da imagem aqui

My example is ugly and poorly written because it is just tests, studies, I do in my local machine to learn, since I am unemployed, but, stop learning, never :-) !

  • Beauty buddy, so test warning here

  • That’s really what I need, but I’m still struggling...

  • I started the back-end photo, I created a service that makes the request ajax... but I’m not able to understand to implement in companies-index.Component -> to show the table

  • Also share your Component.ts . html and your service to see how you are implementing. Apparently your endpoint is correct, showing the total number of lines.

  • After so much research, everything worked out, solved the issue, thank you for your attention

  • Share how you did it, so others can use this post to solve the same problem

  • You haven’t given +1 in my answer, if you can do this, if she’s been helpful, I’m grateful.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.