我发现了错误。请帮助 我使用过dataTable,里面是我的删除按钮
$list = $this->foobar->get_datatables();
$data = array();
$userId = $_POST['start'];
foreach ($list as $foobar) {
$userId++;
$row = array();
$row[] = $foobar->userId;
$row[] = $foobar->UserType;
$row[] = $foobar->FirstName." ".$foobar->LastName;
$row[] = $foobar->EmailAddress;
$row[] = $foobar->Position;
$row[] = $foobar->BranchId;
$row[] = '<td class="w3-row" >
<form method="get" class="w3-half " action="edit_user/'.$foobar->userId.'">
<button class="btn btn-default" title="View">
<i class="fa fa-eye"></i></button></form>
<button class="btn btn-danger" href="javascript:void()" title="Delete" onclick="delete_user('.$foobar->userId.')">
<i class="fa fa-trash" ></i></button>
</td>';
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->foobar->count_all(),
"recordsFiltered" => $this->foobar->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
现在onclick将在我的javascript中找到该函数,例如delete_user和userdata
`
function delete_user(userId) {
swal({
title: "Are you sure?",
text: "You will not be able to recover this user account!",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
swal("Deleted!", "user account has been deleted.", "success");
//ajax delete data to database
$.ajax({
url: "<?php echo site_url('/Admin_Dashboard/delete_user/')?>" + userId,
type: "POST",
dataType: "JSON",
success: function(data) {
table.ajax.reload(null, false); //just reload table
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error deleting data');
}
});
} else {
swal("Cancelled", "user account is safe :)", "error");
}
});
} `
并且ajax将在我的控制器中搜索为Admin_Dashboard / delete_user
`function delete_user()
{
$this->User->delete_by_id($id);
}`
最后发送调用我的删除模型
public function delete_by_id($userId)
{
$this->db->where('userId', $userId);
$this->db->delete($this->table);
}
答案 0 :(得分:1)
您需要在控制器中更改功能以接收参数
function delete_user($id) //<---------- Add $id
{
$this->User->delete_by_id($id);
}