Skip to content

Instantly share code, notes, and snippets.

@mrofi
Created January 2, 2015 14:20
Show Gist options
  • Save mrofi/4419e366a83540fb7391 to your computer and use it in GitHub Desktop.
Save mrofi/4419e366a83540fb7391 to your computer and use it in GitHub Desktop.
CRUD Bootstrap 3 + jquery + laravel JS
('#tbody-data').delegate('tr .btn-edit', 'click', function(e) {
e.preventDefault();
backToCage();
btn = $(this);
tr = btn.parents('tr');
id = tr.attr('data-id');
$('#tbody-data tr.open-edit').next().remove();
if (tr.is('.open-edit')) {
tr.removeClass('open-edit');
return;
}
$('#tbody-data tr.open-edit').removeClass('open-edit')
tr.addClass('open-edit');
tr.after('<tr><td colspan="6"></td></tr>');
$('#edit-container').appendTo(tr.next().find('td'));
form = $('#edit-container').find('form');
form.find('button[type=cancel]').click(function(e) {
e.preventDefault();
$(this).parents('tr').prev().find('.btn-edit').click();
});
index = $('#tbody-data tr').index(tr);
for (x in allData[index]) {
if (allData[index][x] == '') continue;
form.find('[name='+x+']').val(allData[index][x]);
if (x == 'photo') form.find('.uploadPreview').attr('src', allData[index][x]);
}
form.find('[name=nama]').focus();
});
$('#tbody-data').delegate('tr .btn-hapus', 'click', function(e) {
e.preventDefault();
backToCage();
btn = $(this);
tr = btn.parents('tr');
id = tr.attr('data-id');
modal = $('#modal-hapus');
$('#tbody-data tr.open-edit').next().remove();
$('#tbody-data tr.open-edit').removeClass('open-edit')
index = $('#tbody-data tr').index(tr);
modal.modal('show');
modal.find('#nama-user').html(allData[index]['nama']);
});
$('.btn-konfirm-hapus').click(function(e) {
e.preventDefault();
bb = $(this);
modal = $('#modal-hapus');
bb.find('.icon').removeClass('hide');
$.post('/manager/user/delete', {_method: 'delete', id: id}, function(success) {
bb.find('.icon').addClass('hide');
if (success == 'ok') {
modal.find('.alert-success').removeClass('hide');
modal.modal('hide');
} else {
modal.find('.alert-danger').removeClass('hide').find('.global-error').html(success['global']);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment