[CLOSED] Problem with order of items in Grid to Tree

  1. #1

    [CLOSED] Problem with order of items in Grid to Tree

    Hi Guys,
    I already asked you about this problem
    http://forums.ext.net/showthread.php...n-Grid-to-Tree

    Our customers again report about the same problem

    Let's see here:
    https://examples1.ext.net/#/DragDrop/Grid/Grid_to_Tree/

    Please select first 4 items... using mouse+shift ;)
    If you will put before "Leaf1" it's put in normal order
    If you will put after "Leaf1" it's put in wrong order (the same selection)

    Please help
    Last edited by Daniil; Jan 31, 2012 at 6:11 PM. Reason: [CLOSED]
  2. #2
    Hi,

    I can suggest the following solution - override the TreeDropZone's completeDrop function sorting the dropped nodes.

    The solution is between the "//start of solution" and "//end of solution" comments.

    As well, default sorting is set up for the Store.
    <SortInfo Field="company" Direction="ASC" />
    Example
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        private object[] TestData
        {
            get
            {
                DateTime now = DateTime.Now;
    
                return new object[]
                {
                    new object[] { "3m Co", 71.72, 0.02, 0.03, now },
                    new object[] { "Alcoa Inc", 29.01, 0.42, 1.47, now },
                    new object[] { "Altria Group Inc", 83.81, 0.28, 0.34, now },
                    new object[] { "American Express Company", 52.55, 0.01, 0.02, now },
                    new object[] { "American International Group, Inc.", 64.13, 0.31, 0.49, now },
                    new object[] { "AT&T Inc.", 31.61, -0.48, -1.54, now },
                    new object[] { "Boeing Co.", 75.43, 0.53, 0.71, now },
                    new object[] { "Caterpillar Inc.", 67.27, 0.92, 1.39, now },
                    new object[] { "Citigroup, Inc.", 49.37, 0.02, 0.04, now },
                    new object[] { "E.I. du Pont de Nemours and Company", 40.48, 0.51, 1.28, now },
                    new object[] { "Exxon Mobil Corp", 68.1, -0.43, -0.64, now },
                    new object[] { "General Electric Company", 34.14, -0.08, -0.23, now },
                    new object[] { "General Motors Corporation", 30.27, 1.09, 3.74, now },
                    new object[] { "Hewlett-Packard Co.", 36.53, -0.03, -0.08, now },
                    new object[] { "Honeywell Intl Inc", 38.77, 0.05, 0.13, now },
                    new object[] { "Intel Corporation", 19.88, 0.31, 1.58, now },
                    new object[] { "International Business Machines", 81.41, 0.44, 0.54, now },
                    new object[] { "Johnson & Johnson", 64.72, 0.06, 0.09, now },
                    new object[] { "JP Morgan & Chase & Co", 45.73, 0.07, 0.15, now },
                    new object[] { "McDonald\"s Corporation", 36.76, 0.86, 2.40, now },
                    new object[] { "Merck & Co., Inc.", 40.96, 0.41, 1.01, now },
                    new object[] { "Microsoft Corporation", 25.84, 0.14, 0.54, now },
                    new object[] { "Pfizer Inc", 27.96, 0.4, 1.45, now },
                    new object[] { "The Coca-Cola Company", 45.07, 0.26, 0.58, now },
                    new object[] { "The Home Depot, Inc.", 34.64, 0.35, 1.02, now },
                    new object[] { "The Procter & Gamble Company", 61.91, 0.01, 0.02, now },
                    new object[] { "United Technologies Corporation", 63.26, 0.55, 0.88, now },
                    new object[] { "Verizon Communications", 35.57, 0.39, 1.11, now },
                    new object[] { "Wal-Mart Stores, Inc.", 45.45, 0.73, 1.63, now }
                };
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                this.Store1.DataSource = this.TestData;
                this.Store1.DataBind(); 
            }
        }
    
        protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            this.Store1.DataSource = this.TestData;
            this.Store1.DataBind(); 
        }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Ext.NET Example</title>
    
        <ext:ResourcePlaceHolder runat="server" Mode="ScriptFiles" />
    
        <script type="text/javascript">
            Ext.tree.TreeDropZone.override({
                completeDrop : function (de) {
                    var ns = de.dropNode, 
                        p = de.point, 
                        t = de.target, 
                        n,
                        i,
                        len;
                    
                    if(!Ext.isArray(ns)){
                        ns = [ns];
                    }
    
                    //start of solution
                    if (p === "above") {
                        ns.sort(sortFn);    
                    } else if (p === "below") {
                        ns.sort(sortFnDesc);
                    }
                    //end of solution
    
                    for (i = 0, len = ns.length; i < len; i++){
                        n = ns[i];
                        if (p === "above") {
                            t.parentNode.insertBefore(n, t);
                        } else if (p === "below") {
                            t.parentNode.insertBefore(n, t.nextSibling);
                        } else {
                            t.appendChild(n);
                        }
                    }
                    n.ui.focus();
                    if (Ext.enableFx && this.tree.hlDrop) {
                        n.ui.highlight();
                    }
                    t.ui.endDrop();
                    this.tree.fireEvent("nodedrop", de);
                }
            });
    
            var sortFn = function (a, b, dir) {
                var res = 0;
                if (a.text < b.text) {
                    res = -1;
                } else if (a.text > b.text) {
                    res = 1;
                }
                return res * (dir || 1);
            };
    
            var sortFnDesc = function (a, b) {
                return sortFn(a, b, -1);
            };
        </script>
        
        <script type="text/javascript">
             var beforenodedrop = function (e) { 
                if (Ext.isArray(e.data.selections)) {
                    var rec,
                        i;
    
                    e.cancel = false;
                    e.dropNode = [];
                    
                    for (i = 0; i < e.data.selections.length; i++) {
                        rec = e.data.selections[i];
                        
                        e.dropNode.push(this.loader.createNode({
                            text   : rec.get("company"),
                            leaf   : true,
                            price  : rec.get("price"),
                            change : rec.get("change"),
                            qtip   : rec.get("lastChange")
                        }));
                    }             
                    
                    return true;
                }
            };
        </script>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            
            <ext:Store ID="Store1" runat="server" OnRefreshData="MyData_Refresh">
                <Reader>
                    <ext:ArrayReader>
                        <Fields>
                            <ext:RecordField Name="company" />
                            <ext:RecordField Name="price" Type="Float" />
                            <ext:RecordField Name="change" Type="Float" />
                            <ext:RecordField Name="pctChange" Type="Float" />
                            <ext:RecordField Name="lastChange" Type="Date" />
                        </Fields>
                    </ext:ArrayReader>
                </Reader>
                <SortInfo Field="company" Direction="ASC" />
            </ext:Store>
            
            <ext:Panel 
                runat="server" 
                Width="700" 
                Height="400" 
                Layout="BorderLayout">
                <Items>
                    <ext:GridPanel 
                        runat="server"
                        Region="Center"
                        MarginsSummary="5 0 5 5"
                        Title="Grid" 
                        StoreID="Store1" 
                        StripeRows="true"
                        AutoExpandColumn="Company"
                        EnableDragDrop="true"
                        DDGroup="grid2tree">
                        <ColumnModel runat="server">
                            <Columns>
                                <ext:Column 
                                    ColumnID="Company" 
                                    Header="Company" 
                                    Width="160" 
                                    Sortable="true" 
                                    DataIndex="company" 
                                    />
                                <ext:Column 
                                    Header="Price" 
                                    Width="75" 
                                    Sortable="true" 
                                    DataIndex="price">
                                    <Renderer Format="UsMoney" />
                                </ext:Column>
                            </Columns>
                        </ColumnModel>
                        <SelectionModel>
                            <ext:RowSelectionModel runat="server" />
                        </SelectionModel>
                        <LoadMask ShowMask="true" />
                        <BottomBar>
                            <ext:PagingToolbar runat="server" PageSize="10" />
                        </BottomBar>
                    </ext:GridPanel>
                        
                    <ext:TreePanel 
                        runat="server"
                        Region="East"
                        Split="true" 
                        MarginsSummary="5 5 5 0" 
                        Width="300"
                        Title="tree"
                        AutoScroll="true"
                        Collapsible="true"
                        EnableDD="true" 
                        DDGroup="grid2tree">
                        <Root>
                            <ext:TreeNode Text="Root" Expanded="true">
                                <Nodes>
                                    <ext:TreeNode 
                                        Text="Folder 1" 
                                        Qtip="Rows dropped here will be appended to the folder">
                                        <Nodes>
                                            <ext:TreeNode 
                                                Text="Subleaf 1" 
                                                Qtip="Subleaf 1 Quick Tip" 
                                                Leaf="true" />
                                        </Nodes>
                                    </ext:TreeNode>
                                            
                                    <ext:TreeNode 
                                        Text="Folder 2" 
                                        Qtip="Rows dropped here will be appended to the folder">
                                        <Nodes>
                                            <ext:TreeNode 
                                                Text="Subleaf 2" 
                                                Qtip="Subleaf 2 Quick Tip" 
                                                Leaf="true" />
                                        </Nodes>
                                    </ext:TreeNode>
                                            
                                    <ext:TreeNode 
                                        Text="Leaf 1" 
                                        Qtip="Leaf 1 Quick Tip" 
                                        Leaf="true" />
                                </Nodes>
                            </ext:TreeNode>
                        </Root>
                        <Listeners>
                            <BeforeNodeDrop Fn="beforenodedrop" />
                        </Listeners>
                    </ext:TreePanel>
                </Items>
            </ext:Panel> 
        </form>
    </body>
    </html>
  3. #3
    I just make sorting ant it's doesn't work
    Click image for larger version. 

Name:	GridToTree.PNG 
Views:	230 
Size:	83.1 KB 
ID:	3767
  4. #4
    Please clarify did you try my example? Can you confirm it works well?
  5. #5
    Well, I see your rows are sorted in descending order. You should sort nodes accordingly.
  6. #6
    Quote Originally Posted by Daniil View Post
    Well, I see your rows are sorted in descending order. You should sort nodes accordingly.
    I tried your example... it's works before sorting.
    But if you sorted by name for example or different column.
    It's putting nodes in wrong order.

    ps: I took screenshot from your example.
  7. #7
    Well, my example was just an example how to start implementing the requirement.

    You should apply the respective changes here:
    //start of solution
    if (p === "above") {
        ns.sort(sortFn);   
    } else if (p === "below") {
        ns.sort(sortFnDesc);
    }
    //end of solution
    sorting according to the Store's sort state.

    To get the Store's sort state, please use its getSortState method.
    http://docs.sencha.com/ext-js/3-4/#!...d-getSortState
  8. #8
    Hi Danill,
    One more question about your solution

    Quote Originally Posted by Daniil View Post
                  e.dropNode.push(this.loader.createNode({
                            text   : rec.get("company"),
                            leaf   : true,
                            price  : rec.get("price"),
                            change : rec.get("change"),
                            qtip   : rec.get("lastChange")
                        }));
    But Node doesn't have those properties (price,change,qtip)

    How are you going make sorting by price for example in this code:

    Quote Originally Posted by Daniil View Post
            var sortFn = function (a, b, dir) {
                var res = 0;
                if (a.text < b.text) {
                    res = -1;
                } else if (a.text > b.text) {
                    res = 1;
                }
                return res * (dir || 1);
            };
    a.price will be undefined.
  9. #9
    It becomes node's attributes.

    Example
    node.attributes.price
    The "text" attribute is just shared to a node as it's a required attribute and very frequently accessed.

    See also
    http://docs.sencha.com/ext-js/3-4/#!...rty-attributes
    http://docs.sencha.com/ext-js/3-4/#!...-property-text

Similar Threads

  1. [CLOSED] Order GridPanel Groups in an arbitrary order?
    By dmoore in forum 1.x Legacy Premium Help
    Replies: 4
    Last Post: Dec 27, 2013, 4:35 AM
  2. [CLOSED] Problem when inserting items in grouped grid
    By Pablo_Azevedo in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Jul 24, 2012, 3:46 PM
  3. [CLOSED] Question about order of <content> and <items>
    By rosua in forum 1.x Legacy Premium Help
    Replies: 2
    Last Post: Apr 12, 2012, 4:56 AM
  4. [CLOSED] Problem with order of items in Grid to Tree
    By skisly in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Jan 25, 2012, 9:55 PM
  5. [CLOSED] Problem with events order
    By jeybonnet in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Nov 29, 2010, 2:05 PM

Posting Permissions