[CLOSED] [1.0] CommandColumn's PrepareToolbar listener does not fire if no no command is specified

  1. #1

    [CLOSED] [1.0] CommandColumn's PrepareToolbar listener does not fire if no no command is specified

    I need to add all Items to a CommandColumn dynamically. So, I am listening to the Column's PrepareToolbar listener, and adding the desired items there.

    As all items are dynamic, the <Commands> collection is initially empty when the grid renders. In this case, the PrepareToolbar event does not fire.

    I need to add atleast one item in <Commands> collection to make the event to fire. My current workaround is to add a <CommandSpacer> with Width="1px" which makes the event to fire. However, it would be useful if the event fires with an empty collection also.
  2. #2
    Hello, r_honey!

    It seems it's not so easy to change the code of CommandColumn class as you require without adding new bugs. In addition it's question we should to do it or not. Maybe adding a new section like CreateToolbar would be a solution...

    So, in your case a possible solution can look something like this - setting Commands in code behind within the OnInit handler of CommandColumn.

    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                Store store = this.GridPanel1.Store.Primary;
                store.DataSource = new object[] { 
                                             new object[] {"3m Co"},
                                             new object[] {"Alcoa Inc"},
                                             new object[] {"Altria Group Inc"}
                                    };
                store.DataBind();
            }
        }
    
        protected void Init_Handler(object sender, EventArgs e)
        {
            ColumnModel cm = sender as ColumnModel;
            CommandColumn cc = cm.Columns[1] as CommandColumn;
            cc.Commands.Add(new CommandSpacer(1));
        }
    </script>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.Net Example</title>
    </head>
    <body>
        <form runat="server">
        <ext:ResourceManager runat="server" />
        <ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
            <Store>
                <ext:Store runat="server">
                    <Reader>
                        <ext:ArrayReader>
                            <Fields>
                                <ext:RecordField Name="company" />
                            </Fields>
                        </ext:ArrayReader>
                    </Reader>
                </ext:Store>
            </Store>
            <ColumnModel ID="ColumnModel1" runat="server" OnInit="Init_Handler">
                <Columns>
                    <ext:Column Header="Company" DataIndex="company" />
                    <ext:CommandColumn>
                        <PrepareToolbar Handler="alert('PrepareToolbar handler');"/>
                    </ext:CommandColumn>
                </Columns>
            </ColumnModel>
        </ext:GridPanel>
        </form>
    </body>
    </html>
    If it's not appropriate please provide us with a scenario. When do you need add items to CommandColumn? Maybe creating a CommandColumn at all would be more rational?
  3. #3
    Hi Daniil, creating Commands in Init handler even is NOT an option. The command actually differ based on the record content. Check this file for an example:
    http://ike.co.in/files/SplitCommand.aspx

    This clearly means that Commands need to be created client-side, because it is not possible to customize them on the server based on the record.
  4. #4
    Hello, r_honey!
    Thank you for the example.

    In your case the CommandColumn can look like this:

    Example
    <ext:CommandColumn>
        <Commands>
            <ext:SplitCommand CommandName="RowCommand" Icon="Add" Text="Menu" />
        </Commands>
        <PrepareToolbar Handler="var split = toolbar.getComponent(0);
                                split.menu = new Ext.menu.Menu({items: []});
                                switch (record.data.position) {
                                    case 'Supervisor':
                                        split.menu.add({ command: 'Super1', text: 'Supervisor Action 1' });
                                        split.menu.add({ command: 'Super2', text: 'Supervisor Action 2' });
                                        break;
                                    case 'Manager':
                                        split.menu.add({ command: 'Manager1', text: 'Manager Action 1' });
                                        split.menu.add({ command: 'Manager2', text: 'Manager Action 2' });
                                        break;
                                }
                                toolbar.addButton(split);
                                toolbar.doLayout();" />
        <Renderer />
    </ext:CommandColumn>
  5. #5
    Hi,

    Fixed in SVN

    P.S. If you use Commands actively then you can be interested by the following property of the CommandMenu
    - Shared

    If you set it to the true then only one instance of the menu will be used for all records. It increases performance because one instance is used instead creating menu for each record. It can be used if you have menu is similar for all records (like Delete, Edit commands and etc). Please note that shared menu must be defined on the server (it cannot be defined via PrepareToolbar)
  6. #6
    Quote Originally Posted by Daniil.Veriga View Post
    <ext:CommandColumn>
        <Commands>
            <ext:SplitCommand CommandName="RowCommand" Icon="Add" Text="Menu" />
        </Commands>
        <PrepareToolbar Handler="var split = toolbar.getComponent(0);
                             ...............
                                toolbar.addButton(split);
                                toolbar.doLayout();" />
        <Renderer />
    </ext:CommandColumn>
    Is it advisable to do that (toolbar.addButton(split);)? Remember, the split button already exists in the toolbar. Isn't calling doLayout() sufficient??
  7. #7
    Quote Originally Posted by vladimir View Post
    P.S. If you use Commands actively then you can be interested by the following property of the CommandMenu
    - Shared

    If you set it to the true then only one instance of the menu will be used for all records. It increases performance because one instance is used instead creating menu for each record. It can be used if you have menu is similar for all records (like Delete, Edit commands and etc). Please note that shared menu must be defined on the server (it cannot be defined via PrepareToolbar)
    I hope "Actively" here does not relate to any particular way Commands are used (but just means to say that if I use commands often).

    If setting to Shared, would the menu show on each row independently, or would it appear dynamically as each row is selected??
  8. #8
    Hi,

    I hope "Actively" here does not relate to any particular way Commands are used (but just means to say that if I use commands often).
    Yes, right. I meant "often"

    If setting to Shared, would the menu show on each row independently, or would it appear dynamically as each row is selected??
    CommandMenu can be used inside another command (which is represented by button). So, when you click on the button then menu is showed. If the menu appears then another menus are hidden at the same time (by default). Therefore we can use one menu for all records (if menu is similar for all records). If menu is marked as shared then only one instance will be used. All commands button will use that single menu instance (by default, if Shared="false", each command button creates own instance of the menu (if menu is defined))
  9. #9
    Thanks for the reply vlad.
  10. #10
    The thread is related to this one:
    http://forums.ext.net/showthread.php?18608

    The problem with Store re-loading and
    Shared="true"
    is discussed in that thread.

Similar Threads

  1. Replies: 8
    Last Post: Aug 16, 2012, 4:44 AM
  2. Replies: 1
    Last Post: Apr 11, 2012, 12:39 PM
  3. [CLOSED] Command Listener not fired on IE9
    By ddslogistics in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Mar 05, 2012, 3:07 PM
  4. Replies: 7
    Last Post: Jun 15, 2011, 7:34 PM
  5. Replies: 5
    Last Post: Nov 26, 2010, 5:39 PM

Posting Permissions