SlickGrid的行高可以动态改变吗?

Dav*_*sen 9 slickgrid

我们正在实现用户偏好(立即)在网格上显示"更多"或"更少"的数据."更多"应该增加行高(每行具有相同的增加高度).

当用户切换时,我们更新DataView,并使用更新的rowHeight值调用网格上的setOptions.然后我们调用invalidate()和render().

但行高度未更新.:(

有人可以建议解决方案吗?我们应该直接通过CSS改变高度吗?如果是这样,有关这方面的任何提示吗?

vio*_*313 15

事实上,这可以根据用户交互动态更新行的高度.在Slickgrid API提供了所有我们需要的.

因为:

  1. 我们可以动态添加/删除行;
  2. 我们可以css在行和单元级别动态应用自定义.


这是一个简单的演示来开始:

////////////////////////////////////////////////////////////////////////////////
//example codez re trying to create a grid with rows of dynamic height to
//cater for folks that wanna bung loads of stuff in a field & see it all...
//by violet313@gmail.com ~ visit: www.violet313.org/slickgrids
//have all the fun with it  ;) vxx.
////////////////////////////////////////////////////////////////////////////////
modSlickgridSimple=(
function()
{
    var _dataView=null;
    var _grid=null;
    var _data=[];


    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    var getPaddingItem=function(parent , offset)
    {
        var item={};

        for (var prop in _data[0]) item[prop]=null;
        item.id=parent.id+"."+offset;

        //additional hidden padding metadata fields
        item._collapsed=     true;
        item._isPadding=     true;

        return item;
    }

    //////////////////////////////////////////////////////////////
    //this just builds our expand collapse button
    //////////////////////////////////////////////////////////////
    var onRenderIDCell=function(row, cell, value, columnDef, item)
    {
        if (item._isPadding==true); //render nothing
        else if (item._collapsed) return "<div class='toggle expand'></div>";
        else
        {
            var html=[];
            var rowHeight=_grid.getOptions().rowHeight;

            //V313HAX:
            //putting in an extra closing div after the closing toggle div and ommiting a
            //final closing div for the detail ctr div causes the slickgrid renderer to
            //insert our detail div as a new column ;) ~since it wraps whatever we provide
            //in a generic div column container. so our detail becomes a child directly of
            //the row not the cell. nice =)  ~no need to apply a css change to the parent
            //slick-cell to escape the cell overflow clipping.

            //sneaky extra </div> inserted here-----------------v
            html.push("<div class='toggle collapse'></div></div>");

            html.push("<div class='dynamic-cell-detail' ");   //apply custom css to detail
            html.push("style='height:", item._height, "px;"); //set total height of padding
            html.push("top:", rowHeight, "px'>");             //shift detail below 1st row
            html.push("<div>",item._detailContent,"</div>");  //sub ctr for custom styling
            //&omit a final closing detail container </div> that would come next

            return html.join("");
        }
    }

    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    var onRowClick=function(e, args)
    {
        _dataView.beginUpdate();

        if ($(e.target).hasClass("toggle"))
        {
            var item=_dataView.getItem(args.row);

            if (item)
            {
                if (!item._collapsed)
                {
                    item._collapsed=true;
                    for (var idx=1; idx<=item._sizePadding; idx++)
                        _dataView.deleteItem(item.id+"."+idx);
                    item._sizePadding=0;
                }
                else
                {
                    item._collapsed=false;
                    kookupDynamicContent(item);
                    var idxParent=_dataView.getIdxById(item.id);
                    for (var idx=1; idx<=item._sizePadding; idx++)
                        _dataView.insertItem(idxParent+idx, getPaddingItem(item,idx));
                }
                _dataView.updateItem(item.id, item);
            }
            e.stopImmediatePropagation();
        }

        _dataView.endUpdate();
    }

    //////////////////////////////////////////////////////////////
    var gridOptions={ enableColumnReorder:  true };

    //////////////////////////////////////////////////////////////
    var _gridColumns=
    [
        {
            id:         "id",
            name:       "",
            field:      "id",
            resizable:  false,
            width:      20,
            formatter:  onRenderIDCell,
        },
        {id: "title",        name: "Title",         field: "title",        resizable: true},
        {id: "duration",     name: "Duration",      field: "duration",     resizable: true},
        {id: "pcComplete",   name: "% Complete",    field: "pcComplete",   resizable: true},
        {id: "start",        name: "Start",         field: "start",        resizable: true},
        {id: "finish",       name: "Finish",        field: "finish",       resizable: true},
        {id: "effortDriven", name: "Effort Driven", field: "effortDriven", resizable: true},
    ];

    //////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////
    var kookupTestData=(function()
    {
        for (var i = 0; i < 100; i++)
            _data[i] =
            {
                id:               i,
                title:            "Task " + i,
                duration:         "5 days",
                pcComplete:       Math.round(Math.random() * 100),
                start:            "01/01/2009",
                finish:           "01/05/2009",
                effortDriven:     (i % 5 == 0),

                //additional hidden metadata fields
                _collapsed:       true,
                _sizePadding:     0,     //the required number of pading rows
                _height:          0,     //the actual height in pixels of the detail field
                _isPadding:       false,
            };
    })();

    //////////////////////////////////////////////////////////////
    //create the detail ctr node. this belongs to the dev & can be custom-styled as per
    //////////////////////////////////////////////////////////////
    var kookupDynamicContent=function(item)
    {
        //add some random oooks as fake detail content
        var oookContent=[];
        var oookCount=Math.round(Math.random() * 12)+1;
        for (var next=0; next<oookCount; next++)
            oookContent.push("<div><span>oook</span></div>");
        item._detailContent=oookContent.join("");

        //calculate padding requirements based on detail-content..
        //ie. worst-case: create an invisible dom node now &find it's height.
        var lineHeight=13; //we know cuz we wrote the custom css innit ;)
        item._sizePadding=Math.ceil((oookCount*lineHeight) / _grid.getOptions().rowHeight);
        item._height=(item._sizePadding * _grid.getOptions().rowHeight);
    }

    //////////////////////////////////////////////////////////////
    //jquery onDocumentLoad
    //////////////////////////////////////////////////////////////
    $(function()
    {
        //initialise the data-model
        _dataView=new Slick.Data.DataView();
        _dataView.beginUpdate();
        _dataView.setItems(_data);
        _dataView.endUpdate();

        //initialise the grid
        _grid=new Slick.Grid("#grid-simple", _dataView, _gridColumns);
        _grid.onClick.subscribe(onRowClick);

        //wire up model events to drive the grid per DataView requirements
        _dataView.onRowCountChanged.subscribe
            (function(){ _grid.updateRowCount();_grid.render(); });

        _dataView.onRowsChanged.subscribe
            (function(e, a){ _grid.invalidateRows(a.rows);_grid.render(); });

        $(window).resize(function() {_grid.resizeCanvas()});
    });
}
)();
//////////////////////////////////////////////////////////////
//done ;)
Run Code Online (Sandbox Code Playgroud)
::-webkit-scrollbar       
{ 
    width:              12px; 
    background-color:   #B9BACC; 
}
::-webkit-scrollbar-track 
{ 
    color:              #fff; 
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3); 
    border-radius:      10px;  
}
::-webkit-scrollbar-thumb 
{ 
    color:              #96A9BB; 
    border-radius:      10px;  
    -webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.5); 
}

body
{
    font-family:        Arial, Helvetica, sans-serif;
    background-color:   #131313;
    position:           absolute;
    top:                5px;
    bottom:             5px;
    left:               5px;
    right:              5px;
}

#grid-simple
{
    position:         absolute;
    top:              0px;
    left:             0px;
    right:            0px;    
    bottom:           0px;
    margin:           auto;
    font-size:        12px;
    background-color: #ECEEE9;
}

.toggle
{
    height:           16px;
    width:            16px;
    display:          inline-block;
}
.toggle.expand
{
    background: url(http://tinyurl.com/k9ejb3a/expand.gif) no-repeat center center;
}

.toggle.collapse
{
    background: url(http://tinyurl.com/k9ejb3a/collapse.gif) no-repeat center center;
}


/*--- generic slickgrid padding pollyfill  ----------------------*/
 
.dynamic-cell-detail
{
    z-index:            10000;
    position:           absolute;
    background-color:   #F4DFFA;
    margin:             0;
    padding:            0;
    width:              100%;
    display:            table;
}

.dynamic-cell-detail > :first-child
{
    display:            table-cell;
    vertical-align:     middle;
    text-align:         center;
    font-size:          12px;
    line-height:        13px;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/Celebio/SlickGrid/master/lib/jquery.event.drag-2.2.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/Celebio/SlickGrid/master/slick.core.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/Celebio/SlickGrid/master/slick.grid.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/Celebio/SlickGrid/master/slick.dataview.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/Celebio/SlickGrid/master/slick.grid.css">
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/Celebio/SlickGrid/master/slick-default-theme.css">
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/base/jquery-ui.css">


<div id="grid-simple"></div>
Run Code Online (Sandbox Code Playgroud)

少于200行代码.
摆弄它!

顺便说一下,这种方法也是优秀的Datatables (几乎)通过它的API本身提供的方法.&imo这是正确的模式; 以及我如何选择使用自己的东西Slickgrid.但它涉及轻微的黑客攻击,无论如何*不完全符合OP要求; 我声称是可能的.


为了对每个单元格进行动态行高,我们采用了类似的技巧,但我们还必须处理一些副作用:〜

造型排除了划分

我们必须:

  1. 逃避每单元溢出剪辑
  2. 删除不需要的行stipeyness
  3. 删除行边框

Slickgrid API通过提供基于行的造型Grid.getItemMetadata回调接口.在第107行的下一个小提琴中,看看onRenderRow这个界面的实现:
摆弄它!

另请注意,在第148-150行,我调用Slickgrid Grid.setCellCssStyles API来添加一个自定义dynamic-cell css类来设置overflowto visible,以便去掉每个单元格的溢出剪辑.

列调整大小

如果详细内容是静态的,则列大小调整是一种限制. 响应列宽(流动文本或wotnot)变化的

细节内容需要一些工作.填充行需要动态添加和删除.见(从第66行)的和功能在未来的小提琴: 拨弄它!addPaddingtrimPadding

排序

这里还有一些工作要做.我们需要确保无论我们是向上还是向下排序,填充都会在父级下方连续存在.看到comparer线136:在接下来的拨弄
它小提琴!

滤波

几乎是一个单行:如果是填充,则将比较委托给父.任务完成.看到pcFilter线192:在未来拨弄
它小提琴!

好极了!这是调整大小,排序和过滤500线以下相当清晰,自由评论的自定义javascripts ..我实际上看到某些花哨的input-range-sliderpollyfill与更多的代码行;)
acu


注意事项

我只介绍了基础知识.有一个完整的可选/可编辑的方面Slickgrid,〜超出了我目前的要求(sry).
也:

  • 仅示例代码; 没准备好生产.你被警告过等等=)
  • 示例似乎适用于大多数现代浏览器; 我有没有/尝试过iE; 版本> = 11可能没问题..

更多信息

除了SO无参考政策指导方针之外,还有更多可以说的可以合理地被解释为答案.任何有兴趣了解所有这些东西的人都可以到这里进行更详细的介绍.

最后一个例子

这是最后一个有趣的例子.它采用了上述所有功能,但可以看出,我已经抛弃了expando-rows,并且有两个动态内容字段.另外,fyi,这个例子利用MutationObservers来生成onPostRender事件,作为本机 列选项回调的替代方法: 摆弄它!SlickgridasyncPostRender

我们终于得到它了.- 一路走向DataView-like Slickgrid extension-mod; 而且无需在可爱的Slickgrid代码上使用可怕的hax.yippee;)


G-M*_*Man 5

你可以通过CSS来做到这一点.看一下该slick.grid.css文件,然后在那里进行所需的更改.

看看吧

.slick-row.ui-widget-content,.slick-row.ui-state-active课程

要么

您可以使用rowHeightSlickGrid 的属性

看看网格选项

https://github.com/mleibman/SlickGrid/wiki/Grid-Options

  • 如果您还没有尝试过,为什么要将其标记为已完成?我有关于行高的相同问题,但现在我不知道这个答案是否有用. (14认同)