Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PickList | Keep selection after moving an item #11286

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 91 additions & 17 deletions src/app/components/picklist/picklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {ObjectUtils, UniqueComponentId} from 'primeng/utils';
template: `
<div [class]="styleClass" [ngStyle]="style" [ngClass]="{'p-picklist p-component': true, 'p-picklist-striped': stripedRows}" cdkDropListGroup>
<div class="p-picklist-buttons p-picklist-source-controls" *ngIf="showSourceControls">
<button type="button" [attr.aria-label]="upButtonAriaLabel" pButton pRipple icon="pi pi-angle-up" [disabled]="disabled" (click)="moveUp(sourcelist,source,selectedItemsSource,onSourceReorder,SOURCE_LIST)"></button>
<button type="button" [attr.aria-label]="topButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-up" [disabled]="disabled" (click)="moveTop(sourcelist,source,selectedItemsSource,onSourceReorder,SOURCE_LIST)"></button>
<button type="button" [attr.aria-label]="downButtonAriaLabel" pButton pRipple icon="pi pi-angle-down" [disabled]="disabled" (click)="moveDown(sourcelist,source,selectedItemsSource,onSourceReorder,SOURCE_LIST)"></button>
<button type="button" [attr.aria-label]="bottomButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-down" [disabled]="disabled" (click)="moveBottom(sourcelist,source,selectedItemsSource,onSourceReorder,SOURCE_LIST)"></button>
<button type="button" [attr.disabled]="disableSourceControls()" [attr.aria-label]="upButtonAriaLabel" pButton pRipple icon="pi pi-angle-up" [disabled]="disabled" (click)="moveUp(sourcelist,source,selectedItemsSource,onSourceReorder,SOURCE_LIST)"></button>
<button type="button" [attr.disabled]="disableSourceControls()" [attr.aria-label]="topButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-up" [disabled]="disabled" (click)="moveTop(sourcelist,source,selectedItemsSource,onSourceReorder,SOURCE_LIST)"></button>
<button type="button" [attr.disabled]="disableSourceControls()" [attr.aria-label]="downButtonAriaLabel" pButton pRipple icon="pi pi-angle-down" [disabled]="disabled" (click)="moveDown(sourcelist,source,selectedItemsSource,onSourceReorder,SOURCE_LIST)"></button>
<button type="button" [attr.disabled]="isDisabled('source-bottom')" [attr.aria-label]="bottomButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-down" [disabled]="disabled" (click)="moveBottom(sourcelist,source,selectedItemsSource,onSourceReorder,SOURCE_LIST)"></button>
</div>
<div class="p-picklist-list-wrapper p-picklist-source-wrapper">
<div class="p-picklist-header" *ngIf="sourceHeader || sourceHeaderTemplate">
Expand Down Expand Up @@ -49,10 +49,10 @@ import {ObjectUtils, UniqueComponentId} from 'primeng/utils';
</ul>
</div>
<div class="p-picklist-buttons p-picklist-transfer-buttons">
<button type="button" [attr.aria-label]="rightButtonAriaLabel" pButton pRipple icon="pi pi-angle-right" [disabled]="disabled" (click)="moveRight()"></button>
<button type="button" [attr.aria-label]="allRightButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-right" [disabled]="disabled" (click)="moveAllRight()"></button>
<button type="button" [attr.aria-label]="leftButtonAriaLabel" pButton pRipple icon="pi pi-angle-left" [disabled]="disabled" (click)="moveLeft()"></button>
<button type="button" [attr.aria-label]="allLeftButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-left" [disabled]="disabled" (click)="moveAllLeft()"></button>
<button type="button" [attr.disabled]="moveRightDisabled" [attr.aria-label]="rightButtonAriaLabel" pButton pRipple icon="pi pi-angle-right" [disabled]="disabled" (click)="moveRight()"></button>
<button type="button" [attr.disabled]="moveAllRightDisabled" [attr.aria-label]="allRightButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-right" [disabled]="disabled" (click)="moveAllRight()"></button>
<button type="button" [attr.disabled]="moveLeftDisabled" [attr.aria-label]="leftButtonAriaLabel" pButton pRipple icon="pi pi-angle-left" [disabled]="disabled" (click)="moveLeft()"></button>
<button type="button" [attr.disabled]="moveAllLeftDisabled" [attr.aria-label]="allLeftButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-left" [disabled]="disabled" (click)="moveAllLeft()"></button>
</div>
<div class="p-picklist-list-wrapper p-picklist-target-wrapper">
<div class="p-picklist-header" *ngIf="targetHeader || targetHeaderTemplate">
Expand Down Expand Up @@ -85,10 +85,10 @@ import {ObjectUtils, UniqueComponentId} from 'primeng/utils';
</ul>
</div>
<div class="p-picklist-buttons p-picklist-target-controls" *ngIf="showTargetControls">
<button type="button" [attr.aria-label]="upButtonAriaLabel" pButton pRipple icon="pi pi-angle-up" [disabled]="disabled" (click)="moveUp(targetlist,target,selectedItemsTarget,onTargetReorder,TARGET_LIST)"></button>
<button type="button" [attr.aria-label]="topButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-up" [disabled]="disabled" (click)="moveTop(targetlist,target,selectedItemsTarget,onTargetReorder,TARGET_LIST)"></button>
<button type="button" [attr.aria-label]="downButtonAriaLabel" pButton pRipple icon="pi pi-angle-down" [disabled]="disabled" (click)="moveDown(targetlist,target,selectedItemsTarget,onTargetReorder,TARGET_LIST)"></button>
<button type="button" [attr.aria-label]="bottomButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-down" [disabled]="disabled" (click)="moveBottom(targetlist,target,selectedItemsTarget,onTargetReorder,TARGET_LIST)"></button>
<button type="button" [attr.disabled]="disableTargetControls()" [attr.aria-label]="upButtonAriaLabel" pButton pRipple icon="pi pi-angle-up" [disabled]="disabled" (click)="moveUp(targetlist,target,selectedItemsTarget,onTargetReorder,TARGET_LIST)"></button>
<button type="button" [attr.disabled]="disableTargetControls()" [attr.aria-label]="topButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-up" [disabled]="disabled" (click)="moveTop(targetlist,target,selectedItemsTarget,onTargetReorder,TARGET_LIST)"></button>
<button type="button" [attr.disabled]="disableTargetControls()" [attr.aria-label]="downButtonAriaLabel" pButton pRipple icon="pi pi-angle-down" [disabled]="disabled" (click)="moveDown(targetlist,target,selectedItemsTarget,onTargetReorder,TARGET_LIST)"></button>
<button type="button" [attr.disabled]="disableTargetControls()" [attr.aria-label]="bottomButtonAriaLabel" pButton pRipple icon="pi pi-angle-double-down" [disabled]="disabled" (click)="moveBottom(targetlist,target,selectedItemsTarget,onTargetReorder,TARGET_LIST)"></button>
</div>
</div>
`,
Expand Down Expand Up @@ -131,6 +131,8 @@ export class PickList implements AfterViewChecked,AfterContentInit {

@Input() filterLocale: string;

@Input() disableButtons: string[];

@Input() trackBy: Function = (index: number, item: any) => item;

@Input() sourceTrackBy: Function;
Expand Down Expand Up @@ -173,6 +175,8 @@ export class PickList implements AfterViewChecked,AfterContentInit {

@Input() stripedRows: boolean;

@Input() keepFocus: boolean;

@Output() onMoveToSource: EventEmitter<any> = new EventEmitter();

@Output() onMoveAllToSource: EventEmitter<any> = new EventEmitter();
Expand Down Expand Up @@ -231,6 +235,14 @@ export class PickList implements AfterViewChecked,AfterContentInit {

fromListType: number;

moveLeftDisabled: boolean;

moveRightDisabled: boolean;

moveAllLeftDisabled: boolean;

moveAllRightDisabled: boolean;

emptyMessageSourceTemplate: TemplateRef<any>;

emptyFilterMessageSourceTemplate: TemplateRef<any>;
Expand Down Expand Up @@ -309,6 +321,8 @@ export class PickList implements AfterViewChecked,AfterContentInit {
this.movedDown = false;
this.reorderedListElement = null;
}

this.initButtonState();
}

onItemClick(event, item: any, selectedItems: any[], callback: EventEmitter<any>) {
Expand Down Expand Up @@ -541,11 +555,19 @@ export class PickList implements AfterViewChecked,AfterContentInit {
this.onMoveToTarget.emit({
items: this.selectedItemsSource
});
this.selectedItemsSource = [];


if (this.filterValueTarget) {
this.filter(this.target, this.TARGET_LIST);

}

this.cd.detectChanges();

if (this.keepFocus) {
this.stayOnFocus(this.TARGET_LIST);
}

this.selectedItemsSource = [];
}
}

Expand Down Expand Up @@ -591,12 +613,36 @@ export class PickList implements AfterViewChecked,AfterContentInit {
items: this.selectedItemsTarget
});

this.selectedItemsTarget = [];

if (this.filterValueSource) {
this.filter(this.source, this.SOURCE_LIST);
}
}

this.cd.detectChanges();

if(this.keepFocus) {
this.stayOnFocus(this.SOURCE_LIST);
}

this.selectedItemsTarget = [];
}

stayOnFocus(source) {
if (source === this.SOURCE_LIST) {
const children = this.listViewSourceChild.nativeElement.children;

children[children.length - 1].focus();

this.onItemClick(event, this.selectedItemsTarget[0], this.selectedItemsSource, this.onSourceSelect)
}

if (source === this.TARGET_LIST) {
const children = this.listViewTargetChild.nativeElement.children;

children[children.length - 1].focus();

this.onItemClick(event, this.selectedItemsSource[0], this.selectedItemsTarget, this.onSourceSelect);
}
}

moveAllLeft() {
Expand Down Expand Up @@ -626,6 +672,34 @@ export class PickList implements AfterViewChecked,AfterContentInit {
}
}

disableSourceControls(){
if(!this.selectedItemsSource.length){
return true;
}
}

disableTargetControls(){
if(!this.selectedItemsTarget.length){
return true;
}
}

initButtonState(){
this.moveRightDisabled = !this.selectedItemsSource.length ? true : null;
this.moveLeftDisabled = !this.selectedItemsTarget.length ? true : null;
this.moveAllRightDisabled = this.source && this.source.length ? null : true;
this.moveAllLeftDisabled = this.target && this.target.length ? null : true;
this.cd.detectChanges();
}

isDisabled(position){
if (this.disableButtons) {
if (this.disableButtons.includes(position)) {
return true;
}
}
}

isSelected(item: any, selectedItems: any[]) {
return this.findIndexInSelection(item, selectedItems) != -1;
}
Expand Down Expand Up @@ -814,7 +888,7 @@ export class PickList implements AfterViewChecked,AfterContentInit {
destroyStyle() {
if (this.styleElement) {
document.head.removeChild(this.styleElement);
this.styleElement = null;``
this.styleElement = null;
}
}

Expand Down
14 changes: 13 additions & 1 deletion src/app/showcase/components/picklist/picklistdemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h1>PickList</h1>

<div class="content-section implementation">
<div class="card">
<p-pickList [source]="sourceProducts" [target]="targetProducts" sourceHeader="Available" targetHeader="Selected" [dragdrop]="true"
<p-pickList [disableButtons]="['source-bottom']" [keepFocus]="true" [source]="sourceProducts" [target]="targetProducts" sourceHeader="Available" targetHeader="Selected" [dragdrop]="true"
[responsive]="true" [sourceStyle]="{'height':'30rem'}" [targetStyle]="{'height':'30rem'}" filterBy="name"
sourceFilterPlaceholder="Search by name" targetFilterPlaceholder="Search by name">
<ng-template let-product pTemplate="item">
Expand Down Expand Up @@ -327,6 +327,18 @@ <h5>Properties</h5>
<td>false</td>
<td>Whether to displays rows with alternating colors.</td>
</tr>
<tr>
<td>disableButtons</td>
<td>array</td>
<td>null</td>
<td>Defines which buttons will be disabled. Valid values are 'source-up', 'source-top', 'source-down', 'source-bottom', 'target-up', 'target-top', 'target-down', 'target-bottom', 'right', 'all-right', 'left', 'all-left'.</td>
</tr>
<tr>
<td>keepFocus</td>
<td>boolean</td>
<td>null</td>
<td>Keeps focus on the last selected item.</td>
</tr>
</tbody>
</table>
</div>
Expand Down