MarkFirstMatch on any part of text field?

8 posts, 1 answers
  1. Alan T
    Alan T avatar
    125 posts
    Member since:
    Apr 2010

    Posted 05 Aug 2011 Link to this post

    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

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 05 Aug 2011 Link to this post

    Hello Alan,

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

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Alan T
    Alan T avatar
    125 posts
    Member since:
    Apr 2010

    Posted 05 Aug 2011 Link to this post

    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
  5. Alan T
    Alan T avatar
    125 posts
    Member since:
    Apr 2010

    Posted 05 Aug 2011 Link to this post

    Ok i can accomplish the first by simply setting allowcustomtext to false. 
  6. Thad
    Thad avatar
    106 posts
    Member since:
    Sep 2010

    Posted 05 Aug 2011 Link to this post

    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
  7. Alan T
    Alan T avatar
    125 posts
    Member since:
    Apr 2010

    Posted 05 Aug 2011 Link to this post

    Howdy Thad, certainly helps, thanks a lot. It seems to clear all the items from the combobox if none are found though. 
  8. Answer
    Thad
    Thad avatar
    106 posts
    Member since:
    Sep 2010

    Posted 05 Aug 2011 Link to this post

    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
  9. Alan T
    Alan T avatar
    125 posts
    Member since:
    Apr 2010

    Posted 06 Aug 2011 Link to this post

    I think thats the one Thad, many thanks.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017