[CLOSED] SelectBox selectFirst() difference between 1/2.x and 3.x

  1. #1

    [CLOSED] SelectBox selectFirst() difference between 1/2.x and 3.x

    Hi,

    I noticed a subtle change in the selectFirst() method for the SelectBox control in 3.x compared to earlier versions that I was relying on:

    Example:

    <%@ Page Language="C#" %>
    
    <!DOCTYPE html>
    
    <html>
        <head runat="server">
            <title>Select First Programmatically</title>
        </head>
        <body>
            <ext:ResourceManager runat="server" />
            
            <ext:SelectBox runat="server" ID="SelectBox1">
                <Items>
                    <ext:ListItem Text="1" />
                    <ext:ListItem Text="2" />
                    <ext:ListItem Text="3" />
                    <ext:ListItem Text="4" />
                    <ext:ListItem Text="5" />
                </Items>
                <SelectedItem Text="1" />
                <Listeners>
                    <Select Handler="console.log(arguments);" />
                </Listeners>
            </ext:SelectBox>
            
            <ext:Button runat="server" Text="Select First">
                <Listeners>
                    <Click Handler="App.SelectBox1.selectFirst();" />
                </Listeners>
            </ext:Button>
        </body>
    </html>
    A) Run the above code in 2.x in the following way:
    1) Click the button to select the first item programmatically.
    2) Notice that the console.log is reached for the select handler and the arguments are written to the console

    B) Run the same code in 3.x
    Notice that the select event is not fired.

    Looking at the selectFirst implementation I see select calls focusAndSelect():

        focusAndSelect: function (record) {
            var picker = this.getPicker();
    
            record = Ext.isNumber(record) ? this.store.getAt(record) : record;
            this.ignoreSelection++;
            picker.clearHighlight();
            picker.select(record);
            picker.getNavigationModel().setPosition(record);
            this.ignoreSelection--;
    
            if (this.getValue() !== record.data[this.valueField]) {
                this.setValue([record], false);
                this.fireEvent('select', this, [record]);
            }
            
            this.inputEl.focus();
        },
    Compared to earlier versions of Ext.NET it seems now in 3.x the select event is only fired if the record value isn't the same as the current value of the select item.

    In many ways, I think this makes sense.

    I was originally writing this post to say I need to support a scenario where I need this select event to fire but I realized I can remove the selecteditem configuration in my particular scenario.

    However, as it is not listed as a breaking change, I wanted to still post to ensure it is intended different behaviour and also provide a couple of alternatives for anyone who needs it:

    1) Ext.NET team could add an optional boolean parameter to selectFirst(), and to focusAndSelect(). This optional parameter indicates whether to fire the select event, so something like this (not tested)

        selectFirst : function (forceSelectEvent) {
            this.focusAndSelect(this.store.data.first(), forceSelectEvent);
        },
    and

        focusAndSelect: function (record, forceSelectEvent) {
            var picker = this.getPicker();
    
            record = Ext.isNumber(record) ? this.store.getAt(record) : record;
            this.ignoreSelection++;
            picker.clearHighlight();
            picker.select(record);
            picker.getNavigationModel().setPosition(record);
            this.ignoreSelection--;
    
            if (forceSelectEvent === true || this.getValue() !== record.data[this.valueField]) {
                this.setValue([record], false);
                this.fireEvent('select', this, [record]);
            }
            
            this.inputEl.focus();
        },
    It may not be a good idea, in which case there are two alternatives here:

    2) Programmatically fire the select event myself:

            <ext:Button runat="server" Text="Select First">
                <Listeners>
                    <Click Handler="
                        App.SelectBox1.selectFirst();
                        App.SelectBox1.fireEvent('select', App.SelectBox1, [App.SelectBox1.getSelectedRecord()]);
                        " />
                </Listeners>
            </ext:Button>
    3) Question whether the select event is really needed, if the SelectBox already starts off configured to select the first event? In the contrived example I could remove the SelectedItem Text="1" configuration and things will work.

    I actually went for option 3) in my real production code while migrating from 1.x to 3.x while typing up this thread question, realising that in my particular case I do not need both SelectedItem from the start and the select event to fire for selectFirst.

    So, for the Ext.NET team, do you think suggestion 1) is worth doing? I am not convinced myself (as in some ways what I was trying seems illogical - to pre-select that item and still to have the select event fired), so I leave it to you to decide. For everyone else, hope option 2) or 3) is useful if option 1) is decided as not needed.

    Thanks!
    Last edited by Daniil; Jan 14, 2015 at 4:33 AM. Reason: [CLOSED]
  2. #2
    Hi Anup,

    An article as usual:)

    First of all, apologize for your waste of time on that issue because of the change in behavior.

    If an item is already selected before a .selectFirst() call, then we can say that there is no actual selection happening and, therefore, I would expect the Select event doesn't fire. I would rather treat it as a defect in Ext.NET v1 and v2 (which, though, we are probably not going to fix).

    As for the "forceSelectEvent" option for the selectFirst method, I am quite reluctant to add it. If it selects something indeed, the event fires, if it doesn't select anything, the event doesn't fire.

    Are you OK with my answer?:)
  3. #3
    That's prefectly fine, matching my option 1!

    Option 2 and 3 are there for anyone who comes across this.

    You can close this.

    P.s. no need to apologise. Good way for me to learn more :)

Similar Threads

  1. Replies: 1
    Last Post: Sep 09, 2012, 5:12 PM
  2. What is the difference between Sencha's Ext Designer and Ext.NET?
    By mgbloomfield in forum Open Discussions
    Replies: 1
    Last Post: Apr 16, 2012, 10:02 AM
  3. Replies: 4
    Last Post: Oct 24, 2011, 3:34 AM
  4. Alert CSS difference
    By gfcmedia in forum 1.x Help
    Replies: 4
    Last Post: Aug 17, 2010, 9:32 PM
  5. [CLOSED] What is the difference?
    By state in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Apr 09, 2010, 2:57 AM

Posting Permissions