我有工作的codeigniter控制器和视图成功删除记录。我想用ajax-jquery来做,以避免重新加载页面,果然如果他确定删除记录,我将需要提示用户。
任何代码段都会受到高度赞赏,因为我不知道从哪里开始:
以下是控制器的代码:
public function deleteUser()
{
$this->load->model('backOfficeUsersModel');
$deleteWhat = array(
'dpage' => $this->input->post('dpage'),
'rid' => $this->input->post('rid')
);
$this->backOfficeUsersModel->delete($deleteWhat['rid']);
redirect($deleteWhat['dpage']);
} // end of function deleteUser
以下是我现在的观点:(为了简单起见,只提供了PHP代码)
foreach ($users as $key => $user) {
echo form_open('backOfficeUsers/deleteUser');
echo form_hidden('dpage', 'backOfficeUsers/displayAllUsers');
echo form_hidden('rid', $user['userid']);
echo "<tr>";
echo "<td>";
$data = array(
'name' => 'row_sel',
'id' => $user['userid'],
'value' => $user['userid'],
'class' => 'select_row'
);
echo form_checkbox($data);
//echo "<input type='checkbox' class='select_row' name='row_sel'>";
echo "</td>";
echo "<td>" . $user['userid'] . "</td>";
echo "<td>" . $user['firstname'] . " " . $user['lastname'] . "</td> ";
echo "<td>" . $user['username'] . "</td> ";
echo "<td><a href=mailto:" . $user['email'] . ">" . $user['email'] . "</td> ";
echo "<td style='text-align: center;'>";
echo anchor("backOfficeUsers/displayEditUserForm/$user[userid]/", "<i class=splashy-pencil></i>", "class=ext_disabled");
echo "</td>";
echo"<td style='text-align: center;'>";
$confirm = "class='splashy-remove' style='border:none; width:16px; height:16px;'";
echo form_submit('submit', '', $confirm);
echo"</td></tr>";
echo form_close();
}
任何帮助(代码段或某些示例的链接)都将深表赞赏
的问候,卓然
答案 0 :(得分:3)
请轻松查看echo
语句。如果您发现自己使用过度它可能因为您应该使用嵌入式PHP编写HTML而不是使用嵌入式HTML编写PHP。此外,在SO上发布问题时,请删除额外的绒毛只会使回答者混淆。您的代码中有很多 crap 无法访问问题的 heart 。
<table>
<thead>
<tr>
<th>Username</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<? foreach($users as $uK => $uV):?>
<tr>
<td><?= $uV['username']?></td>
<td><a class='deleteUser' href='backOfficeUsers/deleteUser/<?= $uV["id"]?>'>Delete</a></td>
</tr>
<? endforeach;?>
</tbody>
<script type='text/javascript'>
$(document).ready(function(){
$(".deleteUser").click(function(e){
$this = $(this);
e.preventDefault();
var url = $(this).attr("href");
$.get(url, function(r){
if(r.success){
$this.closest("tr").remove();
}
})
});
});
</script>
class Backofficeuser extends CI_Controller{
function __construct(){
...
}
function deleteUser($id){
$u = new User($id);
if($u->delete()) return json_encode(array("success" => true));
}
}
我在这里使用Datamapper的语法删除记录,请适应Active Record。代码没有经过测试,它是在SO编辑器中编写的 - 但这就是我要做的事情。