简体   繁体   English

jQuery数据表插件不删除表行

[英]jQuery Data Tables plugin not removing table row

I'm trying to figure out why when I click my delete button it changes performs the php side of things but when it gets back to the client side it doesn't delete the row from the table. 我试图找出为什么当我点击我的删除按钮时,它更改执行php方面的东西,但当它返回到客户端时,它不会删除表中的行。 I'm using datatables for my tables. 我正在为我的桌子使用数据表。

$('.delete').click(function() { 
    var titleID = $(this).attr('rel');
    $.post('titles/delete', { titleID:titleID }, function(data) {
        if (data.success)
        {
            var anSelected = fnGetSelected( oTable );
            oTable.fnDeleteRow( anSelected[0] );
        }
    });
});

The response from the php side is this: php方面的反应如下:

{"success":"Yes","message":"Title was deleted successfully!"}

EDIT : 编辑:

Here's what I'm using now and I'm getting an interesting error message that says fnGetSelected is not defined. 这就是我现在正在使用的内容,我收到一条有趣的错误消息,指出未定义fnGetSelected。 So I'm not sure if I"m even doing this properly to delete a table row. 所以我不确定我是否正确地删除表格行。

$('.delete').click(function() { 
var titleID = $(this).attr('rel');
$.post('titles/delete', { titleID:titleID }, function(data) {
    if (data.success)
    {
        var anSelected = fnGetSelected( oTable );
        oTable.fnDeleteRow( anSelected[0] );
    }
}, 'json');
});

oTable is an object found within the datatables functionality and is defined there within it. oTable是在数据表功能中找到的对象,并在其中定义。 Once the function has ran its course and rendered whatever it will the variable is destroyed as its not a global variable of any sort. 一旦函数运行并且渲染了它将会被破坏的变量,因为它不是任何类型的全局变量。

also

$('.delete').click(function() { 
    var titleID = $(this).attr('rel');
    $.post('titles/delete', { titleID:titleID }, function(data) {
        if (data.success)
        {
            var anSelected = fnGetSelected( oTable );
            oTable.fnDeleteRow( anSelected[0] );
        }
    });
});

If your expecting to work with $.post and data coming back, specifying the data and type you want back is a good idea 如果您希望使用$ .post和数据返回,指定您想要的数据和类型是一个好主意

$('.delete').click(function() { 
    var titleID = $(this).attr('rel');
    $.post('titles/delete', { titleID:titleID }, function(data) {
        if (data.success)
        {
            var anSelected = fnGetSelected( oTable );
            oTable.fnDeleteRow( anSelected[0] );
        }
    }, 'json');
});

also worth mentioning is rather than trying to work with the datatable object after its rendered.. you could and most likely your easiest route depending on where your "delete" button is located do something like 另外值得一提的是,不是在渲染之后尝试使用数据表对象..你可以而且很可能是最简单的路线,取决于你的“删除”按钮所在的位置做类似的事情

$(this).parent('tr').remove();

Assuming the button/link whatever, is in the same row as the one to be removed.. you can do something like above mentioned as this is just purely for visual effect of removing the tables row in question. 假设按钮/链接是什么,与要删除的那一行在同一行..你可以做上面提到的事情,因为这纯粹是为了删除有问题的表行的视觉效果。 Also assuming your ajax is changing the data for the next time you load the page so its not in the set anymore and thus wont be included on the next load 还假设您的ajax正在更改下次加载页面时的数据,因此它不再在集合中,因此不会包含在下一个加载中

there are multiple problems with the code u hav provided here... 这里提供的代码存在多个问题......
1.going by the response you are getting for the POST request. 1.通过您获得POST请求的响应。
you are getting "YES" value for "Success" property which i dont think if in javascript would take as true . 你获得“成功”属性的“YES”值,我认为if在javascript中这样做是true
2. fnGetSelected function of datatables needs an instance to work upon.. you cant just call the function as if it is a global one.. so if u hav the table refrence stored in a varibale like dTable then u can call it as dTable.fnGetSelected() 2.数据表的fnGetSelected函数需要一个实例来处理..你不能只是调用函数就好像它是一个全局函数。所以如果你有一个存储在像dTable这样的变量中的表dTable那么你可以把它称为dTable.fnGetSelected()
so probabaly you should change this 所以你应该改变这个

var anSelected = fnGetSelected( oTable );

to this 对此

var anSelected = oTable.fnGetSelected();


im assuming that oTable has your datatable 我假设oTable有你的数据表
so to debug open the developer tools by pressing f12 and look for javascript error in console or wherever you have depending upon ur browser 所以要通过按f12调试打开开发人员工具,并在控制台或任何地方查找javascript错误,具体取决于你的浏览器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM