简体   繁体   中英

Primefaces Multiple Value autocomplete

I'm using primefaces autocomlete force selection to search over accounts by name

JSF Component

<p:autoComplete value="#{journal.journal.debit}"
                completeMethod="#{account.completeAccount}" 
                var="var" itemLabel="#{var.name}" itemValue="#{var}"
                forceSelection="true" immediate="false" required="true">

Bean Method:

public List<Account> completeAccount(String query) {
    List<Account> allAccounts = service.get(Account.class);
    List<Account> filteredAccounts = new ArrayList();

    for (int i = 0; i < allAccounts.size(); i++) {
        Account foundAccount = allAccounts.get(i);
        if (foundAccount.getName().toLowerCase().contains(query.toLowerCase())) {
            filteredAccounts.add(foundAccount);
        }
    }
    return filteredAccounts;
}

this works fine, now if I want to change the search to search also for account number in the query value. I have used the following:

if (foundAccount.getName().toLowerCase().contains(query.toLowerCase()) || foundAccount.getNumber() == Integer.parseInt(query)) {
            filteredAccounts.add(foundAccount);
        }

but then, the filter is only returning searching for the number and ignoring name search. How can I achieve this?

I think that what Deepak was trying to say is that what you're doing is perfectly valid, and not an issue with primefaces rather something wrong with your condition. And indeed, the most obvious thing is that there is no Integer.parseInteger(String s) method, at least no in java.lang.Integer .

If this is a typo of some sort, and if you're working with Integer objects (not the int primitives) , make sure that you're comparing them by using .equals method. Comparing Integers by == will work only in the range from -128 - 127, outside of that range it will compare references.

Hope it helps

try this

if (
     (foundAccount.getName().toLowerCase().contains(query.toLowerCase()))
     ||
     (foundAccount.getNumber() == Integer.parseInteger(query))
   )
{
   filteredAccounts.add(foundAccount);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM