【问题标题】:SlickGrid - Selecting a Row without custom controlsSlickGrid - 选择没有自定义控件的行
【发布时间】:2012-01-23 20:20:24
【问题描述】:

我需要应用程序中的功能来允许用户单击一行来选择一行。此外,我想为 Ctrl/Shift 添加功能 + 单击以选择一行。

我看到了将复选框添加到每一行以选择一行的示例。我不希望将此类复选框添加到应用程序中。

我遇到了一个已有一年的链接,但似乎回答了我的问题http://groups.google.com/group/slickgrid/browse_thread/thread/96a5291161d41efa

这里有更好的处理方式吗?

【问题讨论】:

  • 你想对“选定的”表格行元素做什么?

标签: jquery slickgrid


【解决方案1】:

SlickGrid DOM 不是静态的 - 请避免直接修改它。

此功能已通过可插入的 SelectionModel 内置到 SlickGrid 中。发行版中包含两个选择模型 - Slick.CellSelectionModel 和 Slick.RowSelectionModel。你所要做的就是打电话

grid.setSelectionModel(new Slick.RowSelectionModel());

有关示例,请参阅 http://mleibman.github.com/SlickGrid/examples/example9-row-reordering.html

【讨论】:

  • 非常感谢蒂姆,正是我想要的。
  • 嗨 Tin,执行此 [new Slick.RowSelectionModel()] 时找不到 RowSelectionModel() 函数。你有什么想法吗?
  • Stiger,您可能需要在 slick.grid.js 脚本之后将<script src="slick.rowselectionmodel.js"></script> 添加到标题
  • 一个稍微更新的规范链接是mleibman.github.io/SlickGrid/examples/…
【解决方案2】:

这是一个 83Kb 的文件(只是 JS),这并不可怕,但对于您想要做的事情来说相当大。

//select the parent table element, then select its child (the `tbody` element) then get its children which will be the `tr` elements
$('table').children().children().on('click', function (event) {

    //check to see if ctrl+shift is being held while this click occured
    if (event.ctrlKey === true && event.shiftKey === true) {

        //if ctrl+shift were held during the click then you know this element has been selected
        //and you can do what you need, in this demo I'm just adding a class to denote that the element has been selected
        $(this).toggleClass('selected');
    }
});

如您所见,如果您只想测试在单击 tr 元素时是否按住 ctrl+shift,那么您可以使用少于 83KB 的代码(上面的演示是 600 字节)。

这是一个演示:http://jsfiddle.net/YZytJ/

注意 .on() 是 jQuery 1.7 中的新内容,在这种情况下与使用 .bind() 相同:http://api.jquery.com/on

【讨论】:

  • 您好 Jasper,感谢您的及时回复。我在这里的基本关注是围绕网格结构(在本例中为表)所做的解决方案中有一些基本假设。如果由于某种原因结构发生变化(或添加了一些渲染器),则可能存在问题。那么我们是否知道我们是否有针对 slickgrid 的开箱即用的东西?我的应用程序中有一个 HTML Table 控件,我当然会将您提供的解决方案用于该控件。 -Yazad Khambata
  • 我无法评论slickgrid 插件,我从未使用过它。然而,它很可能使用至少保持静态的 HTML 结构。您可以使用您的开发人员工具检查 DOM 并找出 slickgrid 正在创建的 HTML 结构。
  • 再次感谢 Jasper... 我检查了 SlickGrid 的 dom 结构,该结构基于 DIV。我认为根据您分享的示例,我应该能够将其与 SlickGrid 并列,但是是的,如果我从某人那里获得解决方案或指向更 SlickGrid 特定的开箱即用解决方案的指针,我仍然会非常高兴。 :)
猜你喜欢
  • 2015-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 1970-01-01
  • 2013-09-03
  • 2016-04-19
  • 1970-01-01
  • 2011-05-15
相关资源
最近更新 更多