4

グリッドで剣道列メニューを使用しています。列メニューの項目のチェックを外すと、列幅が全幅を適切に占有しません。これを修正する方法を教えてください。

var grid = $("#grid").kendoGrid({
    dataSource: {
        data  : createRandomData(100),
        pageSize : 10,
        schema: {
            model: {
                fields: {
                    Id       : { type: 'number' },
                    FirstName: { type: 'string' },
                    LastName : { type: 'string' },
                    City     : { type: 'string' }
                }
            }
        }
    },
    pageable  : true,
    columnMenu: true,
    columns   : [
        { field: "FirstName", width: 90, title: "First Name" },
        { field: "LastName", width: 90, title: "Last Name" },
        { field: "City", width: 100 }
    ],
    dataBound: function () {
        $(".k-header-column-menu").kendoTooltip({
            content: "column menu"
        });
    }
}).data("kendoGrid");

このフィドルをチェックしてください

http://jsfiddle.net/OnaBai/JCSGz/

4

3 に答える 3

3

問題は、width各列に指定されているためです。グリッドが最初にロードされるとき、幅は無視され、合計の比率として使用されます。列メニューを使用すると、幅があなたの発言に強制されます。

達成したい内容に応じて、単にwidth列定義から削除するかGrid、目的の幅であることを確認してください。

ここでグリッドの全幅を使用するように列のサイズを変更する例http://jsfiddle.net/OnaBai/JCSGz/2/

各列の幅を維持するためにテーブルのサイズを変更したい場合は、次のことを行う必要があります。

  1. たとえば、CSS スタイルを使用してグリッド幅を定義します。
  2. 関数を columnShow イベントにバインドし、列の幅をグリッドの現在の幅に追加します。
  3. 関数を columnHide イベントにバインドし、列の幅をグリッドの現在の幅から減算します。

CSS:

#grid {
    width: 300px
}

JavaScript:

var grid = $("#grid").kendoGrid({
    dataSource: {
        data  : createRandomData(100),
        pageSize : 10,
        schema: {
            model: {
                fields: {
                    Id       : { type: 'number' },
                    FirstName: { type: 'string' },
                    LastName : { type: 'string' },
                    City     : { type: 'string' }
                }
            }
        }
    },
    pageable  : true,
    columnMenu: true,
    columnHide: function (e) {
        this.wrapper.width(grid.wrapper.width() - e.column.width);
    },
    columnShow: function (e) {
        this.wrapper.width(grid.wrapper.width() + e.column.width);
    },
    columns   : [
        { field: "FirstName", width: 100, title: "First Name" },
        { field: "LastName", width: 50, title: "Last Name" },
        { field: "City", width: 150 }
    ],
    dataBound: function () {
        $(".k-header-column-menu").kendoTooltip({
            content: "column menu"
        });
    }
}).data("kendoGrid");

JSFiddle での実行例: http://jsfiddle.net/OnaBai/JCSGz/4/

于 2013-09-26T10:24:14.963 に答える
0

私は同じ問題を抱えていました。このリンクで解決策を得ました

function loadColumnState(columnStateKey: string, realGrid): void
{  
    var colState = JSON.parse($.jStorage.get(columnStateKey));

    if(colState && colState.length > 0) 
    {
        var visibleIndex = -1;
        for (var i = 0; i < colState.length; i++) 
        {                
            var column = colState[i];

            // 1. Set correct order first as visibility and width both depend on this.                                     
            var existingIndex = -1;

            if (typeof column.field !== 'undefined')
            {
                existingIndex = findFieldIndex(realGrid, column.field);
            }
            else if (typeof column.commandName !== 'undefined')
            {
                existingIndex = findCommandIndex(realGrid, column.commandName);
            }

            if (existingIndex > -1 && existingIndex != i) // Different index
            {   // Need to reorder
                realGrid.reorderColumn(i, realGrid.columns[existingIndex]);
            }

            // 2. Set visibility state
            var isHidden = (typeof column.hidden === 'undefined') ? false : column.hidden;

            if (isHidden) 
            { 
                realGrid.hideColumn(i); 
            } 
            else 
            { 
                realGrid.showColumn(i); 
                ++visibleIndex;
            }

            // 3. Set width
            var width = (typeof column.width === 'undefined') ? null : column.width;

            if(width != null)
            {
                realGrid.columns[i].width = width; // This sets value, whilst rest redraws
                realGrid.thead.prev().find('col:eq(' + visibleIndex + ')').width(width);
                realGrid.table.find('>colgroup col:eq(' + visibleIndex + ')').width(width); 
            }                              
        }
    }    
}
于 2014-02-21T06:37:28.533 に答える