0

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?

2 Answers 2

1

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

1
  • It was a typo and I have edited my code .getNumber() will return int and not Integer therefore I used == instead of .equals. after implementing the second condition the second condition is performed and the first condition is ignored.
    – fareed
    Nov 21, 2014 at 1:45
-1

try this

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.