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

clearItems() not working

4 Answers 136 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Richard M
Top achievements
Rank 1
Richard M asked on 15 Sep 2010, 04:56 PM
function ResetCombo(combo) {
    if (combo) {
        combo.clearItems();
        combo.set_text("");
    }
}
function LocationSelectedIndexChanged<%= ID %>(sender, eventArgs) {
    var locationCombo = $find("<%= rcbLocation.ClientID %>");
    var addressCombo = $find("<%= rcbAddress.ClientID %>");
    var contactCombo = $find("<%= rcbContact.ClientID %>");
    var contact2Combo = $find("<%= rcbContact2.ClientID %>");
    var value = locationCombo.get_value();
    ResetCombo(addressCombo);  // Items clear on on first index change
    ResetCombo(contactCombo);
    ResetCombo(contact2Combo);
   if (contactCombo) contactCombo.disable(); 
   if (contact2Combo) contact2Combo.disable(); 
    if (value == "")
    {
        if (addressCombo) addressCombo.disable(); 
    }
    else
    {
        if (addressCombo) {
        addressCombo.enable(); 
        addressCombo.requestItems("", false); // Fires only on first time index is changed
        }
    }
}

When this method is called, the text does empty, but the items do no clear.  So when the combox is opened, it will not request it's items from the server, which have changed.  When the drop down location index is changed, the first time it fires all events.  Changing it again calls the methods, but clear items does not work, and request items does not fire.

Note:  requestItems fires with "" because it is a custom implementation using context instead of the text of the combo box

4 Answers, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 15 Sep 2010, 05:09 PM

Hello Richard,

Try wrapping you ResetCombo code inside of a trackChanges()/commitChanges() block. Like so:

function ResetCombo(combo) { 
    if (combo) { 
        combo.trackChanges();
        combo.clearItems(); 
        combo.set_text(""); 
        combo.commitChanges();
    
}

This way the changes are persisted to the ViewState.

I hope that helps.
0
Richard M
Top achievements
Rank 1
answered on 15 Sep 2010, 05:15 PM
That didn't solve the problem.  I was under the impression that trackchanges just made the changes available on the server side.  I only need the change to work client side.
0
Simon
Telerik team
answered on 16 Sep 2010, 10:02 AM
Hello Richard Maxwell,

Indeed, track/commit changes preserve the client-side changes so that they are available on the server.

Still the clearItems method works fine on my side in the following setup:
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" />
    <div>
        <telerik:RadComboBox ID="RadComboBox1" runat="server">
            <Items>
                <telerik:RadComboBoxItem Text="1" />
                <telerik:RadComboBoxItem Text="2" />
                <telerik:RadComboBoxItem Text="3" />
                <telerik:RadComboBoxItem Text="4" />
            </Items>
        </telerik:RadComboBox>
    </div>
    <input type="button" id="clear" value="clear" />
    </form>
 
    <script type="text/javascript">
        (function ($) {
            $("#clear").click(function () {
                var combo = $find("RadComboBox1");
                combo.clearItems();
                combo.set_text("");
            });
        })($telerik.$);
    </script>

So most probably, there is something else going on in your implementation, which causes the issue. Could you please try putting an alert right after the clearItems call to verify the number of Items in the RadComboBox after clearing:
function ResetCombo(combo) {
    if (combo) {
        combo.clearItems();
        alert(combo.get_items().get_count());
        combo.set_text("");
    }
}

Regards,
Simon
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Richard M
Top achievements
Rank 1
answered on 16 Sep 2010, 07:24 PM
var randomnumber = Math.floor(Math.random() * 10001);
addressCombo.requestItems(randomnumber, false);

I was able to fix the problem like this.  Because I use the context to pass in the data I actually use to search, I just always left the text blank.  The problem was requestitems appears to not fire if the text is the same, so to force it to fire when I know the context items have changed, I had to make sure it got something new for text.  So I just gave it a random number, so when I tell it to update, it thinks it has a reason to.
Tags
ComboBox
Asked by
Richard M
Top achievements
Rank 1
Answers by
Cori
Top achievements
Rank 2
Richard M
Top achievements
Rank 1
Simon
Telerik team
Share this question
or