1

I currently have a wrapper list containing custom sObjects and checkboxes. I also have a inputText field on a Visualforce page and would like to filter the wrapper list by License_Voucher__c which is a field in my custom SObject contained within my wrapper.

I would like for the search to contain all variances of the string such as if the user typed in 'john' , the search would return 'john', 'johnson', 'johnny'. Basically john* or a sql equivalent of 'LIKE'.

To make life simpler I added a string voucherNumber to my wrapper and tried to iterate through the list with the .contains() to see if each element met the inputText requirements.

Unfortunately whether I try searching with the entire correct string or part of the correct string I seem to get no results. Would there be a better approach to this? thanks!

Wrapper Class

public class wrapper_OlympusLR{ 
public License_Request__c lrObj {get;set;}
public String macAddress {get;set;}
public String serverName {get;set;}
public String serialNumber {get;set;}
public String serverModel {get;set;}
public Boolean selected {get;set;}
public String voucherNumber {get;set;}

public wrapper_OlympusLR(License_Request__c lrq)
{              
        this.lrObj      = lrq;
        this.selected   = false;               
        this.macAddress = null;
        this.serverName = null;
        this.serialNumber   = null;
        this.serverModel    = null;
        this.voucherNumber = lrq.License_Voucher__c;

}

Controller Search Method

    public PageReference runSearch()
{
    String voucherNumber = inputVoucherNumber+'.*';
    if(voucherNumber.equals(''))
    {
        //DO NOTHING OR USE ORIGINAL LIST 
        wrapDynLRList = wrapLRListOriginal;

    }
    else
    {
        wrapDynLRList.clear();
        for(wrapper_OlympusLR lrList : wrapLRListOriginal)
        {
            if(lrList.voucherNumber.contains(voucherNumber))
            {
                wrapDynLRList.add(lrList);
            }
        }

    }
    return null;
} 
  • The code you posted appears to have 3 different lists: wrapDynLRList, wrapLRListOriginal and wrapLRList. It would make more sense if wrapLRListOriginal was used in place of wrapLRList. – Keith C Aug 11 '14 at 20:11
  • small typo mistake on my part. There's only supposed to be two lists and not three. Fixed it for a better understanding. Thanks for spotting that out! – enph Aug 11 '14 at 21:44
2

You do not need to append the '.*' on the voucher number

Using the .contains(voucherNumber) will return any result that has the voucherNumber in the field the contains is applied on.

  • bah, guess I overlooked how the .contains() works. Removing the .* fixed it, thanks for the insight. – enph Aug 11 '14 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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