Consistent grid headers with Flexigrid ?

前端 未结 12 1049
长发绾君心
长发绾君心 2021-02-03 10:05

So I\'m trying to use the flexigrid plugin. It looks like it\'s going to work great aside from the fact that it looks like you have to manually set the column widths. Otherwis

12条回答
  •  [愿得一人]
    2021-02-03 10:45

    I've been working on this today, too. What I've come up with involves adding an onSuccess handler and figuring out what the maximum size of each column is between the header and the body, then setting the width of both to the maximum for that column.

    grid.flexigrid({
    
      ...other setup...
    
      onSuccess: function() {
           format();
        });
      }
    });
    
    function format() {
        var gridContainer = this.Grid.closest('.flexigrid');
        var headers = gridContainer.find('div.hDiv table tr:first th:not(:hidden)');
        var drags = gridContainer.find('div.cDrag div');
        var offset = 0;
        var firstDataRow = this.Grid.find('tr:first td:not(:hidden)');
        var columnWidths = new Array( firstDataRow.length );
        this.Grid.find( 'tr' ).each( function() {
            $(this).find('td:not(:hidden)').each( function(i) {
                var colWidth = $(this).outerWidth();
                if (!columnWidths[i] || columnWidths[i] < colWidth) {
                    columnWidths[i] = colWidth;
                }
            });
        });
        for (var i = 0; i < columnWidths.length; ++i) {
            var bodyWidth = columnWidths[i];
    
            var header = headers.eq(i);
            var headerWidth = header.outerWidth();
    
            var realWidth = bodyWidth > headerWidth ? bodyWidth : headerWidth;
    
            firstDataRow.eq(i).css('width',realWidth);
            header.css('width',realWidth);            
            drags.eq(i).css('left',  offset + realWidth );
            offset += realWidth;
        }
    }
    

提交回复
热议问题