2015-12-09 33 views
0

我有一个数据表格实例的网页上有全数字分页。这包括第一个,上一个,下一个和最后一个按钮以及单独编号的按钮。删除和恢复datatables分页点击功能

表中的每一行都有一个“编辑”链接。当我点击它时,我想禁用分页工作。当我点击“取消”按钮时,我想恢复分页功能。我可以轻松完成第一部分,但无法恢复分页点击功能。这里是我的代码:

function ghostPage(state) 
{ 
    // In this method, 'state' will be either true or false 
    // If it's true, remove the click handler 
    // If it's false, restore the click handler 

    if(state) 
    { 
     $('#displayTable_paginate').find('a').each(function(e){ 
      $(this).off('click'); 
     }) 
    } 
    else 
    { 
     $('#displayTable_paginate').find('a').each(function(){ 
      $(this).on('click'); 
     }) 
    } 
} 

回答

0

如果您想保存点击处理程序并稍后恢复,请尝试此操作。

function saveClickHander(){ 
    var $this; 
    $('#displayTable_paginate').find('a').each(function(e){ 
     $this = $(this); 
     $this.data().originalClick = $this.data("events")['click'][0].handler; 
    }) 

} 

function ghostPage(state) 
{ 
    // In this method, 'state' will be either true or false 
    // If it's true, remove the click handler 
    // If it's false, restore the click handler 

    if(state) 
    { 
     $('#displayTable_paginate').find('a').each(function(e){ 
      $(this).off('click'); 
     }) 
    } 
    else 
    { 
     var $this; 
     $('#displayTable_paginate').find('a').each(function(){ 
      $this = $(this); 
      $this.on('click', $this.data().originalClick); 
     }) 
    } 
} 
+0

我不确定这是如何工作的。尽管如此,我尝试了它。 – Ann

+0

我应该说,我没有其他方式来阻止点击事件的工作。我试过禁用父div没有成功。我可以隐藏整个按钮的div,但这非常笨重。有点奇怪;似乎我可以用这些div来完成所有我想要的操作,除了切换点击和关闭。 – Ann

+0

最初,click处理函数绑定到元素的click监听器。 $(this).off('click')只是将处理器引用设置为null。您想在解除绑定之前在某处存储引用,以便稍后恢复。我不知道为什么它不适合你。这可能是因为你使用了“div.onclick = funciton(){...}”。看起来禁用元素是一种更好的方法,但我认为JQuery中没有“禁用”功能。一个替代方案可以隐藏按钮,并显示相同样式的非响应按钮,虽然它有点难看.. –