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

RadSearcBox SearchContext remove item is not working

3 Answers 113 Views
SearchBox
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Marcel asked on 28 Dec 2017, 07:08 PM

Hello,

I am using JavaScript to remove some SearchContext items from the dropdown list. After executing the code below, the items remain in the Search Context dropdown list, but their selection no longer works correctly. I have tried both itemsCollection.removeAt(i); and itemsCollection.remove(item); and both behave in the same way.  If this is a bug, is there a way to define these items in server side code (C#)? That way I can decide which items to add. Thinking about it, I guess I could try adding the items on the client side, rather than declaratively.

JS Code to remove the desired items: I have stepped through vis the debugger to ensure the code is executed as expected.

var searchBox = $find("<%=SearchRadSearchBox.ClientID%>");
 
if (searchBox) {
    searchContext = searchBox.get_searchContext();
    if (searchContext) {
        itemsCollection = searchContext.get_items();
 
        for (var i = itemsCollection.get_count() - 1; i >= 0; i--) {
            item = itemsCollection.getItem(i);
            if (item) {
                if (item.get_text() == 'Joint Barcode' || item.get_text() == 'Weld Barcode') {
                    // remove the item
                    itemsCollection.removeAt(i);
                }
            }
        }
    }
}

 

Definition of the SearchBox:

<telerik:RadSearchBox RenderMode="Lightweight" ID="SearchRadSearchBox" runat="server" Width="500" OnSearch="SearchRadSearchBox_Search" EnableAutoComplete="False">
    <Localization DefaultItemText="General" LoadingItemsMessage="Some Loading" />
    <SearchContext>
        <Items>
            <telerik:SearchContextItem Text="Document Type" Key="1" />
            <telerik:SearchContextItem Text="Heat Number" Key="2" />
            <telerik:SearchContextItem Text="Spread" Key="3" />
            <telerik:SearchContextItem Text="Joint Barcode" Key="4" />
            <telerik:SearchContextItem Text="Weld Barcode" Key="5" />
        </Items>
    </SearchContext>
</telerik:RadSearchBox>

 

 

3 Answers, 1 is accepted

Sort by
0
Marcel
Top achievements
Rank 1
answered on 24 Jan 2018, 11:07 PM

Although I can add the last 2 SearchContextItems on the client side using this code:

itemsCollection = searchContext.get_items();
var item = new Telerik.Web.UI.SearchContextItem();
item.set_text('Joint Barcode');
item.set_key('4');
itemsCollection.add(item);

You cannot then manipulate these items on the server side, because the server does not know they exist. e.g. SearchRadSearchBox.SearchContext.SelectedItem is null on the server when you select an item added via the client.

I could use a work around please.

0
Marcel
Top achievements
Rank 1
answered on 24 Jan 2018, 11:27 PM

For now I returned to my original code, adding the following line just before the remove.

If any of these '--' items are selected, the defaultItemText 'General' is shown, which is OK.

item.set_text('--'); // workaround for the bug below
0
Marcel
Top achievements
Rank 1
answered on 24 Jan 2018, 11:30 PM

I returned to my original code, adding the following line of code before the RemoveAt()

item.set_text('--'); // workaround for the bug below
 
// remove the item - there is a bug where the items are not removed completely
itemsCollection.removeAt(i);

If any of the '--' items are selected, the DefaultItemText "General" is shown as the selected item.

Tags
SearchBox
Asked by
Marcel
Top achievements
Rank 1
Answers by
Marcel
Top achievements
Rank 1
Share this question
or