[CLOSED] Select value in custom search

  1. #1

    [CLOSED] Select value in custom search

    Hi,

    I need to see an example of how to set the selected value and display value of a combo box, like in your example:

    https://examples2.ext.net/#/Form/Com...Custom_Search/

    How could I add a default selected value and have a button, which when clicked set the selected value and display text of the combobox?

    Hope you can help,

    Paul.
    Last edited by Daniil; Jan 21, 2014 at 4:42 AM. Reason: [CLOSED]
  2. #2
    Hi Paul,

    To set initial selection I would use a ComboBox's SelectedItems. To set an item on the fly, I would use the SetValue method.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Store store = this.ComboBox1.GetStore();
                store.DataSource = new object[] 
                { 
                    new object[] { "1", "Item 1" },
                    new object[] { "2", "Item 2" },
                    new object[] { "3", "Item 3" }
                };
            }
        }
    
        protected void Set(object sender, DirectEventArgs e)
        {
            this.ComboBox1.SetValue("2");
        }
    </script>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            <ext:ComboBox 
                ID="ComboBox1" 
                runat="server" 
                DisplayField="text" 
                ValueField="value">
                <Store>
                    <ext:Store runat="server">
                        <Model>
                            <ext:Model runat="server">
                                <Fields>
                                    <ext:ModelField Name="value" />
                                    <ext:ModelField Name="text" />
                                </Fields>
                            </ext:Model>
                        </Model>
                        <Reader>
                            <ext:ArrayReader />
                        </Reader>
                    </ext:Store>
                </Store>
                <SelectedItems>
                    <ext:ListItem Value="1" />
                </SelectedItems>
            </ext:ComboBox>
    
            <ext:Button runat="server" Text="Set" OnDirectClick="Set" />
        </form>
    </body>
    </html>
    Though, both the actions require an item to be loaded in the ComboBox's Store. It won't preload it automatically.

    You might be interested to read this thread:
    http://forums.ext.net/showthread.php?22653
  3. #3
    Hi Daniil,

    Your example clearly doesn't answer the question - A bit of a cop out really!! Clearly I need to be able to set a default value using remote store, but the thread you directed me to doesn't answer the question. The OP says he created a custom control but there is no hint to me how to make it work.

    Can you just give a little example code of how to set the combobox value when a remote stored is used please?


    Quote Originally Posted by Daniil View Post
    Hi Paul,

    To set initial selection I would use a ComboBox's SelectedItems. To set an item on the fly, I would use the SetValue method.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Store store = this.ComboBox1.GetStore();
                store.DataSource = new object[] 
                { 
                    new object[] { "1", "Item 1" },
                    new object[] { "2", "Item 2" },
                    new object[] { "3", "Item 3" }
                };
            }
        }
    
        protected void Set(object sender, DirectEventArgs e)
        {
            this.ComboBox1.SetValue("2");
        }
    </script>
    
    <!DOCTYPE html>
    
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            <ext:ComboBox 
                ID="ComboBox1" 
                runat="server" 
                DisplayField="text" 
                ValueField="value">
                <Store>
                    <ext:Store runat="server">
                        <Model>
                            <ext:Model runat="server">
                                <Fields>
                                    <ext:ModelField Name="value" />
                                    <ext:ModelField Name="text" />
                                </Fields>
                            </ext:Model>
                        </Model>
                        <Reader>
                            <ext:ArrayReader />
                        </Reader>
                    </ext:Store>
                </Store>
                <SelectedItems>
                    <ext:ListItem Value="1" />
                </SelectedItems>
            </ext:ComboBox>
    
            <ext:Button runat="server" Text="Set" OnDirectClick="Set" />
        </form>
    </body>
    </html>
    Though, both the actions require an item to be loaded in the ComboBox's Store. It won't preload it automatically.

    You might be interested to read this thread:
    http://forums.ext.net/showthread.php?22653
  4. #4
    Quote Originally Posted by paulc View Post
    Clearly I need to be able to set a default value using remote store, but the thread you directed me to doesn't answer the question.
    There is an example in the post #9:
    http://forums.ext.net/showthread.php...l=1#post105666
  5. #5
    Quote Originally Posted by Daniil View Post
    There is an example in the post #9:
    http://forums.ext.net/showthread.php...l=1#post105666
    Thanks for the link but I am using MVC. How would I data bind the selected value. I've tried RawValue and RawDisplayValue which looked promising but didn't appear to do anything. The other thing is I need to set the selected value/display value using javascript.
  6. #6
    Quote Originally Posted by paulc View Post
    Thanks for the link but I am using MVC. How would I data bind the selected value.
    You can pass the Data via Model or ViewBag and apply it to the Store's Data in a View. And set up a ComboBox's SelectedItems.

    Quote Originally Posted by paulc View Post
    The other thing is I need to set the selected value/display value using javascript.
    First, I would check if that item already exists in the ComboBox or not.
    http://docs.sencha.com/extjs/4.2.1/#...dRecordByValue

    If yes, then you could just use a ComboBox's setValue method.

    If no, there are two ways:

    1. Add an item, see:
    https://examples2.ext.net/#/Form/Com...Items_Actions/
    and select it.
    comboBox.addItem(defaultText, defaultValue);
    comboBox.setValue(defaultValue);
    At some point you might need to remove this item to avoid duplicating, then a ComboBox gets the same item from server side.

    or

    2. Preload an item and select it on a callback:
    store.load({ 
        params: {
            itemToLoad: defaultValue
        },
        callback: function() {
            comboBox.setValue(defaultValue);
        }
    });
    Last edited by Daniil; Jan 14, 2014 at 11:17 AM.

Similar Threads

  1. [CLOSED] [1.0] Custom search help
    By edigital in forum 1.x Legacy Premium Help
    Replies: 11
    Last Post: Apr 10, 2015, 7:44 PM
  2. Custom Search with ComboBox
    By Vinci in forum 2.x Help
    Replies: 1
    Last Post: May 23, 2013, 8:01 AM
  3. Custom Search
    By DanielU in forum 1.x Help
    Replies: 23
    Last Post: Mar 04, 2013, 10:54 AM
  4. [CLOSED] [1.0] custom search
    By vali1993 in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Apr 23, 2010, 3:48 PM
  5. Custom Search
    By sharif in forum 1.x Help
    Replies: 0
    Last Post: Jul 14, 2009, 4:04 PM

Posting Permissions