This is a migrated thread and some comments may be shown as answers.

MarkFirstMatch on any part of text field?

7 Answers 197 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Alan T
Top achievements
Rank 1
Alan T asked on 05 Aug 2011, 11:34 AM
Hi there, i'm wondering if this is possible, what i'd like to be able to implement is a radcombobox where it finds the closest match to whatever you type.

For example i've got a radcombobox with markfirstmatch enabled and allowcustomtext to false (because i want to limit them to the items within the combobox). 

Take this for example; a combobox populated with countries. 

United Kingdom 
United Arab Emirates. 

I start typing 'Uni' and it jumps to any items starting with 'Uni' which is great. However, what if i want to type 'Kingdom' ? Or 'Arab' , or any part of the text. 

I'd like it filter the items in the combobox based on this. 

Another example would be me typing in 'land' and having it find 'England', and any other items containing that text.

Many thanks for your support Telerik.

Alan

7 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Aug 2011, 11:46 AM
Hello Alan,

Check the following demo which explains the similar scenario.Hope this helps.
ComboBox / Filtering .

Thanks,
Shinu.
0
Alan T
Top achievements
Rank 1
answered on 05 Aug 2011, 03:17 PM
HI Shinu, thats pretty much it. Is there a way i can enforce some validation that limits them to having to have selected an item from the list rather than typing some rubbish in ? I'd also like it so that if i enter 'Enter' or 'Tab' after typing a few letters, it defaults to the first item matched in the list. 

Are these two possible ?

Many thanks
0
Alan T
Top achievements
Rank 1
answered on 05 Aug 2011, 03:21 PM
Ok i can accomplish the first by simply setting allowcustomtext to false. 
0
Thad
Top achievements
Rank 2
answered on 05 Aug 2011, 06:26 PM
Hi Alan T,

This is what we do to accomplish your goals.

function singleSelect_RadComboBox_OnClientBlur(sender, eventArgs) {
    markFirstMatch(sender, function (item) {
        if (item) {
            // don't re-select the item or it fires a postback when tabbing through a RadComboBox without changing it.
            if (item != sender.get_selectedItem()) {
                item.select();
            }
        }
        else { //No match found
            if (sender.get_emptyMessage() != 'Select...') {
                sender.set_emptyMessage('Select...');
            }
            sender.clearSelection();
        }
    });
}
 
function markFirstMatch(comboBox, action) {
    action(findFirstMatch(comboBox));
}
 
function findFirstMatch(comboBox) {
    var text = comboBox.get_text();
    if (!text) return null;
    text = text.toLowerCase();
 
    var items = comboBox.get_items();
 
    // search for an exact match first  (stops Arkansas from being selected when you type Kansas)
    for (var i = 0, length = items.get_count(); i < length; i++) {
        if (items.getItem(i).get_text().toLowerCase() == text) {
            return items.getItem(i);
        }
    }
 
    // search for a partial match if an exact match wasn't found
    for (var i = 0, length = items.get_count(); i < length; i++) {
        if (items.getItem(i).get_text().toLowerCase().indexOf(text) >= 0) {
            return items.getItem(i);
        }
    }
}

Hopefully that helps you!
Thad
0
Alan T
Top achievements
Rank 1
answered on 05 Aug 2011, 07:26 PM
Howdy Thad, certainly helps, thanks a lot. It seems to clear all the items from the combobox if none are found though. 
0
Accepted
Thad
Top achievements
Rank 2
answered on 06 Aug 2011, 01:49 AM
Hey Alan,

Try attaching this to the Focus event of your RadComboBox.  I missed that in my first response.

function singleSelect_RadComboBox_Focus(sender, event) {
    /// <summary>Tries to focus on all text in textbox on focus</summary>
    /// <param name="sender" type="object">A MSAjax reference to the RadComboBox</param>
    /// <param name="event" type="object">A custom Telerik event object</param>
 
    //Check to see if no item has been chosen.  If so, set
    //the text to blank and reload the on-demand list.  Failure
    //to do this will cause the RCB to not load the items the
    //second time focus is set to it!
    if (sender.get_text() == "") {
        sender.requestItems("", false);
    }
}

Let me know if you have further issues,
Thad
0
Alan T
Top achievements
Rank 1
answered on 06 Aug 2011, 01:11 PM
I think thats the one Thad, many thanks.
Tags
ComboBox
Asked by
Alan T
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Alan T
Top achievements
Rank 1
Thad
Top achievements
Rank 2
Share this question
or