PickListPickList is used to reorder items between differents lists.

PickList - 图1

Documentation

Import

  1. import {PickListModule} from 'primeng/picklist';
  2.  

Getting Started

PickList requires two arrays as its lists and a ng-template for the item content where each item in the array can be accessed inside the ng-template using a local ng-template variable.

  1. <p-pickList [source]="list1" [target]="list2">
  2. <ng-template let-car pTemplate="item">
  3. <div class="ui-helper-clearfix">
  4. <img src="assets/showcase/images/demo/car/{{car.brand}}.png" style="display:inline-block;margin:2px 0 2px 2px" width="48">
  5. <div style="font-size:14px;float:right;margin:15px 5px 0 0">{{car.brand}} - {{car.year}} - {{car.color}}</div>
  6. </div>
  7. </ng-template>
  8. </p-pickList>
  9.  
  1. export class MyComponent {
  2. list1: any[];
  3. list2: any[];
  4. ngOnInit() {
  5. this.list1 = //initialize list 1
  6. this.list2 = //initialize list 2
  7. }
  8. }
  9.  

Responsive

In responsive mode, picklist adjusts its controls based on screen size. To activate this mode, set responsive as true.

  1. <p-pickList [responsive]="true">
  2.  

Headers

sourceHeader and targetHeader attributes are used to define captions for the lists.

  1. <p-pickList sourceHeader="Source List" targetHeader="Target List">
  2.  

Multiple Selection

Multiple items can either be selected using metaKey or toggled individually depending on the value of metaKeySelection property value which is true by default. On touch enabled devices metaKeySelection is turned off automatically.

Filtering

Options can be filtered using an input field in the overlay by enabling the filterBy property. This filterBy property decides which field to search(Accepts multiple fields with a comma).

  1. <p-pickList [source]="sourceCars" [target]="targetCars" filterBy="brand"></p-pickList>
  2.  

DragDrop

Items can be reordered using drag and drop by enabling dragdrop property along with dragdropScope to avoid conflict with other drag drop events on view.This dragdrop property also supports cross list actions.

  1. <p-pickList [source]="sourceCars" [target]="targetCars" dragdrop="true"></p-pickList>
  2.  

Templates

NameParametersDescription
itemitem: List item index: Index of the itemContent of an item in the list.
emptymessagesource-Content to display when there is no item available for selection.
emptymessagetarget-Content to display when there is no element selected.

Properties

NameTypeDefaultDescription
sourcearraynullAn array of objects for the source list.
targetarraynullAn array of objects for the target list.
sourceHeaderstringnullText for the source list caption
targetHeaderstringnullText for the target list caption
filterBystringnullWhen specified displays an input field to filter the items on keyup and decides which field to search (Accepts multiple fields with a comma).
trackByFunctionnullFunction to optimize the dom operations by delegating to ngForTrackBy, default algoritm checks for object identity. Use sourceTrackBy or targetTrackBy in case different algorithms are needed per list.
sourceTrackByFunctionnullFunction to optimize the dom operations by delegating to ngForTrackBy in source list, default algoritm checks for object identity.
targetTrackByFunctionnullFunction to optimize the dom operations by delegating to ngForTrackBy in target list, default algoritm checks for object identity.
showSourceFilterbooleantrueWhether to show filter input for source list when filterBy is enabled.
showTargetFilterbooleantrueWhether to show filter input for target list when filterBy is enabled.
dragdropbooleanfalseWhether to enable dragdrop based reordering.
stylestringnullInline style of the component.
styleClassstringnullStyle class of the component.
sourceStylestringnullInline style of the source list element.
targetStylestringnullInline style of the target list element.
responsivebooleanfalseWhen enabled orderlist adjusts its controls based on screen size.
showSourceControlsbooleantrueWhether to show buttons of source list.
showTargetControlsbooleantrueWhether to show buttons of target list.
metaKeySelectionbooleantrueDefines how multiple items can be selected, when true metaKey needs to be pressed to select or unselect an item and when set to false selection of each item can be toggled individually. On touch enabled devices, metaKeySelection is turned off automatically.
sourceFilterPlaceholderstringnullPlaceholder text on source filter input.
targetFilterPlaceholderstringnullPlaceholder text on target filter input.
disabledbooleanfalseWhen present, it specifies that the component should be disabled.
ariaSourceFilterLabelstringnullDefines a string that labels the filter input of source list.
ariaTargetFilterLabelstringnullDefines a string that labels the filter input of target list.

Events

NameParametersDescription
onMoveToTargetevent.items: Moved items arrayCallback to invoke when items are moved from source to target.
onMoveToSourceevent.items: Moved items arrayCallback to invoke when items are moved from target to source.
onMoveAllToTargetevent.items: Moved items arrayCallback to invoke when all items are moved from source to target.
onMoveAllToSourceevent.items: Moved items arrayCallback to invoke when all items are moved from target to source.
onSourceReorderevent.items: Moved items arrayCallback to invoke when items are reordered within source list.
onTargetReorderevent.items: Moved items arrayCallback to invoke when items are reordered within target list.
onSourceSelectevent.originalEvent: Browser event items: Selected items arrayCallback to invoke when items are selected within source list.
onTargetSelectevent.originalEvent: Browser event items: Selected items arrayCallback to invoke when items are selected within target list.
onSourceFilterevent.query: Filter value items: Filtered itemsCallback to invoke when the source list is filtered
onTargetFilterevent.query: Filter value items: Filtered itemsCallback to invoke when the target list is filtered

Methods

NameParametersDescription
resetFilter-Resets the filters.
  1. <p-pickList #pl [source]="sourceCars" [target]="targetCars" filterBy="brand"></p-pickList>
  2. <button type="button" pButton (click)="pl.resetFilter()" label="Reset"></button>
  3.  

Styling

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

NameElement
ui-picklistContainer element.
ui-picklist-source-controlsContainer of source list buttons.
ui-picklist-target-controlsContainer of target list buttons.
ui-picklist-buttonsContainer of buttons.
ui-picklist-listwrapperParent of a list element.
ui-picklist-listList element.
ui-picklist-itemAn item in the list.

Dependencies

None.

Source

View on GitHub

  1. <p-pickList [source]="sourceCars" [target]="targetCars" sourceHeader="Available" targetHeader="Selected" [responsive]="true" filterBy="brand"
  2. dragdrop="true" sourceFilterPlaceholder="Search by brand" targetFilterPlaceholder="Search by brand" [sourceStyle]="{'height':'300px'}" [targetStyle]="{'height':'300px'}">
  3. <ng-template let-car pTemplate="item">
  4. <div class="ui-helper-clearfix">
  5. <img src="assets/showcase/images/demo/car/{{car.brand}}.png" style="display:inline-block;margin:2px 0 2px 2px" width="48">
  6. <div style="font-size:14px;float:right;margin:15px 5px 0 0">{{car.brand}} - {{car.year}} - {{car.color}}</div>
  7. </div>
  8. </ng-template>
  9. </p-pickList>
  10.  
  1. export class PickListDemo {
  2. sourceCars: Car[];
  3. targetCars: Car[];
  4. constructor(private carService: CarService) { }
  5. ngOnInit() {
  6. this.carService.getCarsSmall().then(cars => this.sourceCars = cars);
  7. this.targetCars = [];
  8. }
  9. }
  10.