jQWidgets Forums
jQuery UI Widgets › Forums › Grid › How to use virtualmode with Server-Side paging
This topic contains 3 replies, has 2 voices, and was last updated by Hristo 5 years ago.
-
Author
-
1. The
app.component.html
defines thejqxGrid
withvirtualmode
attribute set totrue
:<jqxGrid #myGrid [source]="dataAdapter" [columns]="columns" [autoheight]="true" [pageable]="true" [virtualmode]="true" [rendergridrows]="rendergridrows"> </jqxGrid>
2. The
app.component.ts
imports theHttpService
and uses itsgenerateData
method to get the Productsjson
data:import { Component, ViewChild, ElementRef, AfterViewInit, OnInit } from '@angular/core'; import { HttpService } from './http.service'; import { jqxGridComponent } from 'jqwidgets-ng/jqxgrid' @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { @ViewChild('myGrid', { static: false }) myGrid: jqxGridComponent; source: any = { datatype: 'array', localdata: {}, totalrecords: 1000000 } columns: any[] = [ { text: 'Id', datafield: 'id', width: 50 }, { text: 'First Name', datafield: 'firstname', width: 120 }, { text: 'Last Name', datafield: 'lastname', width: 120 }, { text: 'Product', datafield: 'productname', width: 180 }, { text: 'Quantity', datafield: 'quantity', width: 100, cellsalign: 'right' }, { text: 'Unit Price', datafield: 'price', width: 100, cellsalign: 'right', cellsformat: 'c2' } ]; dataAdapter: any = new jqx.dataAdapter(this.source); constructor(private http: HttpService) { } rendergridrows = (params: any): any[] => { let data = this.http.generateData(params.startindex, params.endindex); return data; } }
3. The
http.service.ts
script returns the jsondata
stored indata
variable:import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs/internal/Observable'; @Injectable({ providedIn: 'root' }) export class HttpService { constructor(private http: HttpClient) { } generateData(startindex: number, endindex: number): any { let data = [ {"id": 1, "firstname": "1 Regina", "lastname": "Bein", "productname": "Cappuccino", "price": 1, "quantity": 11}, {"id": 2, "firstname": "2 Peter", "lastname": "Wilson", "productname": "Doubleshot", "price": 2, "quantity": 12}, {"id": 3, "firstname": "3 Sven", "lastname": "Vileid", "productname": "Espresso", "price": 3, "quantity": 13}, {"id": 4, "firstname": "4 Nancy", "lastname": "Winkler", "productname": "Cramel", "price": 4, "quantity": 14}, {"id": 5, "firstname": "5 Regina", "lastname": "Winkler", "productname": "Caffe", "price": 5, "quantity": 15}, {"id": 6, "firstname": "6 Petra2", "lastname": "Bein", "productname": "Tea", "price": 6, "quantity": 16}, {"id": 7, "firstname": "7 Regina", "lastname": "Peterson", "productname": "Latte", "price": 7, "quantity": 17}, {"id": 8, "firstname": "8 Beate", "lastname": "Fuller", "productname": "Espresso", "price": 8, "quantity": 18}, {"id": 9, "firstname": "9 Shelley", "lastname": "Ohno", "productname": "Truffle", "price": 9, "quantity": 19}, {"id": 10, "firstname": "10 Petra", "lastname": "Murphy", "productname": "Saavedra", "price": 101, "quantity": 20} ] return data; } }
Now, instead of storing the json in
data
value I would like to make ahttp
request to http server. To mimic it, I replace thegenerate
method with:generateData(startindex: number, endindex: number): Observable<any> { return this.http.get('./assets/product.json'); }
with the
product.json
file used to store thejson
data and it looks like:[ {"id": 1, "firstname": "1 Regina", "lastname": "Bein", "productname": "Cappuccino", "price": 1, "quantity": 11}, {"id": 1, "firstname": "2 Peter", "lastname": "Wilson", "productname": "Doubleshot", "price": 2, "quantity": 12}, {"id": 1, "firstname": "3 Sven", "lastname": "Vileid", "productname": "Espresso", "price": 3, "quantity": 13}, {"id": 1, "firstname": "4 Nancy", "lastname": "Winkler", "productname": "Cramel", "price": 4, "quantity": 14}, {"id": 1, "firstname": "5 Regina", "lastname": "Winkler", "productname": "Caffe", "price": 5, "quantity": 15}, {"id": 1, "firstname": "6 Petra2", "lastname": "Bein", "productname": "Tea", "price": 6, "quantity": 16}, {"id": 1, "firstname": "7 Regina", "lastname": "Peterson", "productname": "Latte", "price": 7, "quantity": 17}, {"id": 1, "firstname": "8 Beate", "lastname": "Fuller", "productname": "Espresso", "price": 8, "quantity": 18}, {"id": 1, "firstname": "9 Shelley", "lastname": "Ohno", "productname": "Truffle", "price": 9, "quantity": 19}, {"id": 1, "firstname": "10 Petra", "lastname": "Murphy", "productname": "Saavedra", "price": 101, "quantity": 20} ]
Now instead of the regular
json
data thegenerateData
method returns theObservable
object. But it looks like, therendergridrows
method doesn’t know what to do with theObservable
object returned by thegenerateData
method and grid shows no data.
How to make thevirtualmode
andrendergridrows
work with the http requests needed to query data from the remote server?Hello jqwidgetuser,
If you change the source you need to use a new DataAdapter.
I would like to suggest you one tutorial for Server-Side paging.
Please, take a look at this tutorial:
https://www.jqwidgets.com/angular-components-documentation/documentation/angular-serverside/angular-serverside-paging.htm?search=Best Regards,
Hristo HristovjQWidgets team
https://www.jqwidgets.comThank you for the info!
According to this Angular Server-Side Paging tutorial, instead of using thesource.localdata
attribute thesource.url
attribute of thedataAdapter
should be used. ThedataAdapter
will be sending the http-requests to the server directly.
How do we access the data received by the dataAdapter from http server? Is there a way to monitor when the data was received (it can take some time for the data to be prepred by the server and to be returned back)?Hello jqwidgetuser,
The jqxDataAdapter provides useful callbacks, for example, the
downloadComplete
callback will be useful.
More details about it you could find in the API Documentation page:
https://www.jqwidgets.com/jquery-widgets-documentation/documentation/jqxdataadapter/jquery-data-adapter.htm?search=dataadapter
If you want to add extra parameters to the HTTP request then theformatData
callback will be useful.Best Regards,
Hristo HristovjQWidgets team
https://www.jqwidgets.com -
AuthorPosts
You must be logged in to reply to this topic.