研发后2天,我决定寻求一些帮助,因为我无法继续前进。
我在我的网站上显示一个用户表。 每一行都有用户数据和删除用户按钮。
如果按删除键,一个模式都有露面,询问您是否要删除某些用户。 这就是为什么我要送一个参数是用户名。
这模式是确认模式与传说:你确定要删除//用户名//?
问题是,我知道如何发送参数,但不能将其与jQuery删除功能集成。 也没有如何删除行,因为我真的新手用JS和jQuery一个很清晰的概念。
到目前为止,这是我(请注意使用Smarty的模板引擎,即时通讯):
<tbody>
{foreach $frontusers as $frontuser}
<tr>
{if $frontuser->frontavatar_id eq null}
<td><img src="{site_url()}assets/img/avatar.png" alt="" /></td>
{else}
<td><img src="{site_url()}assets/img/avatar1.jpg" alt="" /></td>
{/if}
<td class="hidden-phone">{$frontuser->username}</td>
<td>{$frontuser->name}</td>
<td>{$frontuser->lastname}</td>
<td class="hidden-phone">{$frontuser->email}</td>
<td class="hidden-phone">{$frontuser->state}</td>
<td class="hidden-phone">{$frontuser->creation_date|date_format:"%Y/%m/%d"}</td>
{if $frontuser->status eq 2}
<td ><span class="label label-success">Activo</span></td>
{else}
<td ><span class="label label-warning">No Activo</span></td>
{/if}
<td><a class="btn mini blue-stripe" href="{site_url()}admin/editFront/{$frontuser->id}">Modificar</a></td>
<td><a href="#" data-id="{$frontuser->id}" class="btn mini red-stripe confirm-delete" role="button">Delete</a></td>
</tr>
<!-- modal -->
<div id="myModal3" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h3 id="myModalLabel3">Delete</h3>
</div>
<div class="modal-body">
<p>Are you sure you want to delete user ....?</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button data-dismiss="modal" class="btn red" id="btnYes">Confirm</button>
</div>
</div>
<!-- end modal -->
{foreachelse}
<tr>
<td colspan="2"><span class="text-error"><i class="icon-exclamation"></i> No hay Usuarios cargados.</span></td>
</tr>
{/foreach}
</tbody>
这是我的js文件(从我得到了它这个链接 )
$('#myModal3').on('show', function() {
var id = $(this).data('id'),
removeBtn = $(this).find('.red');
})
$('.confirm-delete').on('click', function(e) {
e.preventDefault();
var id = $(this).data('id');
$('#myModal3').data('id', id).modal('show');
});
$('#btnYes').click(function() {
// handle deletion here
var id = $('#myModal3').data('id');
$('[data-id='+id+']').remove();
$('#myModal3').modal('hide');
});
所以,总结一下,我需要适应我的代码执行以下操作:
- 发送用户名作为参数传递给模态
- 如果按下确认按钮删除整行
更新:
该模式的工作,我的意思是,它打开和关闭。 模态的确认按钮只删除从该行,而不是整个行“删除”按钮。
最后,可以使它发挥作用。 这里的这个短版(没有Ajax和智者和harcoded用户)在线和工作:
http://bootply.com/97366
视图:
<tbody>
{foreach $frontusers as $frontuser}
<tr>
{if $frontuser->frontavatar_id eq null}
<td><img src="{site_url()}assets/img/avatar.png" alt="" /></td>
{else}
<td><img src="{site_url()}assets/img/avatar1.jpg" alt="" /></td>
{/if}
<td class="hidden-phone">{$frontuser->username}</td>
<td>{$frontuser->name}</td>
<td>{$frontuser->lastname}</td>
<td class="hidden-phone">{$frontuser->email}</td>
<td class="hidden-phone">{$frontuser->state}</td>
<td class="hidden-phone">{$frontuser->creation_date|date_format:"%Y/%m/%d"}</td>
{if $frontuser->status eq 2}
<td ><span class="label label-success">Activo</span></td>
{else}
<td ><span class="label label-warning">No Activo</span></td>
{/if}
<td><a class="btn mini blue-stripe" href="{site_url()}admin/editFront/{$frontuser->id}">Edit</a></td>
//here in data-title i store the username so later i can "catch it" in the jquery function
<td><a href="#" class="confirm-delete btn mini red-stripe" role="button" data-title="{$frontuser->username}" data-id="{$frontuser->id}">Delete</a></td>
</tr>
<!-- modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel3" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
<h3 id="myModalLabel3">Delete</h3>
</div>
<div class="modal-body">
<p></p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Cerrar</button>
<button data-dismiss="modal" class="btn red" id="btnYes">Confirmar</button>
</div>
</div>
<!-- end modal -->
{foreachelse}
//no users are in the db
<tr>
<td colspan="2"><span class="text-error"><i class="icon-exclamation"></i> No hay Usuarios cargados.</span></td>
</tr>
{/foreach}
</tbody>
js文件:
//after first function is triggered, modal shows and this function runs
$('#myModal').on('show', function() {
//catch the id for later deletion, and username to display on modal
var id = $(this).data('id'),
username = $(this).data('usern');
$('#myModal .modal-body p').html("Do you want to delete user: " + '<b>' + username + '</b>' + ' ?');
})
//when clicking "delete" button from a row, this is the first function that runs
$('.confirm-delete').on('click', function(e) {
e.preventDefault();
//catch the user id and username
var id = $(this).data('id');
var user = $(this).data('title');
//assign to the modal id and username
$('#myModal').data('id', id).modal('show');
$('#myModal').data('usern', user).modal('show');
});
$('#btnYes').click(function() {
var id = $('#myModal').data('id');
//sending to php the row to be deleted from the db
$.ajax({
url: 'deleteFrontUser',
type: 'POST',
data: 'id='+id,
success: function(html){
//removing entire row
$('[data-id='+id+']').parents('tr').remove();
$('#myModal').modal('hide');
}
});
return false;
});