primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Multiple field for AutoComplete

Open sourdoth opened this issue 9 years ago • 13 comments

[ ] bug report => Search github for a similar issue or PR before submitting [x] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap [ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Multiple field for "autoComplete" for example:

<p-autoComplete [(ngModel)]="val" [suggestions]="results" (completeMethod)="search($event)" field="name,lastname"></p-autoComplete>

or 
<p-autoComplete [(ngModel)]="val" [suggestions]="results" (completeMethod)="search($event)" field="name+' '+lastname"></p-autoComplete>

in two case show: name(space)lastname => Carlos Rojas (example)

*this is a suggestion, if exist other way (or better) to do this, its fine :D

thanks.

* **Browser:** [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]
<!-- All browsers where this could be reproduced -->
 
* **Language:** [all | TypeScript X.X | ES6/7 | ES5]

* **Node (for AoT issues):** `node --version` =   

sourdoth avatar Oct 25 '17 14:10 sourdoth

Any info regarding this item? It would be a very good addition!

winarg avatar Jan 29 '18 10:01 winarg

Very need this feature, or custom template for single autocomplete

sorcerb avatar Nov 22 '18 11:11 sorcerb

Any updates?

shashidhar-gm avatar Dec 05 '18 11:12 shashidhar-gm

Up. We need this feature!!!

minhdtb avatar Dec 02 '19 08:12 minhdtb

Any solutions or any updates?

BharatGK avatar Feb 19 '20 10:02 BharatGK

Any udpates? We need this

svjr avatar Apr 04 '20 21:04 svjr

any update?

sourdoth avatar Jan 05 '21 21:01 sourdoth

We need it too.

blosser avatar Jun 18 '21 14:06 blosser

I "solved" the problem in the following way, I hope it will help you until they implement the definitive option

HTML <!-- **NOT set/put "field" attribute** --> <p-autoComplete #customId [(ngModel)]="value" (onSelect)="select($event)" [suggestions]="suggestions" (completeMethod)="filter($event)"> <ng-template let-v pTemplate="item"> {{v.name}} {{v.lastname}} {{v.etc}} </ng-template> </p-autoComplete>

TS @ViewChild('customId', {static: false}) auto:any;

select(v : any) { let allFilds:string = v.name+' '+v.lastname..... this.auto.inputFieldValue = allFilds.trim(); }

sourdoth avatar Jun 23 '21 19:06 sourdoth

I "solved" the problem in the following way, I hope it will help you until they implement the definitive option

HTML <!-- **NOT set/put "field" attribute** --> <p-autoComplete #customId [(ngModel)]="value" (onSelect)="select($event)" [suggestions]="suggestions" (completeMethod)="filter($event)"> <ng-template let-v pTemplate="item"> {{v.name}} {{v.lastname}} {{v.etc}} </ng-template> </p-autoComplete>

TS @ViewChild('customId', {static: false}) auto:any;

select(v : any) { let allFilds:string = v.name+' '+v.lastname..... this.auto.inputFieldValue = allFilds.trim(); }

thanks man . Nice and easy alternative.

HrushikeshAITDIAN avatar Aug 26 '21 13:08 HrushikeshAITDIAN

You can already put any string you want for the selected item of a single autocomplete using a pipe-like function as an input for [field]

Exemple :

<p-autocomplete [field]="myCustomDisplay"/>
function myCustomDisplay(item : ItemType): string {
    return `${item.firstAttribute} - ${item.secondAttribute}`;
}

Sources : https://github.com/primefaces/primeng/blob/master/src/app/components/autocomplete/autocomplete.ts#L591

this.field ? ObjectUtils.resolveFieldData(value, this.field) : value;

https://github.com/primefaces/primeng/blob/master/src/app/components/utils/objectutils.ts#L60

 public static resolveFieldData(data: any, field: any): any {
        if (data && field) {
            if (this.isFunction(field)) {
                return field(data);
            }
            else if (field.indexOf('.') == -1) {
                return data[field];
            }
            else {
                let fields: string[] = field.split('.');
                let value = data;
                for(let i = 0, len = fields.length; i < len; ++i) {
                    if (value == null) {
                        return null;
                    }
                    value = value[fields[i]];
                }
                return value;
            }
        }
        else {
            return null;
        }
    }

N.B

  • field type is string and should be any (or the type of field param for resolveFieldData if it becomes more precise one day)
  • documentation should be updated

Tyneor avatar Sep 13 '21 15:09 Tyneor

Hi,

So sorry for the delayed response! Improvements have been made to many components recently, both in terms of performance and enhancement. Therefore, this improvement may have been developed in another issue ticket without realizing it. You can check this in the documentation. If there is no improvement on this, can you reopen the issue so we can include it in our roadmap? Please don't forget to add your feedback as a comment after reopening the issue. These will be taken into account by us and will contribute to the development of this feature. Thanks a lot for your understanding!

Best Regards,

mertsincan avatar Nov 09 '22 14:11 mertsincan

I've opened Add multi-column feature to AutoComplete #13893

mendozagit avatar Oct 16 '23 21:10 mendozagit