jQuery数据表插件不删除表行 [英] jQuery Data Tables plugin not removing table row

查看:86
本文介绍了jQuery数据表插件不删除表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出为什么当我点击我的删除按钮,它的更改会执行PHP的一些事情,但是当它回到客户端时,它不会从表中删除该行。我正在使用表的数据表。

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. 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] );
        }
    });
});

来自php端的回复是这样的:

The response from the php side is this:

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

编辑:

这是我现在正在使用的,我有一个有趣的没有定义fnGetSelected的错误消息。所以我不知道我是否可以正确地删除表格行。

Here's what I'm using now and I'm getting an interesting error message that says fnGetSelected is not defined. 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是在数据表功能中发现的一个对象,在其中定义了一个对象,一旦函数已经运行,并且变量将被破坏,因为它不是任何类型的全局变量。

oTable is an object found within the datatables functionality and is defined there within it. 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.

$('.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] );
        }
    });
});

如果您期望使用$ .post和数据回来,请指定所需的数据和类型返回是一个好主意

If your expecting to work with $.post and data coming back, specifying the data and type you want back is a good idea

$('.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');
});

也值得一提的不是试图在它渲染后使用datatable对象..你可以和很可能你最简单的路线取决于你的删除按钮所在的地方,这样做就像这样的一样:

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();

假设按钮/链接任何,与要删除的行在同一行..你可以做如上所述的事情,因为这仅仅是为了删除所讨论的表行的视觉效果。还假设你的ajax正在更改下一次加载页面的数据,因此它不再在该集合中,因此不会被包含在下一个加载中

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

这篇关于jQuery数据表插件不删除表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆