2011-11-16 47 views
0

我正在使用Datatables和一个表中有两列第二列有一个文本链接与类.deletecomp单击时ajax从数据库中删除条目,但我有麻烦刷新桌子。jquery绑定点击事件类? datatables ajax从标记删除行

我在// Test delete tr注释下面添加了代码,当我单击表中的任何tr时,它将删除该行并刷新表。 AJAX被激发并且db表中的条目被删除。我怎样才能改变这种功能与具有.deletecomp类我删除文本链接工作

这是怎么了初始化表

oTableVC = $('#viewcomp').dataTable({ 
     "sDom":'t<"bottom"filp><"clear">', 
     "bAutoWidth": false, 
     "sPaginationType": "full_numbers", 
      "aoColumns": [ 
      null, 
      null 
      ] 
    } 
    ); 
    //Test delete tr 
    $("#viewcomp tbody tr").live('click', function() { 
     oTableVC.fnDeleteRow(this); 
    }); 

}); 

这是删除数据库条目中的AJAX

$(".deletecomp").live('click', function(event){ 
    event.preventDefault(); 
    cidstring = $(this).attr("id"); 
    cidArr = cidstring.split("-"); 
    cid = cidArr[1]; 
    var url = 'http://domain.com/ajaxdeletecomp?format=json'; 

    $.post(url, { id: cid},function(ajaxdata) { 
     //deleting row from table 

     }); 
    }); 

回答

0

我以前没有用过这个插件;然而根据您的代码以下应该做的伎俩:

// Needs to be declared in the the JS Document outside of any functions // 
var oTableVC = $('#viewcomp').dataTable({ 
    "sDom":'t<"bottom"filp><"clear">', 
    "bAutoWidth": false, 
    "sPaginationType": "full_numbers", 
    "aoColumns": [ 
     null, 
     null 
     ] 
    } 
); 


$(".deletecomp").live('click', function(event){ 
    event.preventDefault(); 
    cidstring = $(this).attr("id"); 
    cidArr = cidstring.split("-"); 
    cid = cidArr[1]; 
    var url = 'http://domain.com/ajaxdeletecomp?format=json'; 
    var $target = $(this).closest('tr'); // Add Reference </tr> of Link 

    $.post(url, { id: cid},function(ajaxdata) { 
     //deleting row from table 
     oTableVC.fnDeleteRow($target); // Deletes Row 
    }); 
}); 

我希望这有助于!

+0

我们尝试将fnDeleteRow函数放在那里,但它并没有触发 – Anagio

+0

@Anagio oTableVC var最有可能超出范围;看看我的编辑。确保如上添加var部分。 – dSquared

+0

我还没有...我测试过fnClearTable();查看它是否放置在var url上方时触发,并且在单击删除链接时清除表。我不明白为什么DeleteRow不起作用 – Anagio