import {DataListModule} from 'primeng/datalist';
DataList requires a collection of items as its value and a ng-template content to display where each item can be accessed using the implicit variable.
Throughout the samples, a car interface having vin, brand, year and color properties are used to define an object to be displayed by the datalist. Cars are loaded by a CarService that connects to a server to fetch the cars with a Promise.
export interface Car {
vin;
year;
brand;
color;
}
import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Car} from '../domain/car';
@Injectable()
export class CarService {
constructor(private http: Http) {}
getCarsLarge() {
return this.http.get('/showcase/resources/data/cars-large.json')
.toPromise()
.then(res => <Car[]> res.json().data)
.then(data => { return data; });
}
}
Here is a sample DataList that displays a list of cars.
export class DataListDemo implements OnInit {
cars: Car[];
constructor(private carService: CarService) { }
ngOnInit() {
this.carService.getCarsLarge().then(cars => this.cars = cars);
}
}
<p-dataList [value]="cars">
<ng-template let-car pTemplate="item">
Car content
</ng-template>
</p-dataList>
Index of the row is available at the ng-template.
<p-dataList [value]="cars">
<ng-template let-car let-i="index" pTemplate="item">
Car content for {{i}}
</ng-template>
</p-dataList>
DataList either uses setter based checking or ngDoCheck to realize if the underlying data has changed to update the UI. This is configured using the immutable property, when enabled (default) setter based detection is utilized so your data changes such as adding or removing a record should always create a new array reference instead of manipulating an existing array as Angular does not trigger setters if the reference does not change. For example, use slice instead of splice when removing an item or use spread operator instead of push method when adding an item. On the other hand, setting immutable property to false removes this restriction by using ngDoCheck with IterableDiffers to listen changes without the need to create a new reference of data. Setter based method is faster however both methods can be used depending on your preference.
Header and Footer are the two sections aka facets that are capable of displaying custom content.
<p-dataList [value]="cars">
<p-header>List of Cars</p-header>
<p-footer>Choose from the list.</p-footer>
<ng-template let-car pTemplate="item">
Car content
</ng-template>
</p-dataList>
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.
<p-dataList [value]="cars" [paginator]="true" [rows]="10">
<ng-template let-car pTemplate="item">
Car content
</ng-template>
</p-dataList>
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.
<p-dataList [value]="cars" [paginator]="true" [rows]="9" [lazy]="true" (onLazyLoad)="loadData($event)" [totalRecords]="totalRecords">
<ng-template let-car pTemplate="item">
Car content
</ng-template>
</p-dataList>
loadData(event) {
//event.first = First row offset
//event.rows = Number of rows per page
}
| Name | Type | Default | Description |
|---|---|---|---|
| value | array | null | An array of objects to display. |
| rows | number | null | Number of rows to display per page. |
| paginator | boolean | false | When specified as true, enables the pagination. |
| totalRecords | number | null | Number of total records, defaults to length of value when not defined. |
| pageLinks | number | 5 | Number of page links to display in paginator. |
| rowsPerPageOptions | array | null | Array of integer values to display inside rows per page dropdown of paginator |
| alwaysShowPaginator | boolean | true | Whether to show it even there is only one page. |
| lazy | boolean | false | Defines if data is loaded and interacted with in lazy manner. |
| style | string | null | Inline style of the component. |
| styleClass | string | null | Style class of the component. |
| paginatorPosition | string | bottom | Position of the paginator, options are "top","bottom" or "both". |
| emptyMessage | string | No records found. | Text to display when there is no data. |
| trackBy | Function | null | Function to optimize the dom operations by delegating to ngForTrackBy, default algoritm checks for object identity. |
| immutable | boolean | true | Defines how the data should be manipulated. |
| scrollable | string | null | Whether the content section is scrollable based on scrollHeight. |
| scrollHeight | string | null | Maximum height of the viewport in scrollable list. |
| paginatorDropdownAppendTo | any | null | Target element to attach the paginator dropdown overlay, valid values are "body" or a local ng-template variable of another element. |
| Name | Parameters | Description |
|---|---|---|
| onLazyLoad | event.first = First row offset event.rows = Number of rows per page |
Callback to invoke when paging, sorting or filtering happens in lazy mode. |
| onPage | event.first: Index of first record in page event.rows: Number of rows on the page |
Callback to invoke when pagination occurs. |
Following is the list of structural style classes, for theming classes visit theming page.
| Name | Element |
|---|---|
| ui-datalist | Container element. |
| ui-datalist-header | Header section. |
| ui-datalist-footer | Footer section. |
| ui-datalist-content | Wrapper of item container. |
| ui-datalist-data | Item container element. |
| ui-datalist-emptymessage | Element containing the empty message. |
None.
<p-dataList [value]="cars" [paginator]="true" [rows]="5">
<p-header>
List of Cars
</p-header>
<ng-template let-car pTemplate="item">
<div class="ui-g ui-fluid car-item">
<div class="ui-g-12 ui-md-3" style="text-align:center">
<img src="assets/showcase/images/demo/car/{{car.brand}}.png">
</div>
<div class="ui-g-12 ui-md-9 car-details">
<div class="ui-g">
<div class="ui-g-2 ui-sm-6">Vin: </div>
<div class="ui-g-10 ui-sm-6">{{car.vin}}</div>
<div class="ui-g-2 ui-sm-6">Year: </div>
<div class="ui-g-10 ui-sm-6">{{car.year}}</div>
<div class="ui-g-2 ui-sm-6">Brand: </div>
<div class="ui-g-10 ui-sm-6">{{car.brand}}</div>
<div class="ui-g-2 ui-sm-6">Color: </div>
<div class="ui-g-10 ui-sm-6">{{car.color}}</div>
</div>
</div>
</div>
</ng-template>
</p-dataList>
export class DataListDemo implements OnInit {
cars: Car[];
constructor(private carService: CarService) { }
ngOnInit() {
this.carService.getCarsLarge().then(cars => this.cars = cars);
}
}