DataViewDataView displays data in grid or list layout with pagination, sorting and filtering features.

DataView - 图1

Documentation

Import

  1. import {DataViewModule} from 'primeng/dataview';
  2.  

Getting Started

DataView requires a collection of items as its value and one or more templates depending on the layout mode e.g. list and grid.

Throughout the samples, a car interface having vin, brand, year and color properties are used to define an object to be displayed by the dataview. Cars are loaded by a CarService that connects to a server to fetch the cars.

  1. export interface Car {
  2. vin;
  3. year;
  4. brand;
  5. color;
  6. }
  7.  
  1. import {Injectable} from 'angular2/core';
  2. import {Http, Response} from 'angular2/http';
  3. import {Car} from '../domain/car';
  4. @Injectable()
  5. export class CarService {
  6. constructor(private http: Http) {}
  7. getCarsLarge() {
  8. return this.http.get('/showcase/resources/data/cars-large.json')
  9. .toPromise()
  10. .then(res => <Car[]> res.json().data)
  11. .then(data => { return data; });
  12. }
  13. }
  14.  
  1. export class DataViewDemo implements OnInit {
  2. cars: Car[];
  3. constructor(private carService: CarService) { }
  4. ngOnInit() {
  5. this.carService.getCarsLarge().then(cars => this.cars = cars);
  6. }
  7. }
  8.  
  1. <p-dataView [value]="cars">
  2. <ng-template let-car pTemplate="listItem">
  3. <div>
  4. {{car.id}}
  5. </div>
  6. </ng-template>
  7. </p-dataView>
  8.  

Layouts

DataView has two layout modes; "list" and "grid" where a separate template is used to render an item in each mode. In list mode name of the template is "listItem" whereas in grid mode it is "gridItem". In grid mode, the ng-template element should contain a div element as a wrapper with Grid CSS style class of your choice.

Note that there is no restriction to use both layouts at the same time, you may configure only one layout using the layout property with the corresponding ng-template.

  1. <p-dataView [value]="cars">
  2. <ng-template let-car pTemplate="listItem">
  3. <div>
  4. {{car.id}}
  5. </div>
  6. </ng-template>
  7. <ng-template let-car pTemplate="gridItem">
  8. <div class="ui-g-12 ui-md-3">
  9. {{car.year}}
  10. </div>
  11. </ng-template>
  12. </p-dataView>
  13.  

Sections

Header and Footer are the two sections that are capable of displaying custom content.

  1. <p-dataView [value]="cars">
  2. <p-header>List of Cars</p-header>
  3. <p-footer>Choose from the list.</p-footer>
  4. <ng-template let-car pTemplate="listItem">
  5. <div>
  6. {{car.id}}
  7. </div>
  8. </ng-template>
  9. <ng-template let-car pTemplate="gridItem">
  10. <div class="ui-g-12 ui-md-3">
  11. {{car.year}}
  12. </div>
  13. </ng-template>
  14. </p-dataView>
  15.  

DataViewLayoutOptions

When both layout modes are enabled in DataView, a UI element would be necessary to let the user toggle between the view. p-dataViewLayoutOptions is a helper component to display a buttonset to choose the layout mode in DataView. Location of the p-dataViewLayoutOptions should be inside the DataView component. If you prefer a different UI element you can create your own that updates the layout property of the DataView.

  1. <p-dataView [value]="cars">
  2. <p-header>
  3. <p-dataViewLayoutOptions></p-dataViewLayoutOptions>
  4. </p-header>
  5. <p-footer>
  6. <p-dataViewLayoutOptions></p-dataViewLayoutOptions>
  7. </p-footer>
  8. <ng-template let-car pTemplate="listItem">
  9. <div>
  10. {{car.id}}
  11. </div>
  12. </ng-template>
  13. <ng-template let-car pTemplate="gridItem">
  14. <div class="ui-g-12 ui-md-3">
  15. {{car.year}}
  16. </div>
  17. </ng-template>
  18. </p-dataView>
  19.  

Paginator

Pagination is enabled by setting paginator property to true, rows attribute defines the number of rows per page and pageLinks specify the the number of page links to display. To customize the left and right side of the paginators, use "paginatorLeftTemplate" and "paginatorRightTemplate" templates.

  1. <p-dataView [value]="cars" [paginator]="true" [rows]="10">
  2. <ng-template let-car pTemplate="listItem">
  3. <div>
  4. {{car.id}}
  5. </div>
  6. </ng-template>
  7. <ng-template let-car pTemplate="gridItem">
  8. <div class="ui-g-12 ui-md-3">
  9. {{car.year}}
  10. </div>
  11. </ng-template>
  12. <ng-template pTemplate="paginatorleft" let-state>
  13. {{state.first}}
  14. <button type="button" pButton icon="fa-refresh"></button>
  15. </ng-template>
  16. <ng-template pTemplate="paginatorright">
  17. <button type="button" pButton icon="fa-cloud-upload"></button>
  18. </ng-template>
  19. </p-dataView>
  20.  

Lazy Loading

Lazy mode is handy to deal with large datasets, instead of loading the entire data, small chunks of data is loaded by invoking onLazyLoad callback everytime paging happens. To implement lazy loading, enable lazy attribute and provide a method callback using onLazyLoad that actually loads the data from a remote datasource. onLazyLoad gets an event object that contains information about what to load. It is also important to assign the logical number of rows to totalRecords by doing a projection query for paginator configuration so that paginator displays the UI assuming there are actually records of totalRecords size although in reality they aren't as in lazy mode, only the records that are displayed on the current page exist.

  1. <p-dataView [value]="cars" [paginator]="true" [rows]="10"
  2. [lazy]="true" (onLazyLoad)="loadData($event)" [totalRecords]="totalRecords">
  3. <ng-template let-car pTemplate="listItem">
  4. <div>
  5. {{car.id}}
  6. </div>
  7. </ng-template>
  8. <ng-template let-car pTemplate="gridItem">
  9. <div class="ui-g-12 ui-md-3">
  10. {{car.year}}
  11. </div>
  12. </ng-template>
  13. </p-dataView>
  14.  
  1. loadData(event) {
  2. //event.first = First row offset
  3. //event.rows = Number of rows per page
  4. }
  5.  

Sorting

sortField and sortOrder properties are available for sorting functionality, for flexibility there is no built-in UI available so that a custom UI can be used for the sorting element. Here is an example that uses a dropdown where simply updating the sortField-sortOrder bindings of the DataView initiates sorting.

  1. <p-dataView [value]="cars" [sortField]="sortField" [sortOrder]="sortOrder">
  2. <p-header>
  3. <p-dropdown [options]="sortOptions" [(ngModel)]="sortKey" placeholder="Sort By"
  4. (onChange)="onSortChange($event)" [style]="{'min-width':'15em'}"></p-dropdown>
  5. </p-header>
  6. <ng-template let-car pTemplate="listItem">
  7. <div>
  8. {{car.id}}
  9. </div>
  10. </ng-template>
  11. <ng-template let-car pTemplate="gridItem">
  12. <div class="ui-g-12 ui-md-3">
  13. {{car.year}}
  14. </div>
  15. </ng-template>
  16. </p-dataView>
  17.  
  1. export class DataViewSortDemo implements OnInit {
  2. cars: Car[];
  3. sortOptions: SelectItem[];
  4. sortKey: string;
  5. sortField: string;
  6. sortOrder: number;
  7. constructor(private carService: CarService) { }
  8. ngOnInit() {
  9. this.carService.getCarsLarge().then(cars => this.cars = cars);
  10. this.sortOptions = [
  11. {label: 'Newest First', value: '!year'},
  12. {label: 'Oldest First', value: 'year'},
  13. {label: 'Brand', value: 'brand'}
  14. ];
  15. }
  16. onSortChange(event) {
  17. let value = event.value;
  18. if (value.indexOf('!') === 0) {
  19. this.sortOrder = -1;
  20. this.sortField = value.substring(1, value.length);
  21. }
  22. else {
  23. this.sortOrder = 1;
  24. this.sortField = value;
  25. }
  26. }
  27. }
  28.  

Filtering

Filtering is implemented by defining the filterBy property and calling the filter function of the component, for flexibility there is no built-in UI available so that a custom UI can be used for the filter element. Here is an example that uses an input field. filterBy is a string and multiple fields can be defined with a comma separated list.

  1. <p-dataView #dv [value]="cars" filterBy="brand">
  2. <p-header>
  3. <input type="search" pInputText placeholder="Search by brand" (keyup)="dv.filter($event.target.value)">
  4. </p-header>
  5. <ng-template let-car pTemplate="listItem">
  6. {{car.id}}
  7. </ng-template>
  8. <ng-template let-car pTemplate="gridItem">
  9. {{car.year}}
  10. </ng-template>
  11. </p-dataView>
  12.  

Custom UI Elements

As mentioned above, layout options selector, sorting and filtering are baked-in and no strict UI is enforces to make it possible to come up with your own UI elements to enable these features.

Loading Status

DataView has a loading property, when enabled a spinner icon is displayed to indicate data load. An optional loadingIcon property can be passed in case you'd like a different loading icon.

  1. <p-dataview [value]="cars" [loading]="loading">
  2. //content
  3. </p-dataview>
  4.  

Properties

NameTypeDefaultDescription
valuearraynullAn array of objects to display.
layoutstringlistLayout of the items, valid values are "list" and "grid".
paginatorbooleanfalseWhen specified as true, enables the pagination.
rowsnumbernullNumber of rows to display per page.
totalRecordsnumbernullNumber of total records, defaults to length of value when not defined.
pageLinksnumber5Number of page links to display in paginator.
rowsPerPageOptionsarraynullArray of integer values to display inside rows per page dropdown of paginator
paginatorPositionstringbottomPosition of the paginator, options are "top","bottom" or "both".
alwaysShowPaginatorbooleantrueWhether to show it even there is only one page.
paginatorDropdownAppendToanynullTarget element to attach the paginator dropdown overlay, valid values are "body" or a local ng-template variable of another element.
currentPageReportTemplatestring{currentPage} of {totalPages}Text to display the current page information.
showCurrentPageReportbooleanfalseWhether to display current page report.
lazybooleanfalseDefines if data is loaded and interacted with in lazy manner.
emptyMessagestringNo records found.Text to display when there is no data.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
trackByFunctionnullFunction to optimize the dom operations by delegating to ngForTrackBy, default algoritm checks for object identity.
filterBystringnullComma separated list of fields in the object graph to search against.
loadingbooleanfalseDisplays a loader to indicate data load is in progress.
loadingIconstringfa-circle-o-notchThe icon to show while indicating data load is in progress.
firstnumber0Index of the first row to be displayed.

Events

NameParametersDescription
onLazyLoadevent.first = First row offset event.rows = Number of rows per page Callback to invoke when paging, sorting or filtering happens in lazy mode.
onPageevent.first: Index of first record in page event.rows: Number of rows on the pageCallback to invoke when pagination occurs.
onSortevent.field: Name of the sort field. event.order: Order of the sort.Callback to invoke when sorting occurs.

Styling

Following is the list of structural style classes, for theming classes visit theming page.

NameElement
ui-dataviewContainer element.
ui-dataview-headerHeader section.
ui-dataview-footerFooter section.
ui-dataview-contentContainer of items.
ui-table-loadingLoader mask.
ui-dataview-loadingLoader background.
ui-dataview-loading-contentLoader content.
ui-dataview-emptymessageEmpty message element.

Dependencies

None.

Source

View on GitHub

  1. <p-dataView #dv [value]="cars" [paginator]="true" [rows]="20" paginatorPosition="both" filterBy="brand"
  2. [sortField]="sortField" [sortOrder]="sortOrder">
  3. <p-header>
  4. <div class="ui-helper-clearfix">
  5. <div class="ui-g">
  6. <div class="ui-g-12 ui-md-4">
  7. <p-dropdown [options]="sortOptions" [(ngModel)]="sortKey" placeholder="Sort By" (onChange)="onSortChange($event)" [style]="{'min-width':'140px'}"></p-dropdown>
  8. </div>
  9. <div class="ui-g-6 ui-md-4 filter-container">
  10. <div style="position:relative">
  11. <input type="search" pInputText placeholder="Search by brand" (keyup)="dv.filter($event.target.value)">
  12. </div>
  13. </div>
  14. <div class="ui-g-6 ui-md-4" style="text-align:right">
  15. <p-dataViewLayoutOptions></p-dataViewLayoutOptions>
  16. </div>
  17. </div>
  18. </div>
  19. </p-header>
  20. <ng-template let-car pTemplate="listItem">
  21. <div class="ui-g" style="padding: 2em;border-bottom: 1px solid #d9d9d9">
  22. <div class="ui-g-12 ui-md-3" style="text-align:center">
  23. <img src="assets/showcase/images/demo/car/{{car.brand}}.png">
  24. </div>
  25. <div class="ui-g-12 ui-md-8 car-details">
  26. <div class="ui-g">
  27. <div class="ui-g-2 ui-sm-6">Vin: </div>
  28. <div class="ui-g-10 ui-sm-6"><b>{{car.vin}}</b></div>
  29. <div class="ui-g-2 ui-sm-6">Year: </div>
  30. <div class="ui-g-10 ui-sm-6"><b>{{car.year}}</b></div>
  31. <div class="ui-g-2 ui-sm-6">Brand: </div>
  32. <div class="ui-g-10 ui-sm-6"><b>{{car.brand}}</b></div>
  33. <div class="ui-g-2 ui-sm-6">Color: </div>
  34. <div class="ui-g-10 ui-sm-6"><b>{{car.color}}</b></div>
  35. </div>
  36. </div>
  37. <div class="ui-g-12 ui-md-1 search-icon">
  38. <button pButton type="button" icon="pi pi-search" (click)="selectCar($event, car)"></button>
  39. </div>
  40. </div>
  41. </ng-template>
  42. <ng-template let-car pTemplate="gridItem">
  43. <div style="padding:.5em" class="ui-g-12 ui-md-3">
  44. <p-panel [header]="car.vin" [style]="{'text-align':'center'}">
  45. <img src="assets/showcase/images/demo/car/{{car.brand}}.png" width="60">
  46. <div class="car-detail">{{car.year}} - {{car.color}}</div>
  47. <hr class="ui-widget-content" style="border-top:0">
  48. <button pButton type="button" icon="pi pi-search" (click)="selectCar($event, car)" style="margin-top:0"></button>
  49. </p-panel>
  50. </div>
  51. </ng-template>
  52. </p-dataView>
  53. <p-dialog header="Car Details" [(visible)]="displayDialog" [responsive]="true" showEffect="fade" [modal]="true" [style]="{width: '225px'}" (onAfterHide)="onDialogHide()">
  54. <div class="ui-g" *ngIf="selectedCar">
  55. <div class="ui-g-12" style="text-align:center">
  56. <img src="assets/showcase/images/demo/car/{{selectedCar.brand}}.png">
  57. </div>
  58. <div class="ui-g-4">Vin: </div>
  59. <div class="ui-g-8">{{selectedCar.vin}}</div>
  60. <div class="ui-g-4">Brand: </div>
  61. <div class="ui-g-8">{{selectedCar.brand}}</div>
  62. <div class="ui-g-4">Year: </div>
  63. <div class="ui-g-8">{{selectedCar.year}}</div>
  64. <div class="ui-g-4">Color: </div>
  65. <div class="ui-g-8">{{selectedCar.color}}</div>
  66. </div>
  67. </p-dialog>
  68.  
  1. export class DataViewDemo implements OnInit {
  2. cars: Car[];
  3. selectedCar: Car;
  4. displayDialog: boolean;
  5. sortOptions: SelectItem[];
  6. sortKey: string;
  7. sortField: string;
  8. sortOrder: number;
  9. constructor(private carService: CarService) { }
  10. ngOnInit() {
  11. this.carService.getCarsLarge().then(cars => this.cars = cars);
  12. this.sortOptions = [
  13. {label: 'Newest First', value: '!year'},
  14. {label: 'Oldest First', value: 'year'},
  15. {label: 'Brand', value: 'brand'}
  16. ];
  17. }
  18. selectCar(event: Event, car: Car) {
  19. this.selectedCar = car;
  20. this.displayDialog = true;
  21. event.preventDefault();
  22. }
  23. onSortChange(event) {
  24. let value = event.value;
  25. if (value.indexOf('!') === 0) {
  26. this.sortOrder = -1;
  27. this.sortField = value.substring(1, value.length);
  28. }
  29. else {
  30. this.sortOrder = 1;
  31. this.sortField = value;
  32. }
  33. }
  34. onDialogHide() {
  35. this.selectedCar = null;
  36. }
  37. }
  38.