代码之家  ›  专栏  ›  技术社区  ›  Jake Sylvestre jjNford

如何禁用jquery数据表上的拖放

  •  2
  • Jake Sylvestre jjNford  · 技术社区  · 8 年前

    因此,我使用jquery数据表和 colReorder 设置表格。此表具有一些功能,例如如何单击列标题并选择要切换的列。为了使此功能正常工作,我必须启用 colReorder 唯一的问题是,我所有的列都是可拖动的。我怎样才能解决这个问题?

    这是我试过的

    • 背景 draggable
    • 背景 bsort
    • 背景 bsortable

    请让我知道我做错了什么。还有,这是我的构造函数:

            window.table =
            $table.DataTable({
                data: window.apiData['data'],
                /**
                dat
                 * Specify which columns we're going to show
                 */
                columns: window.columnMapping,
                /**
                 * we want to disable showing page numbers, but still limit the number of results
                 */
                dom: "t",
                /**
                 * let's disable some dynamic custom css
                 */
                asStripClasses: [],
                /**
                 * let's keep the pages reasonable to prevent scrolling
                 */
                pageLength: 8,
    
                /**
                 * this helps with hotswapping columns
                 */
                colReorder: true
            });
    

    谢谢

    1 回复  |  直到 8 年前
        1
  •  2
  •   davidkonrad    8 年前

    您可以覆盖ColReorders事件绑定到 <th> 元素。幸运的是,事件丰富了一个名称空间,因此可以很容易地跟踪它们 ColReorder.mousedown 负责触发列拖动。因此,您可以通过以下方式重置该功能:

    function resetColReorderMD() {
      $('.dataTable thead th').each(function() {
        var md = $._data($(this)[0]).events.mousedown;
        for (var i=0, l=md.length; i<l; i++) {
          if (md[i].namespace == 'ColReorder') {
            md[i].handler = function() {}
          }
        }  
      })
    }
    
    $('#example').DataTable({
      colReorder: true,
      initComplete: function() {
        resetColReorderMD()
      }
    })  
    

    演示->; http://jsfiddle.net/2y4w3v6g/

    使用ColReorder插件时禁用列重新排序似乎没有什么意义。我猜上面提到的“功能”正在大量使用ColReorder功能,而这是真正的问题,以上应该被视为不可取的黑客行为。