0

Please, i have this code here which helps me to extract a few rows from my database into a grid.

Now i have this select all option. Here is the function:

function removeUser(){
    var row = $('#dg').datagrid('getSelected');
    if (row){
        $.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){
            if (r){
                $.post('remove_er.php',{id:row.id},function(result){
                    if (result.success){
                        $('#dg').datagrid('reload');    // reload the user data
                    } else {
                        $.messager.show({   // show error message
                    title: 'Error',
                    msg: result.msg
                        });
                    }
                },'json');
            }
        });
    }
}

And the php:

$id = intval($_REQUEST['id']);
$sql = "delete from rbpos_epos where id=$id";
$result = @mysql_query($sql);

Where do i have to modify in order to delete all the selected rows? For the moment even though i have selected all, i still can delete just one record..

Thanks..

4

3 回答 3

0

看起来您只对所选列表的第一个元素采取行动。

您需要遍历每一行,并且可以为每一行发出 delete $.post() ,或者将字符串生成器与 Orangepill 的解决方案结合起来,在一次调用中将它们全部删除。

我不熟悉“.datagrid()”方法,也许返回的对象有一个“.each()”方法,或者一个“.next()”方法来遍历所有行?

于 2013-08-01T15:27:53.597 回答
0

您可以使用:

$ids = is_array($_REQUEST['id'])?$_REQUEST['id']:array($_REQUEST["id");
$ids = array_map("intval", $ids);
$sql = "delete from rbpos_epos where id in (".implode(",",$ids).")";
$result = @mysql_query($sql);
于 2013-08-01T15:17:11.313 回答
0

在研究.datagrid()了一下之后,看起来你可能需要一些来自这个底部的东西

摘抄:

var ids = [];  
var rows = $('#tt').datagrid('getSelections');  
for(var i=0; i<rows.length; i++){  
    ids.push(rows[i].itemid);  
}  
alert(ids.join('\n'));

特别注意datagrid('getSelections')然后循环遍历所有结果。

编辑:添加一些更具体的帮助......

function removeUser(){
    var rows = $('#dg').datagrid('getSelections');
    $.messager.confirm('Confirm','Are you sure you want to remove this user?',function(r){
        if (r){
            for(var i=0; i<rows.length; i++) {        
                    $.post('remove_er.php',{id:rows[i].id},function(result){
                        if (!result.success){
                            $.messager.show({   // show error message
                                title: 'Error',
                                msg: result.msg
                            });
                        }
                    },'json');
            }
            $('#dg').datagrid('reload');    // reload the user data only once after deleting selected objects
        }
    });
}

代码没有经过测试,但概念就在那里。此方法一次应该足以处理十几个对象,但是如果您要删除的对象不止这些,则可能会使 Web 服务器过载。此方法为每个选中的元素发出一个 ajax 调用。

于 2013-08-01T15:33:41.653 回答