[CLOSED] How to implement GridPanel column resize on double-click?

  1. #1

    [CLOSED] How to implement GridPanel column resize on double-click?

    Hi,

    Is there any way to make a GridPanel column resize to fit when the user double-clicks on the column edges on the client?

    Thanks,

    Vadym
    Last edited by Daniil; Jun 08, 2012 at 2:25 PM. Reason: [CLOSED]
  2. #2
    Hi,

    Unfortunately, there is no such built-in functionality and I can't see a simple way to implement.

    I would implement it something like this.

    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.GridPanel1.GetStore();
                store.DataSource = new object[] 
                { 
                    new object[] { "test1", "test2", "test3" },
                    new object[] { "test4", "test5", "test6" },
                    new object[] { "test7", "test8", "test9" },
                };
                store.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>
    
        <script type="text/javascript">
            var onHeaderClick = function (grid, columnIndex, e) {
                var view = grid.getView(),
                    header = view.findHeaderCell(view.activeHdRef),
                    c = header.style.cursor;
    
                return !(c === "col-resize" || c === "move" || c === "e-resize" || c === "w-resize");
            };
    
            var onDblHeaderClick = function (grid, columnIndex, e) {
                grid.autoExpandColumn = grid.getColumnModel().config[columnIndex].id;
                grid.getView().autoExpand();
            };
        </script>
    </head>
    <body>
        <ext:ResourceManager runat="server" />
        <ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
            <Store>
                <ext:Store runat="server">
                    <Reader>
                        <ext:ArrayReader>
                            <Fields>
                                <ext:RecordField Name="test1" />
                                <ext:RecordField Name="test2" />
                                <ext:RecordField Name="test3" />
                            </Fields>
                        </ext:ArrayReader>
                    </Reader>
                </ext:Store>
            </Store>
            <ColumnModel runat="server">
                <Columns>
                    <ext:Column Header="Test1" DataIndex="test1" />
                    <ext:Column Header="Test2" DataIndex="test2" />
                    <ext:Column Header="Test3" DataIndex="test3" />
                </Columns>
            </ColumnModel>
            <View>
                <ext:GridView runat="server">
                    <Listeners>
                    </Listeners>
                </ext:GridView>
            </View>
            <Listeners>
                <HeaderClick Fn="onHeaderClick" /> <%--To prevent sorting when col-resize cursor disappears--%>
                <HeaderDblClick Fn="onDblHeaderClick" />
            </Listeners>
        </ext:GridPanel>
    </body>
    </html>
    But it requires some tune up. To cause resizing to be available for all columns, you should analyze you are click left or right column resizers.

    Please investigate the GridView handleHdMove function.
    http://docs.sencha.com/ext-js/3-4/so...-grid-GridView

    There are the inLeftResize and inRightResize variable. I think you could get them withing the onHeaderDblClick as well and analyze to determine what column you should expand.
  3. #3
    Thanks Daniil!

    I've tried the sample and it appears that a column auto-expands to some arbitrary width. Is there any way to cause it to expand only to fit the width of the cell text?

    Thanks,

    Vadym
  4. #4
    You should use the setColumnWidth method.
    http://docs.sencha.com/ext-js/3-4/#!...setColumnWidth

    To get header text width I would use TextMetrics.
    http://docs.sencha.com/ext-js/3-4/#!...il.TextMetrics
  5. #5
    This works for me:

    JS
        autoResizeColumn = function (grid, columnIndex, e) {
                    var view = grid.getView(),
                        store = grid.getStore(),
                        colModel = grid.getColumnModel(),
                        cell,
                        width;                
                    if (Math.abs(e.getPageX() - Ext.get(view.getHeaderCell(columnIndex)).getX()) < 20) {
                        columnIndex = columnIndex > 0 ? columnIndex - 1 : 0; // to resize column on left
                    }
                    width = Ext.get(view.getHeaderCell(columnIndex)).child(".x-grid3-hd-inner").getTextWidth();
     
                    if (Ext.isChrome && colModel.config[columnIndex].commands) return; // Chrome has problem with resizing commandcolumn
                    if (store.sortInfo && store.sortInfo.field == colModel.config[columnIndex].dataIndex) {
                        width += 15; // 15px + for sorting arrows
                    }
                    store.each(function (record, rowIdx) {
                        cell = Ext.get(view.getCell(rowIdx, columnIndex));
                        width = Math.max(width, cell.child(".x-grid3-cell-inner").getTextWidth());
                    });
                    colModel.setColumnWidth(columnIndex, width + 10);
                    grid.autoExpandColumn = null;
                };
        ensureAutoResizeDoubleClick = function (grid, columnIndex, e) {
            var view = grid.getView();
            header = view.getHeaderCell(view.activeHdIndex),
    		c = header.style.cursor;
            return !(c === "col-resize" || c === "move" || c === "e-resize" || c === "w-resize");
        };
    Listeners for Ext.NET GridPanel
                    <Listeners>
                        <HeaderClick Fn="ensureAutoResizeDoubleClick" />
                        <HeaderDblClick Fn="autoResizeColumn" />
                    </Listeners>
    Unfortunatelly, this has a small bug - last column cant be automatically resized.
    Last edited by ambruslaco; Feb 14, 2013 at 3:41 PM.
  6. #6
    Hi @ambruslaco,

    Please provide a full example to test with.
  7. #7
    Thanks your request to provide example I fixed another bug:

    Here is an example insipired by Ext.NET Examples (https://examples1.ext.net/#/GridPanel/Layout/FitLayout/)

    <%@ Page Language="C#" %> 
    <%@ Import Namespace="System.Collections.Generic" %>
    <%@ Register assembly="Ext.Net" namespace="Ext.Net" tagprefix="ext" %>
     
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Store1.DataSource = this.Jobs;
            this.Store1.DataBind();
        }
        private List<Job> Jobs
        {
            get
            {
                List<Job> jobs = new List<Job>();
                for (int i = 1; i <= 50; i++)
                {
                    jobs.Add(new Job(
                                i, 
                                "Task" + i.ToString(), 
                                DateTime.Today.AddDays(i), 
                                DateTime.Today.AddDays(i + i), 
                                (i%3 == 0)));
                }
                return jobs;
            }
        }
        public class Job
        {
            public Job(int id, string name, DateTime start, DateTime end, bool completed)
            {
                this.ID = id;
                this.Name = name;
                this.Start = start;
                this.End = end;
                this.Completed = completed;
            }
            public int ID { get; set; }
            public string Name { get; set; }
            public DateTime Start { get; set; }
            public DateTime End { get; set; }
            public bool Completed { get; set; }
        }
    </script>
    
    <!DOCTYPE html>
    <html>
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">   
            <ext:ResourceManager runat="server" />
            <ext:GridPanel
                ID="GridPanel1" 
                runat="server" 
                StripeRows="true"
                Height="400"
                Width="600"
                AutoExpandColumn="Name">
                <Store>
                    <ext:Store ID="Store1" runat="server">
                        <Reader>
                            <ext:JsonReader IDProperty="ID">
                                <Fields>
                                    <ext:RecordField Name="ID" />
                                    <ext:RecordField Name="Name" />
                                    <ext:RecordField Name="Start" Type="Date" />
                                    <ext:RecordField Name="End" Type="Date" />
                                    <ext:RecordField Name="Completed" Type="Boolean" />
                                </Fields>
                            </ext:JsonReader>
                        </Reader>
                    </ext:Store>
                </Store>
                <LoadMask ShowMask="false" />
                <ColumnModel runat="server">
                    <Columns>
                        <ext:Column Header="ID" Width="40" DataIndex="ID"  />
                        <ext:Column ColumnID="Name" Header="Job Name" DataIndex="Name"  />
                        <ext:DateColumn ColumnID="Start" Header="Start" Width="120" DataIndex="Start" Format="yyyy-MM-dd"  />
                        <ext:DateColumn ColumnID="End"  Header="End" Width="120" DataIndex="End" Format="yyyy-MM-dd" />
                        <ext:Column ColumnID="Completed" Header="Completed" Width="80" DataIndex="Completed">
                            <Renderer Handler="return (value) ? 'Yes':'No';" />
                        </ext:Column>
                    </Columns>
                </ColumnModel>
                <Listeners>
                    <HeaderClick Fn="ensureAutoResizeDoubleClick" />
                    <HeaderDblClick Fn="autoResizeColumn" />
                </Listeners>
                <BottomBar>
                    <ext:PagingToolbar 
                        runat="server" 
                        PageSize="10" 
                        DisplayInfo="true"
                        DisplayMsg="Displaying Jobs {0} - {1} of {2}"
                        />
                </BottomBar>
            </ext:GridPanel>
            <script type="text/javascript">
                autoResizeColumn = function (grid, columnIndex, e) {
                    var view = grid.getView(),
                        store = grid.getStore(),
                        colModel = grid.getColumnModel(),
                        cell,
                        width;                
                    if (Math.abs(e.getPageX() - Ext.get(view.getHeaderCell(columnIndex)).getX()) < 20) {
                        columnIndex = columnIndex > 0 ? columnIndex - 1 : 0; // to resize column on left
                    }
                    width = Ext.get(view.getHeaderCell(columnIndex)).child(".x-grid3-hd-inner").getTextWidth();
    
                    if (Ext.isChrome && colModel.config[columnIndex].commands) return; // Chrome has problem with resizing commandcolumn
                    if (store.sortInfo && store.sortInfo.field == colModel.config[columnIndex].dataIndex) {
                        width += 15; // 15px + for sorting arrows
                    }
                    store.each(function (record, rowIdx) {
                        cell = Ext.get(view.getCell(rowIdx, columnIndex));
                        width = Math.max(width, cell.child(".x-grid3-cell-inner").getTextWidth());
                    });
                    colModel.setColumnWidth(columnIndex, width + 10);
                    grid.autoExpandColumn = null;
                };
                ensureAutoResizeDoubleClick = function (grid, columnIndex, e) {                
                    var view = grid.getView();
                    header = view.getHeaderCell(view.activeHdIndex),
                    c = header.style.cursor;
                    return !(c === "col-resize" || c === "move" || c === "e-resize" || c === "w-resize");
                };
            </script>
         </form>
        
    </body>
    </html>
  8. #8
    Thank you for the sample. But I don't understand what issue is with the example. Please clarify.
  9. #9
    There should be no issue any more. I fixed it before providing full example.
    I also edited my first post with JS functions to remove that bug (in case someone will copy it).
  10. #10
    Ok, I misunderstood. Thank you for sharing!

Similar Threads

  1. Replies: 8
    Last Post: Jun 15, 2012, 12:43 PM
  2. GridPanel double click
    By AlexMaslakov in forum 1.x Help
    Replies: 1
    Last Post: Sep 12, 2011, 9:52 AM
  3. Replies: 2
    Last Post: Aug 04, 2011, 2:14 PM
  4. Replies: 8
    Last Post: Apr 11, 2011, 12:55 PM
  5. [CLOSED] Row double click event on gridpanel
    By yobnet in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Apr 29, 2010, 2:54 AM

Tags for this Thread

Posting Permissions